
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