reference a sheet

lezhi

New Member
Joined
Aug 18, 2011
Messages
12
Hi, I want to specify a sheet with varible i:

i = wbkCur.Worksheets("Openwbk").Range("d6")
FileName = "C:\Users\Lele\" & i & ".xlsx"
Set wbkNew = Workbooks.Open(FileName:=FileName)
With Sheets(i)
.Rows("1:1").Insert Shift:=xlDown

It gave the error of "Subscript out of range", at sentence "With Sheets(i)". Please help.

Thx,

Rachel
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi Rachel,

Your code uses i to specify the workbook-name to be opened.

So seems not proper to use i to specify a sheet inside the workbook.

Try

With Sheets(1)

or the name of the sheet you want,ie, something like

With Sheets("Sheet1")

HTH

M.
 
Upvote 0
Thank you for the reply. The reason I used i is the code is in a loop, each time, i changes. (from 1 to 36) Is there anyway to implement it?

Rachel
 
Upvote 0
Are there 36 sheets in the workbook?

If so, maybe using another variable like

Code:
Dim j as long
 
For j = 1 to 36
   With Sheets(j)
       'your code here
   End With
Next j

M.
 
Upvote 0
How is the variable 'i' declared in your code? String? Long? etc

Is it correct that the files that you are opening have the same name (i) as a sheet in that workbook?

Guessing a bit, at least one change I think you will need is this
Rich (BB code):
With wbkNew.Sheets(i)
 
Upvote 0
Thank you! Yes, the sheet name is the same as the file name. I tried With wbkNew.Sheets(i), but still got the same error...
 
Upvote 0
Thank you! Yes, the sheet name is the same as the file name. I tried With wbkNew.Sheets(i), but still got the same error...
What about my other question?

Could we see the whole code if it isn't too long?

When you get the error, click Debug and hover over the 'i' variable. What value is it showing?

What value is in D6 where i is being drawn from?
 
Upvote 0
Thank you! Yes, the sheet name is the same as the file name. I tried With wbkNew.Sheets(i), but still got the same error...

You said you want to loop through 36 sheets - some must have a different name of the workbook-name...

Or do you want to open 36 workbooks all having a sheet with the same same of the workbook?

M.
 
Upvote 0
Hi Peter,
D6 is my input to determine which file to start. (I put 1-36 only) When I get the error, the 'i' variable has value 16, same as what I put in D6.

Below is the code:

Sub Mainfunction()
Dim i As Long
Dim FileName As String
Dim wbkCur As Workbook
Dim wbkNew As Workbook
Set wbkCur = Workbooks("Function book.xlsm")

For i = wbkCur.Worksheets("Openwbk").Range("d6").Value To 36
'might need to change on diff pc
FileName = "C:\Users\Lele\" & i & ".xlsx"
Set wbkNew = Workbooks.Open(FileName:=FileName)
With Sheets(i)
.Rows("1:1").Insert Shift:=xlDown
.Range("A2").Value = MyCount
End With
wbkNew.Close SaveChanges:=True
Next i

End Sub
 
Last edited:
Upvote 0
Yes, I open 36 workbooks one by one. and each has a sheet name the same as the workbook's name.
 
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,572
Members
452,927
Latest member
whitfieldcraig

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