"Subscript out of range" error when using variables as worksheet reference

msthiagu

New Member
Joined
May 20, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi. I have been using excel macro as below and experiencing "Runtime error 9: Subscript out of range" error. It happens only when I use a variable to reference a worksheet within a workbook.
vbaerror.PNG
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
That error just means that the sheet name is wrong (i.e. it doesn't exist in that workbook).
 
Upvote 0
That error just means that the sheet name is wrong (i.e. it doesn't exist in that workbook).
I have tried to debug the code where the error has occured by introducing a msgbox statement just before that. The result was the sheet name (1029, etc.) available in the workbook. Screenshot of sheets in the workbook attached for your reference. Kindly suggest a probable solution.
 

Attachments

  • vbaerror1.PNG
    vbaerror1.PNG
    19.5 KB · Views: 16
Upvote 0
The code should work assuming the correct workbook is active at the time because you have explicitly declared the variable as a string, but you could try adding quotes like this:

Code:
Set wsdest1 = wb1.sheets("" & shname1)

I'm not sure why you are calculating lrow2 on a different sheet to the one whose cells you are reading.
 
Upvote 0
The result was the sheet name (1029, etc.)
What is the significance of the "etc" ? What is the name in the "last" message box before it errors out ?

Also a lot of your declarations don't work and will result in the variables being variant
eg Your worksheet and long dim lines should look like this:
VBA Code:
Dim wscopy1 As Worksheet, wsdest1 As Worksheet, wsinterim1 As Worksheet
Dim a As Long, lrow2 As Long, lrow3 As Long
 
Upvote 0
The code should work assuming the correct workbook is active at the time because you have explicitly declared the variable as a string, but you could try adding quotes like this:

Code:
Set wsdest1 = wb1.sheets("" & shname1)

I'm not sure why you are calculating lrow2 on a different sheet to the one whose cells you are reading.
Hi. I tried the quote mentioned. But this doesn't work
Code:
Set wsdest1 = wb1.sheets("" & shname1)

My objective is to find a value in "wscopy1" worksheet. Go to the respective sheet in the workbook and copy paste data between those 2 sheets.
"Lrow2" is used to find the length of the row.
 
Upvote 0
You calculate lrow2 based on the data in Sheet20 (the Service sheet) but then when you use it in the loop, you're looking at the SCM sheet, which seems odd.

If you are still getting the error, then the sheet name does not exist in the specified workbook. There is no other cause.
 
Upvote 0
What is the significance of the "etc" ? What is the name in the "last" message box before it errors out ?

Also a lot of your declarations don't work and will result in the variables being variant
eg Your worksheet and long dim lines should look like this:
VBA Code:
Dim wscopy1 As Worksheet, wsdest1 As Worksheet, wsinterim1 As Worksheet
Dim a As Long, lrow2 As Long, lrow3 As Long
The msg box before the error pops out is attached. Declarations as suggested also doesn't help.
The same code works in other sheets in this workbook.
 

Attachments

  • vbaerror2.PNG
    vbaerror2.PNG
    4.5 KB · Views: 6
Upvote 0
You calculate lrow2 based on the data in Sheet20 (the Service sheet) but then when you use it in the loop, you're looking at the SCM sheet, which seems odd.

If you are still getting the error, then the sheet name does not exist in the specified workbook. There is no other cause.
I found that bug and it is already corrected. Still the error persists.
 
Upvote 0
Hi,
try changing this line

VBA Code:
Set wsdest1 = wb1.Sheets(shname1)

for this

VBA Code:
Set wsdest1 = wb1.Worksheets(CStr(shname1))

and see if resolves your issue

Dave
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,239
Members
452,621
Latest member
Laura_PinksBTHFT

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