VBA error when referencing a defined worksheet

kgkev

Well-known Member
Joined
Jun 24, 2008
Messages
1,291
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have isolated a piece of code which has been working 3 years which stopped working.
I have found a work around but I would like to know why the original code wouldn't work

This code no longer works
It gives
Runtime Error '9'
Subscript out of range
VBA Code:
Sub test()
Dim MS As Worksheet
Set MS = Sheets("Chasing")
With MS
    .Range("H5", .Range("K5000")).ClearContents
End With
End Sub

This code works and has the desired effect.
VBA Code:
Sub test()
With Sheets("Chasing")
    .Range("H5", .Range("K5000")).ClearContents
End With
End Sub


even this works
VBA Code:
Sub test()
Dim MS As Worksheet
Set MS = Sheets("Chasing")
With MS
    Sheets("Chasing").Range("H5", .Range("K5000")).ClearContents
End With
End Sub


I use "MS....." throughout the module, probably 30+ instances and all other lines of code still function correctly.

Thanks for helping
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Are you sure the 'Chasing' Sheet exists in the currently active workbook?

Try fully qualifyng the sheet with its parent workbook ... Something along these lines:
VBA Code:
Sub test()
    Dim MS As Worksheet
    Set MS = Workbooks("Your_Workbook_Name_Goes_Here.xlsm").Sheets("Chasing")
    With MS
        .Range("H5", .Range("K5")).ClearContents
    End With
End Sub
 
Upvote 0
I have changed the declaration and it still doesn't work

There are also 3 lines of code which reference MS before the clear contents

VBA Code:
    Dim MS As Worksheet
        Set MS = Workbooks("Spares Chasing.xlsm").Sheets("Chasing")

Excel Formula:
    MS.Range("G1").Value = Now             
    MS.Range("G2").Value = Now
    MS.Range("G2").NumberFormat = "hh:mm"
    MS.Range("H5", MS.Range("K5000")).ClearContents
    MS.Range("A5", MS.Range("Z1000")).Interior.ColorIndex = xlNone
    MS.Range("A5", MS.Range("Z1000")).Font.Color = 1
 
Upvote 0
same error on the same line.

VBA Code:
MS.Range("H5", MS.Range("K5000")).ClearContents

but this works
VBA Code:
Sheets("Chasing").Range("H5", MS.Range("K5000")).ClearContents
 
Upvote 0
If I add a "pause" before the code that doesn't and run the suspect code in the immediate window...it works without error.

My head is spinning
 
Upvote 0
How about this : (Using the 'WorkSheets' collection instead of 'Sheets')
VBA Code:
Sub test()
Dim MS As Worksheet
Set MS = Worksheets("Chasing")
With MS
    .Range("H5", .Range("K5000")).ClearContents
End With
End Sub
 
Upvote 0
That works, Thank you.
Any idea why excel suddenly had a problem with this expression when its worked for years?
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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