简体   繁体   中英

referencing Variable in VBA code for Excel is giving me the Run-Time error '9'

Fairly straight forward coding, but I am not used to VBA syntax, and I am really having hard time locating this simple question. so I came here to ask.

Sub sbCopyRangeToAnotherSheet()
    'get previous Sheet's name
    Set prevSheet = Worksheets("Template1").Range("E2")
    'Activate the destination workbook
    Workbooks("ScottMill Tennis Club Rank Calculation.xlsm").Activate
    'Activate worksheets
    ActiveWorkbook.Sheets(prevSheet).Activate 'This doesn't work
    ActiveWorkbook.Sheets("Template1").Activate 'But this does. what gives?
    'Copy the data
    Sheets(prevSheet).Range("B7:J30").Copy
    'Select the target range
    Range("B32").Select
    'Paste in the target destination
    ActiveSheet.Paste
    Application.CutCopyMode = False
End Sub

Sorry if you clicked into my dumb question. I just needed to set my variable to String to fix it.

Dim prevSheet As String

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