FrenchCelt
Board Regular
- Joined
- May 22, 2018
- Messages
- 214
- Office Version
- 365
- Platform
- Windows
Hello,
I've been struggling to find a way to do this using my rudimentary VBA coding ability. I want to find specific text in a given row (4) and then select that cell plus the two cells above and one cell below. From there I will fill those cells with color (I have the VBA to do that once I have the cells selected). I tried two different methods and each one came up short.
My first try was especially awkward and it didn't do any looping. I basically did this:
And then repeated that three more times to select all the cells (offsetting -1, then no offset, and then offsetting 1) and give it the color I wanted. But that only worked for the first instance and the lack of code elegance was also disappointing. So I tried a loop function and this is what I tried:
All that did was select and color the entirety of Row 5, which is completely wrong.
Can anyone set me straight here? And if there is a way to do it one go, selecting the range of cells at once instead of having to do it four times to get each cell in the given columns being affected, that would be great.
I've been struggling to find a way to do this using my rudimentary VBA coding ability. I want to find specific text in a given row (4) and then select that cell plus the two cells above and one cell below. From there I will fill those cells with color (I have the VBA to do that once I have the cells selected). I tried two different methods and each one came up short.
My first try was especially awkward and it didn't do any looping. I basically did this:
VBA Code:
Dim i As Long
On Error Resume Next
For i = 1 To 10
Rows("4:4").Find("*STOP", Searchdirection:=xlRight).Offset(-2, 0).Select
Next
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With
And then repeated that three more times to select all the cells (offsetting -1, then no offset, and then offsetting 1) and give it the color I wanted. But that only worked for the first instance and the lack of code elegance was also disappointing. So I tried a loop function and this is what I tried:
VBA Code:
Dim rng As Range, cell As Range
On Error Resume Next
rng = Rows("4:4")
For Each cell In rng
If cell.Value = "*STOP" Then
cell.Offset(-2, 0).Select
End If
Next cell
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With
All that did was select and color the entirety of Row 5, which is completely wrong.
Can anyone set me straight here? And if there is a way to do it one go, selecting the range of cells at once instead of having to do it four times to get each cell in the given columns being affected, that would be great.