I would like to get the value of cell. I have multiple workbooks, and some of the workbooks have sheets with the same name.
For example, test.xlsx and test2.xlsx both has a sheet named sheet1.xlsx
So, when working on a sheet, I would like to specify the workbook. I use wb.sh.*expression* all the time, and I am surprised that this does not work.
What am I missing here conceptually?
Code:
set wb1 = Workbooks("Test1.xlsx") Set sh1 = Worksheets("Sheet1") Debug.Print wb1.sh1.Range("A1").Value Code which would work, but is not specific enough:
set wb1 = Workbooks("Test1.xlsx") Set sh1 = Worksheets("Sheet1") Debug.Print sh1.Range("A1").Value Note: Test1.xlsx has a sheet named Sheet1
sh1s you can doSet sh1 = wb1.Worksheets("Sheet1")...That way you don't have to include the workbook name each time too.wb1.sh1...You can't nest declared objects like that. It either needs to bewb1.Sheets("Sheet1")...or simplysh1.... If you want to specify the workbook for thesh1variable, do it in your Set line:Set sh1 = wb1.Sheets("Sheet1")and then you can check what workbook it's a part of by looking at it's .Parent.Name property:Debug.Print sh1.Parent.NameWorkbookobject reference returned byWorkbooks.Open, somewhere down the line.