简体   繁体   中英

Dynamic Destination Workbook for Excel VBA

I am trying to make a macro which Copies the Commission Calculation Tables into my Dental Commissions Report. The issue is that I have a fixed name "June Dental Commissions v5.xlsm" in the destination. Is there a way I could replace the "June Dental Commissions v5.xlsm" with a variable which I define as the current workbook in the macro?

Workbooks.Open Filename:= _
    "X:\Customer Service\Dental Reports\Commission Report Calculation\Commission Calculation Tables.xlsx"
Sheets(Array("Entire Commission Table", "Zip & Terr List May 2013", _
    "Abbreviation Finder")).Select
Sheets("Zip & Terr List May 2013").Activate
Sheets(Array("Entire Commission Table", "Zip & Terr List May 2013", _
    "Abbreviation Finder")).Copy Before:=Workbooks( _
    "June Dental Commissions v5.xlsm").Sheets(4)
Dim wb as Workbook, fName as string, fPath as string

fPath = "X:\Customer Service\Dental Reports\Commission Report Calculation\"
fName = "Commission Calculation Tables.xlsx"

Set wb = Workbooks.Open(fPath & fName)

wb.Sheets(Array("Entire Commission Table", "Zip & Terr List May 2013", _
    "Abbreviation Finder")).Copy Before:=ThisWorkbook.Sheets(4)

If your macro is not in the active file, use this adaptation of Tim's code

Dim wb as Workbook, fName as string, fPath as string, wbDest as workbook

fPath = "X:\Customer Service\Dental Reports\Commission Report Calculation\"
fName = "Commission Calculation Tables.xlsx"

set wbDest=activeworkbook
Set wb = Workbooks.Open(fPath & fName)

wb.Sheets(Array("Entire Commission Table", "Zip & Terr List May 2013", _
    "Abbreviation Finder")).Copy Before:=wbDest.Sheets(4)

wb.close(false)

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