Consulting

Results 1 to 4 of 4

Thread: Referencing Worksheets from Macros

  1. #1

    Referencing Worksheets from Macros

    In an Exel Macro how do you references worksheets other than the current active worksheet without changing the current worksheet?

  2. #2
    VBAX Newbie
    Joined
    Apr 2008
    Posts
    3
    Location
    Do you mean the old Excel Macros or VBA? If you mean VBA then you need to use the Sheets object so the following gets the value from cell A1 no matter which sheet you are in:

    Sub AccessSheet()

    ' The value in the brackets is referencing which sheet you are trying to access, either a number or
    ' the actual name of the sheet
    MsgBox ActiveWorkbook.Sheets(2).Range("A1").Value

    ' Accessing sheet by name
    MsgBox ActiveWorkbook.Sheets("Sheet2").Range("A1").Value

    End Sub

  3. #3
    VBAX Regular
    Joined
    Sep 2007
    Location
    Singapore
    Posts
    63
    Using a variable is another way:
    [vba]
    Sub ReferenceThat()
    Dim mySht As Worksheet
    Set mySht = Sheets("Sheet2")
    Set mySht = Sheets(2)
    Set mySht = Sheet2
    End Sub[/vba] By the way, all 3 lines above refer to the same sheet.

  4. #4
    Thanks for the Help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •