Workboos Object : Subscript out of range

Apple1

Board Regular
Joined
Jan 18, 2015
Messages
121
Hi

I get the error Subscript out of range in the below code. What is wrong with it? Thank you




Option Explicit

Sub test()


Dim i As Integer



For i = 1 To 6
ThisWorkbook.Worksheets(1).Cells(i, 1).Value = Workbooks(2).Worksheets(i).Name


Next

End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi

Thank you. Yes Workbooks(2) have 6 shsets


QUOTE=AlphaFrog;4929414]Does Workbooks(2) have 6 worksheets?

Does it list the names of any sheets before it errors?[/QUOTE]
 
Upvote 0
My guess is that you misinterpret which is Workbook(2).
Add this line in this position:
Code:
For i = 1 To 6
Debug.Print Workbooks(2).Name               '<<< ADD THIS LINE
ThisWorkbook.Worksheets(1).Cells(i, 1).Value = Workbooks(2).Worksheets(i).Name
Next

Then, while in vba debug mode, open the "Immediate" window (press Contr-g), anche check is the printed name is "your" Workbooks(2)

Bye
 
Last edited:
Upvote 0
Try this...

Code:
[color=darkblue]Sub[/color] test()
    
    [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Integer[/color]
    
    [color=darkblue]For[/color] i = 1 [color=darkblue]To[/color] [B]Workbooks(2).Worksheets.Count[/B]
        ThisWorkbook.Worksheets(1).Cells(i, 1).Value = Workbooks(2).Worksheets(i).Name
    [color=darkblue]Next[/color]
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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