Feb 14 2023 09:37 PM
Hi,
The following code searches a large database for cells with a certain fill color and then removes the underlying conditional formatting. The runtime is quite slow. Can this code be made more efficient?
Thank you.
RGBCode = RGB(192, 0, 0) For Each Cell In Range("D:D") If Cell.Interior.Color <> RGBCode Then GoTo Label10 Cell.Select Selection.FormatConditions.Delete Label10: Next Cell
Feb 15 2023 03:34 AM
Is this better?
Sub Test()
Dim RGBCode As Long
Dim Cell As Range
Dim Rng As Range
Dim Adr As String
Application.ScreenUpdating = False
RGBCode = RGB(192, 0, 0)
With Application.FindFormat
.Clear
.Interior.Color = RGBCode
End With
Set Cell = Range("D:D").Find(What:="", LookAt:=xlPart, SearchFormat:=True)
If Not Cell Is Nothing Then
Adr = Cell.Address
Do
If Rng Is Nothing Then
Set Rng = Cell
Else
Set Rng = Union(Cell, Rng)
End If
Set Cell = Range("D:D").Find(What:="", After:=Cell, LookAt:=xlPart, SearchFormat:=True)
If Cell Is Nothing Then Exit Do
Loop Until Cell.Address = Adr
Rng.FormatConditions.Delete
End If
Application.ScreenUpdating = True
End Sub
Mar 05 2023 02:41 PM
Mar 05 2023 03:03 PM
Try this. Please test it on a smaller subset first, to see if it works.
Sub Obsolete()
Const SBSO = "SBS Online"
Dim l1Row As Long
Dim II As Long
Dim JJ As Long
Dim v As Variant
Application.ScreenUpdating = False
l1Row = Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
v = Range("A1:F" & l1Row).Value
For II = 2 To l1Row - 1
For JJ = II + 1 To l1Row
If v(II, 4) = v(JJ, 4) And v(II, 3) = v(JJ, 3) Then
If v(II, 5) <> SBSO And v(JJ, 5) <> SBSO Then Exit For
If v(II, 5) = SBSO And v(II, 6) > v(JJ, 6) Then v(II, 1) = "del"
If v(JJ, 5) = SBSO And v(JJ, 6) > v(II, 6) Then v(JJ, 1) = "del"
Exit For
End If
Next JJ
Next II
Range("A1:F" & l1Row).Value = v
Application.ScreenUpdating = True
End Sub
Mar 05 2023 03:17 PM
Mar 05 2023 03:33 PM
My version reads the data into an array, traverses the array and then writes the array back to the sheet in one go.
Array manipulations are much faster than sheet manipulations.
Mar 05 2023 03:41 PM
Mar 06 2023 04:04 AM
Mar 22 2023 09:28 PM
Mar 23 2023 06:32 AM
Let's say you have a combo box DateCombo. In the userform's code module:
Private Sub UserForm_Initialize()
Dim d As Date
For d = Date - 14 To Date + 14
Me.DateCombo.AddItem d
Next d
Me.DateCombo = Date
End Sub
Mar 23 2023 03:51 PM
Mar 24 2023 04:47 AM
Jul 24 2023 09:38 PM
Jul 24 2023 11:13 PM
You can use the Find method to search for cells with the specific fill color, which can significantly improve the speed.
Sub RemoveConditionalFormattingByColor()
Dim RGBCode As Long
Dim FoundCell As Range
Dim FirstAddress As String
RGBCode = RGB(192, 0, 0)
' Search for the first cell with the specified fill color
Set FoundCell = Columns("D:D").Find(What:=RGBCode, LookIn:=xlFormulas, LookAt:=xlWhole, SearchFormat:=True)
' If a cell with the specified fill color is found, start the loop
If Not FoundCell Is Nothing Then
FirstAddress = FoundCell.Address
Do
' Remove the conditional formatting for the current cell
FoundCell.FormatConditions.Delete
' Find the next cell with the specified fill color
Set FoundCell = Columns("D:D").FindNext(FoundCell)
' Continue the loop until all cells with the specified fill color are found
Loop While Not FoundCell Is Nothing And FoundCell.Address <> FirstAddress
End If
End Sub
Try using this code once.
Best Regards.
Jul 25 2023 02:39 AM
Hi Wayne, that depends on what the rest of the macro does.
If possible, the fastest way to do this would be to read the entire range into an array (that is just one instruction), manipulate that array, then writing the array back to the sheet (also one instruction).
Array manipulations are blindingly fast compared to cell manipulations.
Jul 25 2023 03:54 PM
Jul 25 2023 04:37 PM
Jul 26 2023 12:36 AM
So the code applies to a single row only? Is that correct?
Jul 26 2023 12:38 AM
Jul 26 2023 12:42 AM - edited Jul 26 2023 12:42 AM
Yes, but that is a single number - the code applies to row 25330, not to any other rows.
Or am I mistaken?
Sorry, I didn't look closely enough.
Stay tuned.