Cell Ranges within sheets

Flue

Board Regular
Joined
Feb 5, 2009
Messages
106
HI all.

Can some explain why this code;
Code:
Range(Cells(FoundCell.Row, 1), Cells(FoundCell.Row, 6)).Interior.ColorIndex = 6
works for the main sheet, but as soon as you want to assign a sheet name (if working with multiple sheets) to it you have to use;
Code:
Sheets("TEST1_15").Range("A" & i & ":F" & i).Interior.ColorIndex = 6

Even this works;
Code:
 Sheets("TEST1_15").Range(Cells(FoundCell.Row, 1)).Interior.ColorIndex = 6
But add that second Cell for the range and it fails.

Any Idea?

Thanks.

Brian
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
As an FYI, whenever you post code question that use variables but don't post the whole code, the post often goes ignored.

Post the whole code and highlight the line you are talking about, and then people can play with it and see if its a VBA quirk or something in your design (foundCell definition??).
 
Upvote 0
Good to know. Thanks.

Rich (BB code):
Sub CompareSheets()
Dim FoundCell, LastCell, FirstAddr
Dim lCount, tCount, i
Application.ScreenUpdating = False

lCount = Sheets("LIVE1_15").UsedRange.Rows.Count
tCount = Sheets("TEST1_15").UsedRange.Rows.Count

With Sheets("LIVE1_15").UsedRange
    Set LastCell = .Cells(.Cells.Count)
End With

For i = 1 To tCount

Set FoundCell = Sheets("LIVE1_15").UsedRange.Find(what:=Sheets("TEST1_15").Cells(i, 2), after:=LastCell)

If Not FoundCell Is Nothing Then
    FirstAddr = FoundCell.Address
End If

Do Until FoundCell Is Nothing
    
    Set FoundCell = Sheets("LIVE1_15").UsedRange.FindNext(after:=FoundCell)
'Highlight matching rows on both sheets  
  Range(Cells(FoundCell.Row, 1), Cells(FoundCell.Row, 6)).Interior.ColorIndex = 6  'This works...If the sheet is not referenced.
    Sheets("TEST1_15").Range("A" & i & ":F" & i).Interior.ColorIndex = 6 'This Works
'But this will not....Sheets("TEST1_15").Range(Cells(i, 1), Cells(i, 5)).Interior.ColorIndex = 6


      
    If FoundCell.Address = FirstAddr Then
        Exit Do
    End If
Loop

Next
End Sub
 
Upvote 0
Ahh I see. I don't have a certain answer, but I know it traces its roots back to using objects and certain syntax not understanding the object to use. Some of the more experienced coders would be better able to explain it, but basically the non-functioning syntax seeems to not recognize the worksheet (I don't know why). It only seems to know the worksheet it is on, having last referenced the Sheets("LIVE1_15") in the last setting of FoundCell before your errored code.

the quick and dirty way around it is to select the sheet you want with a quick "Sheets("TEST1_15").Select line before you do the formatting and that fixes it.

If you post a question about not using the the Select method (which the top people on this site advise against) and explain the erro situation, you'd probably get the perfect answer. I've always found a way to work around, like you did using Range without the Cells function.
 
Upvote 0
Try like this

Code:
With Sheets("TEST1_15")
    .Range(.Cells(i, 1), .Cells(i, 5)).Interior.ColorIndex = 6
End With
 
Upvote 0
Vog, I've noticed sometime even when using the With method that I have to do a .Select line before doing the next action (i.e. changing a format).

What causes this sort of situation, as it usually happens when i toggle bacnk and forth between tabs? I'm not a perfect coder, but does certain syntax solve it? Does Option Explicit solve it (I never use it because I don't know what it is)?

I've tried abolishing .Select from my syntax but sometimes it works when nothing else will.
 
Upvote 0
It is almost never necessary to select. In fact the only case that springs to mind with a worksheet is setting Freeze Panes. Can you give an example where you have to select.

Option Explicit is something different - it requires you to declare (Dim) all variables.
 
Upvote 0
Did you try with the code in the post above. I created a dummy file, re-named 2 sheets as they were in the code provided, and added dummy data in column B of each sheet. I got the same error.
 
Upvote 0
No but this worked for me with a different sheet selected

Code:
Sub test()
Const i = 5
With Sheets("TEST1_15")
    .Range(.Cells(i, 1), .Cells(i, 5)).Interior.ColorIndex = 6
End With
End Sub
 
Upvote 0
Right, like I said, there are workarounds to most stuff.

But in his example, this didn't work:

Sheets("TEST1_15").Range(Cells(i, 1), Cells(i, 5)).Interior.ColorIndex = 6

but this did:
Sheets("TEST1_15").select
Sheets("TEST1_15").Range(Cells(i, 1), Cells(i, 5)).Interior.ColorIndex = 6
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

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
Back
Top