简体   繁体   中英

use worksheet name stored in a cell to call a cell from that worksheet

I need to pull data from a cell on another worksheet but I don't know what the name of the worksheet will be. However, the name of the worksheet will be stored in one of the cells on the main sheet when its generated.

A11 = myNewWorksheet

I need cell G3 from myNewWorksheet that was just created by a macro. The worksheet name is stored in cell A11 on the main worksheet.

I'm not sure how to call the new worksheet name from cell A11 to populate the data from cell G3 in the newly created worksheet.

='$A11'!G3

Can anyone help me with this?

Thanks in advance

Set the workbook with your in-cell value as a string. You could do it in 1 or 2 lines, but this is easier to follow.

Sub IdentifyWorksheet()
    Dim WorksheetName As String
    Dim TargetWorksheet As Worksheet
    WorksheetName = ActiveSheet.Range("A11").Value
    Set TargetWorksheet = Worksheets(WorksheetName)
End Sub

试试下面的代码

Debug.Print Worksheets(Worksheets("Main").Range("A11").Value).Range("G3").Value

You will want to use the INDIRECT function

=INDIRECT(A11&"!G3")

INDIRECT is passed a string which it converts to a range reference. In this case we pass it a string consisting of the value in cell A11 concanetated (&) with the remainder of the address which is "!G3"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM