I have the following VBA code to color the cells of a table, which is the result of a query that can be updated time after time.
Sub ColorMain()
Dim Data As Range
Dim cell As Range
Sheet1.Activate
Range("D7").Select
Range(Selection, Selection.End(xlDown)).Select
'clean the coloring
Set Data = Selection
Data.Interior.ColorIndex = 0
'apply the coloring
For Each cell In Data
If cell.Value = "X" Then
cell.Interior.ColorIndex = 22
ElseIf cell.Value = "Y" Then
cell.Interior.ColorIndex = 44
End If
Next
End Sub
I am calling this Sub from another main Sub which updates the SQL query. When running the main Sub coloring applies to some cells, but not all. When I run the main Sub one more time, then coloring applies to all cells appropriately.
When I run the coloring Sub line by line, it works perfectly. Why it is not working from the first attempt when calling main Sub? Do I have to pause the application somewhere?
EDITED: Main Sub refreshes the queries I have in the workbook and calls ColorMain:
Sub RefreshAll()
ActiveWorkbook.RefreshAll
Call ColorMain
End Sub