简体   繁体   中英

How to open a worksheet using its defined variable

I am trying to open a worksheet that is a defined variable. I have a list of ID's and each of those ID's have their own worksheet where the name of the worksheet is the ID number. I am wanting to use the first ID value and assign it to a variable, then open the corresponding worksheet using that variable. Here is the current code I have. The error message is "Object Required".

    Dim proj As Range
    Dim Project As Range

    Set proj = Sheets("Project List").Range("B9")
    Set Project = proj.Value
    Sheets(Project).Activate

You are trying to put a value into an object. Also you should probably specify the workbook. Try instead:

Dim proj As Range
Dim Project As String

Set proj = ActiveWorkbook.Sheets("Project List").Range("B9")
Project = proj.Value
ActiveWorkbook.Sheets(Project).Activate

you just need to convert the range into a string, which is what the argument needs to be for Sheets()

Dim proj As Range

Set proj = Worksheets("Project List").Range("B9")
Sheets(proj.Text).Activate

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