Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #11  
Old 02-05-2025, 06:12 PM
gmaxey gmaxey is offline VBA code that will apply to certain cells based on their value Windows 10 VBA code that will apply to certain cells based on their value Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Here is the revised procedure. Likely cobbled as I am not very familiar with Excel:

Code:
Sub highlight_cell(ByVal rng As Range)
Dim length As Long, color As Long, text As String, separator As String, colors, re As Object
'*** Added - GKMA
Dim lngCol As Long, lngIndex As Long
  lngCol = rng.Column
  'Clear any existing color coding.
  If rng.Interior.color <> 16777215 Then
    rng.Interior.color = 16777215
    lngIndex = 1
    Do While ActiveSheet.Cells(rng.Row, lngCol + lngIndex).Interior.color <> 16777215 And ActiveSheet.Cells(rng.Row, lngCol + lngIndex).Value = ""
      ActiveSheet.Cells(rng.Row, lngCol + lngIndex).Interior.color = 16777215
      lngIndex = lngIndex + 1
    Loop
  End If
'*** End add.
  Set re = CreateObject("VBScript.RegExp")
  re.IgnoreCase = True
  '*** GKM changed to more muted colors (the US blue was difficult to read)
  colors = Array("15985347", "5296274", "65535", "12439801", "6908415", "0")
  separator = Mid(3 / 2, 2, 1)
  text = rng.Value
  If UCase(text) <> "L" Then
    re.Pattern = "^((?:\d*\.)?\d+)(?:|\-)(US|UK|BR|AP).*$"
    If re.test(text) Then
      With re.Execute(text).Item(0)
        text = .SubMatches(0)
        length = Replace(text, ".", separator) / 0.25
        color = colors((InStr(1, "USUKBRAPL", .SubMatches(1), vbTextCompare) - 1) / 2)
      End With
    End If
  Else
    length = 33 - rng.Column + 1
    color = colors(4)
  End If
  '*** Modified GKM
  If lngCol + length <= 34 Then
    If length Then rng.Resize(1, length).Interior.color = color
  Else
    MsgBox "The defined task duration exceeds the shift period."
  End If
  Set re = Nothing
  '*** End mods
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
 



Similar Threads
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
VBA code that will apply to certain cells based on their value VBA Code apply draft 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

Other Forums: Access Forums

All times are GMT -7. The time now is 08:21 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft