Results 1 to 2 of 2

Thread: refer to this workbook w/ variable ?

  1. #1

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    refer to this workbook w/ variable ?

    Workbooks("NLEAPGIS10.xls").Activate
    Workbooks("NLEAPGIS10.xls").Worksheets("nindex").Activate

    Instead of doing the above lines, is there a command which returns the name of the workbook instead of having to explicitly call the workbook name?

    something like a

    Workbooks(this.Workbook).activate ?

    thanks much

  2. #2
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: refer to this workbook w/ variable ?

    Quote Originally Posted by pgag45
    Workbooks("NLEAPGIS10.xls").Activate
    Workbooks("NLEAPGIS10.xls").Worksheets("nindex").Activate

    Instead of doing the above lines, is there a command which returns the name of the workbook instead of having to explicitly call the workbook name?
    It depends on where your code is on.
    I believe the above code is in Excel VBA.
    If you are writing the code within a module of the workbook "NLEAPGIS10.xls" then you can simply refer to it by using keyword ThisWorkbook.
    If the code is in another workbook, then you can declare a variable:
    Code:
    Dim wb As Workbook
    Dim ws As Worksheet
    
    Set wb = Workbooks("NLEAPGIS10.xls")
    Set ws = wb.Worksheets("nindex")
    
    '-- examples:
    ws.Range("A2") = wb.Path
    ws.Range("A3") = wb.Name
    ws.Range("A4") = ws.Name
    ws.Range("A3").Copy Destination:=ws.Range("C3")
    No need to Activate a workbook or worksheet unless the action requires that such as:
    Code:
    wb.Activate
    ws.Activate
    ws.Range("B10").Select
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

Posting Permissions

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



Click Here to Expand Forum to Full Width