VBA Problem: Subscript Out of Range

craig159753

New Member
Joined
Apr 21, 2015
Messages
24
Hi all,

I am seeing a strange error when running a simple line of VBA code.
When I personally run the VBA macro I do NOT get the error message but when my colleague runs the VBA macro they get the "subscript out of range" error.

The line of code causing this is the following.

Code:
Dim workbookname as string
Dim worksheetname as string

Workbooks(workbookname).Worksheets(worksheetname).Range("C177:C1357").Copy

It is merely selecting the values from column C from row 177 to row 1357 and copying them.
Any idea why this runs fine for me but fails for my colleague?


Thanks
Craig
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Add this bit of code just before the offending line executes:

Code:
For Each bk In Workbooks
    If LCase(bk.Name) = LCase(workbookname) Then
        MsgBox "Book ok"
    End If
Next
For Each sh In Worksheets
    If LCase(sh.Name) = LCase(worksheetname) Then
        MsgBox "Sheet ok"
    End If
Next

What msgboxes do you see?
 
Upvote 0
Hi Steve,

I figured out the problem, it was the workbookname value.

Originally I was deriving the workbookname like so

Code:
workbookname = Trim(Left(WB.Name, InStr(ActiveWorkbook.Name, ".") - 1))

Which was working fine for me, but not my colleague, I therefore simply updated this line to the following

Code:
workbookname = ActiveWorkbook.Name

All working for everyone now.

Thanks
Craig
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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