reference to sheet by name behaviour I cannot explain

nabuzzard

New Member
Joined
Feb 8, 2008
Messages
11
I have a VBA module that seeks to clear the contents of a range of cells on a sheet. The code line that does this is:

ThisWorkbook.Worksheets("core").Range(Cells(1, 10), Cells(2, last_col)).ClearContents

I am certain that the last_col variable is an integer, and has a valid value. Oddly, when this runs (the code executes automatically on opening the workbook) I get an error. If I then click to rename the worksheet, but make no change to the worksheet name (just right click, select rename then click off to exit the rename mode), and rerun the code it works fine, despite there being not change to the code or the sheetname!

I have looked all over and cannot understand what is changing by going into rename sheet? I assume that it is in some way forcing excel to recognise the sheetname where it had not previously - but why would this be?

I have not posted the full code at it is very extensive, with multiple modules, userforms and class modules. It is only this bit that gives the error, and then not always. I can apply a workaround, but wondered if anyone can explain the cause of the behaviour.

Thank you in advance to anyone who has insight
N
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Well .
Testing this line of code working ok
Must be something else where
 
Last edited:
Upvote 0
You don't have workbook/worksheet references for Cells.
VBA Code:
With ThisWorkbook.Worksheets("core")
    .Range(.Cells(1, 10), .Cells(2, last_col)).ClearContents
End With
[/vba]
 
Upvote 0
Solution
You don't have workbook/worksheet references for Cells.
VBA Code:
With ThisWorkbook.Worksheets("core")
    .Range(.Cells(1, 10), .Cells(2, last_col)).ClearContents
End With
[/vba]
Spot on, I had assumed in error that when defining a range on a sheet that any cells identified would automatically be on that sheet. Teach me to assume anything in vba.

Thank you for assisting - the revised code works seamlessly.

N
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,249
Members
449,075
Latest member
staticfluids

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