Referencing Column Range

Kel09

New Member
Joined
Jul 28, 2014
Messages
23
Hi, can anyone help me with this bit of code shown below (The code below forms part of a larger code) What it's suppose to do is add color to columns A-H. It runs but it will only add color to column A. Where have I gone wrong?
Note: S and L are = Dim L As Range / Dim S As Range

Many thanks


Code:

With ActiveSheet.Range("A13:H50")
Set S = Cells.Find("Small", MatchCase:=True, lookat:=xlWhole, LookIn:=xlValues)
If Not S Is Nothing Then
ActiveSheet.Range(Cells(13, 1), Cells(13, 8)).End(xlDown).Interior.ColorIndex = 3
End If
Set L = Cells.Find("Large", MatchCase:=True, lookat:=xlWhole, LookIn:=xlValues)
If Not L Is Nothing Then
ActiveSheet.Range(Cells(13, 1), Cells(13, 8)).End(xlDown).Interior.ColorIndex = 4
End If
End With


The only way I can get it to work is if I write the code in the following way:
With ActiveSheet.Range("A13:H50")
Set S = Cells.Find("Small", MatchCase:=True, lookat:=xlWhole, LookIn:=xlValues)
If Not S Is Nothing Then
ActiveSheet.Range(Cells(13, 1), Cells(13, 8)).End(xlDown).Interior.ColorIndex = 3
ActiveSheet.Range(Cells(13, 2), Cells(13, 8)).End(xlDown).Interior.ColorIndex = 3
ActiveSheet.Range(Cells(13, 3), Cells(13, 8)).End(xlDown).Interior.ColorIndex = 3
ActiveSheet.Range(Cells(13, 4), Cells(13, 8)).End(xlDown).Interior.ColorIndex = 3
ActiveSheet.Range(Cells(13, 5), Cells(13, 8)).End(xlDown).Interior.ColorIndex = 3
ActiveSheet.Range(Cells(13, 6), Cells(13, 8)).End(xlDown).Interior.ColorIndex = 3
ActiveSheet.Range(Cells(13, 7), Cells(13, 8)).End(xlDown).Interior.ColorIndex = 3
ActiveSheet.Range(Cells(13, 8), Cells(13, 8)).End(xlDown).Interior.ColorIndex = 3

End If
Set L = Cells.Find("Large", MatchCase:=True, lookat:=xlWhole, LookIn:=xlValues)
If Not L Is Nothing Then
ActiveSheet.Range(Cells(13, 1), Cells(13, 8)).End(xlDown).Interior.ColorIndex = 4
ActiveSheet.Range(Cells(13, 2), Cells(13, 8)).End(xlDown).Interior.ColorIndex = 4
ActiveSheet.Range(Cells(13, 3), Cells(13, 8)).End(xlDown).Interior.ColorIndex = 4
ActiveSheet.Range(Cells(13, 4), Cells(13, 8)).End(xlDown).Interior.ColorIndex = 4
ActiveSheet.Range(Cells(13, 5), Cells(13, 8)).End(xlDown).Interior.ColorIndex = 4
ActiveSheet.Range(Cells(13, 6), Cells(13, 8)).End(xlDown).Interior.ColorIndex = 4
ActiveSheet.Range(Cells(13, 7), Cells(13, 8)).End(xlDown).Interior.ColorIndex = 4
ActiveSheet.Range(Cells(13, 8), Cells(13, 8)).End(xlDown).Interior.ColorIndex = 4
End If
End With
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,471
Maybe try something like this...

Code:
    [color=darkblue]Dim[/color] LastRow [color=darkblue]As[/color] [color=darkblue]Long[/color]
    LastRow = Cells.Find("*", , LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=True).Row
    [color=darkblue]With[/color] ActiveSheet.Range("A13:H50")
        [color=darkblue]If[/color] [color=darkblue]Not[/color] .Cells.Find("Small") [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color]
            Cells(LastRow, 1).Resize(, 8).Interior.ColorIndex = 3
        [color=darkblue]ElseIf[/color] [color=darkblue]Not[/color] .Cells.Find("Large") [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color]
            Cells(LastRow, 1).Resize(, 8).Interior.ColorIndex = 4
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]End[/color] [color=darkblue]With[/color]
 
Upvote 0

Kel09

New Member
Joined
Jul 28, 2014
Messages
23
Maybe try something like this...

Code:
    [COLOR=darkblue]Dim[/COLOR] LastRow [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    LastRow = Cells.Find("*", , LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=True).Row
    [COLOR=darkblue]With[/COLOR] ActiveSheet.Range("A13:H50")
        [COLOR=darkblue]If[/COLOR] [COLOR=darkblue]Not[/COLOR] .Cells.Find("Small") [COLOR=darkblue]Is[/COLOR] [COLOR=darkblue]Nothing[/COLOR] [COLOR=darkblue]Then[/COLOR]
            Cells(LastRow, 1).Resize(, 8).Interior.ColorIndex = 3
        [COLOR=darkblue]ElseIf[/COLOR] [COLOR=darkblue]Not[/COLOR] .Cells.Find("Large") [COLOR=darkblue]Is[/COLOR] [COLOR=darkblue]Nothing[/COLOR] [COLOR=darkblue]Then[/COLOR]
            Cells(LastRow, 1).Resize(, 8).Interior.ColorIndex = 4
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]

Thank you, so much!
I had to tweak it a little bit. Ended up with this:

Dim LastRow As Long
LastRow = Cells.Find("*", , LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=True).Row
With ActiveSheet.Range("A13:H50")
If Not .Cells.Find("Small") Is Nothing Then
Cells(LastRow, 1).Resize(, 8).Interior.ColorIndex = 3
If Not .Cells.Find("Large") Is Nothing Then
Cells(LastRow, 1).Resize(, 8).Interior.ColorIndex = 4
End If
End If
End With
 
Upvote 0

Forum statistics

Threads
1,195,683
Messages
6,011,137
Members
441,587
Latest member
kbsgiri09

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top