How do you dynamically reference another sheet in Excel?
For example: In sheet1, I have a range that says "A1:B7".
I want to dynamically reference that range in sheet2 and run a macro. Something like this: Sheet1!A1:B7. But that doesn't work. Any ideas? You can't directly reference one cell from another, as the Excel Application object is not shared. You must use the Worksheets collection to access sheets from a given workbook.
For example, if you have Sheet1 and Sheet2 both in the same workbook, then you can get the worksheet of Sheet1 like this: Dim Sheet1 As Excel.Worksheet Set Sheet1 = ThisWorkbook.Worksheets("Sheet1") Then you can get Sheet2 like this: Dim Sheet2 As Excel.Worksheet Set Sheet2 = ThisWorkbook.Worksheets("Sheet2") If Sheet1.Range("A1:B7") contains some values then you can do the following: Dim cellValue As String. CellValue = Sheet1.Range("A1:B7") Value MsgBox cellValue. If you really need to reference cells that are not part of a specific sheet, you will have to get the entire worksheet using the Application object: Dim Sheet As Excel.Worksheet Set Sheet = ThisWorkbook.ActiveSheet If Sheet.Range("A1:B7")
Related Answers
How do I create a query in Excel Online?
In Excel Online, you can create queries using the new Excel Query...
How can I open a PDF file in Excel for free?
How to Convert PDF to Excel for Free. Convert PDF to Exce...
How do I run VBA code when workbook is closed?
Here is a summary of the code: Sub SaveData(). Dim w...