简体   繁体   English

从另一个具有动态文件路径位置的工作簿中运行excel宏

[英]Running excel macro from another workbook with dynamic File path location

So I have a macro in Worksheet1 that opens another file (Worksheet2) and then copy and pastes specific cell and transfer from Worksheet1 to Worksheet2. 因此,我在Worksheet1中有一个宏,它可以打开另一个文件(Worksheet2),然后复制并粘贴特定的单元格并将其从Worksheet1传输到Worksheet2。

Once Worksheet1's macro is done copy pasting the cells from Worksheet1 to Worksheet2, the same macro from Worksheet1 will trigger another macro (which embedded in Worksheet2) to run. 一旦完成工作表1的宏,将单元格从工作表1复制粘贴到工作表2,来自工作表1的相同宏将触发另一个宏(嵌入在工作表2中)运行。

I have the codes working from opening Worksheet2, copy and pasting data from worksheet1 to worksheet2, the only problem is the macro in Worksheet1 to trigger to run the macro embedded in Worksheet2. 我有从打开Worksheet2到从工作表1复制数据并将其粘贴到工作表2的代码,唯一的问题是工作表1中的宏触发运行在工作表2中嵌入的宏。

Sub FCY()
'
' Macro3 Macro
'

    Dim MainFile As Workbook
    Dim jnl As Workbook
    Dim RF As Workbook
    Dim FileExt As String

    Set MainFile = ActiveWorkbook

    'FileExt - Cells where is Worksheet2 being saved (example: Worksheet2.xls)
    FileExt = Sheets("REF Data").Cells(25, 7).Value & "." & Cells(25, 9).Value

    'Set jnl - a command to open the Worksheet2 by using the "FileExt" plus the File Location
    'which is entered in Cells (22,7) or Range("G22") (example - C:\Documents\Worksheet2.xls)
    Set jnl = Workbooks.Open(Worksheets("REF Data").Cells(22, 7).Value & "\" & FileExt)

    'Selecting the specified cells to copy and paste from Worksheet1 to Worksheet2
    MainFile.Activate
    Sheets("FCY").Select
    Range("B11:BJ11").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    jnl.Activate
    Range("B11:BJ11").Select
    ActiveCell.PasteSpecial xlPasteValues


    'Command which runs another Macro embedded in Worksheet2
    'Incorrect code to run the macro in Worksheet2
    Application.Run (Worksheets("REF Data").Cells(22, 7).Value & "\" & FileExt & SaveAstab)

'
End Sub

I already know that this can be achieve using this code: 我已经知道可以使用以下代码来实现:

Application.Run ("'C:\Documents\Worksheet2.xls'!SaveAstab") 

Note: SaveAstab is the name of the macro in Worksheet2 注意: SaveAstab是Worksheet2中宏的名称。

But if you noticed above the File location + filename + extension name is entered on specific cells by the users. 但是,如果您在上面注意到用户在特定单元格上输入了文件位置+文件名+扩展名。 I mean, the details of File location + filename + extension name is dynamic and entered by the users. 我的意思是,文件位置+文件名+扩展名的详细信息是动态的,由用户输入。

My problem is if I call the cells that I assigned for the file location + filename + extension name, I'm having an error (Run Time error 9). 我的问题是,如果我调用为文件位置+文件名+扩展名分配的单元格,则出现错误(运行时错误9)。

‘Incorrect code to run the macro in Worksheet2
Application.Run (Worksheets("REF Data").Cells(22, 7).Value & "\" & FileExt & SaveAstab)

Could anyone share their expertise on this matter? 谁能在这件事上分享自己的专业知识? Thank you very much 非常感谢你

Try that... 试试看...

Dim runCommand as String
runCommand = Chr(39) & Worksheets("REF Data").Cells(22, 7).Value & "\" & FileExt & Chr(39) & "!SaveAstab" 
Debug.print runCommand 
Application.Run(runCommand)

And give me te Output uf debug.print 并给我输出uf debug.print

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM