sheet1 code name vs sheets(1)

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I opened file A.xlsxm and wrote the code below. I ran the code and entered B.xlsx. The code below did not work until I changed the 5th line to the following

Rich (BB code):
 Sheet1.Move before:=Workbooks(2).Sheets(1)

Rich (BB code):
Sub abc()
    Dim x As String
    x = InputBox("file")
    Workbooks.Open (x)
    Sheet1.Move before:=Workbooks(2).Sheet1
End Sub

Why workbooks(2).sheet1 did not work? I also tried to use the Immediate window, and tried after opening B.xlsx

?Sheet1.name --> got A.xlsx

then tried

?workbooks(2).Sheet1.name --> got error message

In project window, I see both A and B have Sheet1 before the move. Thank you.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
You have tripped over a restriction on using sheet CodeName
- the code name is ALWAYS Sheet1 (the code fails if you put a workbook reference before it - ThisWorkbook.Sheet1 fails)
- Sheet1 ALWAYS refers to the sheet with codename Sheet1 in the workbook containing the code
- you cannot use a sheet CodeName when you reference a sheet that is in a different workbook to the one that the code resides
 
Last edited:
Upvote 0
- you cannot use a sheet CodeName when you reference a sheet that is in a different workbook to the one that the code resides

You can if you set a reference between the two workbooks. You then need to refer to it using projectname.codename syntax rather than just the codename.
 
Upvote 0
Thanks for that @RoryA :)
Any chance of an example of the syntax to use?
 
Upvote 0
It's just what I said earlier - projectname.codename and not just codename. So given a vba project named say pjTest, you'd use pjTest.Sheet1 from the referencing project.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
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