![]() |
#2
|
|||
|
|||
![]() Quote:
Seeker, I have quite a bit of experience with VBA in Word, but just a dabbler with Excel VBA. You will most likely get a much better solution or even suggestions for a better way. Anyway, as I understand your requirement. You have these sheets where you manually enter your code sequences e.g. 2-US1234, .75-UK5679 etc. and you want a macro to apply your color coding. Correct? If yes, then you will need to make a slight change to your codes. L for leave is fine as it is all day. However your coding for BR needs to be changed from .5BR, 1BR etc. to .5-BR, 1-BR to match the separator used for US, UK and AP. If you can do that, then this a macro may work for you: Copy all of the code to a standard module. Run Color Code Sheets. If you don't like the colors applied the replace the *###### part in the varCodes to the number of the color you like. Code:
Dim oSheet As Worksheet Dim varCodes, varCats, varColors Sub ColorCodeSheets() Dim lngIndex As Long varCodes = Split("US*15985347|UK*5296274|BR*65535|AP*12439801|L*6908415", "|") For Each oSheet In ThisWorkbook.Sheets For lngIndex = 0 To UBound(varCodes) FindCat varCodes(lngIndex) Next Exit For Next lbl_Exit: Exit Sub End Sub Function FindCat(strCode) Dim oRng As Range Dim strAddr As String Dim strCat As String, lngColor As Long strCat = Split(strCode, "*")(0) lngColor = CLng(Split(strCode, "*")(1)) With oSheet.UsedRange Set oRng = .Find(What:=strCat, LookIn:=xlValues) ProcessFind oRng, lngColor strAddr = oRng.Address If Not oRng Is Nothing Then On Error GoTo Err_GetOut Do Set oRng = .FindNext(oRng) ProcessFind oRng, lngColor Loop While Not oRng Is Nothing And oRng.Address <> strAddr End If End With Err_GetOut: If Err.Number <> 0 Then Err.Clear On Error GoTo 0 Set oRng = Nothing lbl_Exit: Exit Function End Function Sub ProcessFind(oRng As Range, lngColor As Long) Dim varParts Dim lngLen As Long varParts = Split(oRng.Value, "-") If UBound(varParts) = 1 Then lngLen = varParts(0) / 0.25 Else lngLen = 32 End If oRng.Resize(1, lngLen).Interior.Color = lngColor lbl_Exit: Exit Sub End Sub |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Formula or VBA code to format certain cells based on the cell value | Seeker2025 | Excel | 1 | 02-03-2025 01:14 AM |
Apply a formula based on whether a cell is blank or has a date | NLJ | Excel | 4 | 09-17-2021 06:02 AM |
![]() |
lodi123 | Word VBA | 1 | 02-21-2017 04:55 AM |
Conditional formating all cells in an array based on adjacent cells | deejay | Excel | 6 | 12-20-2016 12:00 PM |
Is it possible to have a style apply two formats based upon text? | DMcCollum | Word | 3 | 05-02-2015 06:29 PM |