Fin
2014-05-05 20:07:41 UTC
Using Vbscript for an Excel 2010 worksheet, how can I find the start and end range of distinct values with a column ?
I have a worksheet with some 200k rows, and 45 columns. 20 of such columns contain rows indicating a color value that I need to set. I have code that works but when working on 200k rows it is incredibly slow.
Set objRange = objXLWs.UsedRange
iRows = objRange.Rows.Count
iColumns = objRange.Columns.Count
For iR = 2 To iRows
Select Case objRange.Item(iR, iC).Value
Case "Y"
objXLApp.Range(objRange.Item(iR, iC-1),objRange.Item(iR, iC-1)).Interior.ColorIndex = 6 'yellow
Case "R"
objXLApp.Range(objRange.Item(iR, iC-1),objRange.Item(iR, iC-1)).Interior.ColorIndex = 3 'red
Case "O"
objXLApp.Range(objRange.Item(iR, iC-1),objRange.Item(iR, iC-1)).Interior.ColorIndex = 45 'orange
Case "T"
objXLApp.Range(objRange.Item(iR, iC-1),objRange.Item(iR, iC-1)).Interior.ColorIndex = 42 'terquoise
End Select
Next
What I am thinking is that sorting the column, bringing all the color values to the top, then finding the distinct ranges of each of the 4 x color values and trying to color by a range rather than individual row case statement. Sadly I have no idea how to do so. Obviously I would then resort by the original file format following all columns having been colored.
Can anyone please give me some example code on how I would do this ? I have sort code that seems to work, I just cannot figure how to get start and end cell addresses for a block color statement.
Set objRange2 = objXLWs.Cells(2, iC)
objXLWs.UsedRange.Sort objRange2, xlAscending, , , , , , xlYes
Many thanks for any help, Fin.
I have a worksheet with some 200k rows, and 45 columns. 20 of such columns contain rows indicating a color value that I need to set. I have code that works but when working on 200k rows it is incredibly slow.
Set objRange = objXLWs.UsedRange
iRows = objRange.Rows.Count
iColumns = objRange.Columns.Count
For iR = 2 To iRows
Select Case objRange.Item(iR, iC).Value
Case "Y"
objXLApp.Range(objRange.Item(iR, iC-1),objRange.Item(iR, iC-1)).Interior.ColorIndex = 6 'yellow
Case "R"
objXLApp.Range(objRange.Item(iR, iC-1),objRange.Item(iR, iC-1)).Interior.ColorIndex = 3 'red
Case "O"
objXLApp.Range(objRange.Item(iR, iC-1),objRange.Item(iR, iC-1)).Interior.ColorIndex = 45 'orange
Case "T"
objXLApp.Range(objRange.Item(iR, iC-1),objRange.Item(iR, iC-1)).Interior.ColorIndex = 42 'terquoise
End Select
Next
What I am thinking is that sorting the column, bringing all the color values to the top, then finding the distinct ranges of each of the 4 x color values and trying to color by a range rather than individual row case statement. Sadly I have no idea how to do so. Obviously I would then resort by the original file format following all columns having been colored.
Can anyone please give me some example code on how I would do this ? I have sort code that seems to work, I just cannot figure how to get start and end cell addresses for a block color statement.
Set objRange2 = objXLWs.Cells(2, iC)
objXLWs.UsedRange.Sort objRange2, xlAscending, , , , , , xlYes
Many thanks for any help, Fin.