【问题标题】:How to call a VBA script in a VBA script which is situated in other folder如何在位于其他文件夹中的 VBA 脚本中调用 VBA 脚本
【发布时间】:2015-12-09 22:13:43
【问题描述】:

我有一个名为“Summary_logic”的 Excel 工作表,它打开一个文件夹列表,每个文件夹都有一个 Excel 工作表,其中包含 VBA 脚本。我想调用每个文件夹中的 VBA 脚本(processR1)并将结果复制到 Summary_logic 表中。

'My code is given below.

Private Const test_pvt_1_name As String = "TestPVT_Result_template"
Private Const pvt_1_range As String = "G7:V1000"
Private Const pvt_1_range_testname As String = "C7:C1000" 

Sub CleanTable()
    clear_summary
    delete_auxiliary_sheets
    Sheets("Summary_logic").Select
End Sub

' Deletes all the values in the specified range
Private Sub clear_summary()
    Sheets("Summary_logic").Select
    'PVT_R01, Sensitivity Range:
    Range(pvt_1_range).Select
    Selection.ClearContents
    Range(pvt_1_range_testname).Select
    Selection.ClearContents
    MsgBox ("Clear_Summary Executed")
End Sub

' Delete all sheets except "Summary"
Private Sub delete_auxiliary_sheets()

   For Each wsht In Worksheets
        If wsht.Name <> "Summary_logic" Then
            Application.DisplayAlerts = False
            Sheets(wsht.Name).Delete
            Application.DisplayAlerts = True
        End If
   Next
   MsgBox ("delete_auxiliary_sheets Executed")
End Sub


Private Sub open_files()
Dim MyFolder As String
Dim MyFile As String: MyFile = "TestPVT_Result_template" & ".xlsm"
Dim i As Integer
i = 1
MsgBox (" Don't forget to enter the path of the PVT folders here")
MyFolder = "C:\Users\venkatav\Desktop\vba practice\new code\PVT_2015_10_20"     & Application.PathSeparator & Sheets("PVT_test_names1").Range("A1").Cells(i,     1).Value
MsgBox (" MyFolder is :") & MyFolder            'returns the folder name
MyFile = Dir(MyFolder & Application.PathSeparator & MyFile)
MsgBox (" MyFile is :") & MyFile                'returns the     TESTPVT_R1_out.csv

'Here I have to open the each .xlsm file and run the macro
Workbooks.Open Filename:=MyFolder & "\" & MyFile
MsgBox ("Opened the first file in the first folder")

'Now have to call the macro
MsgBox ("Calling the macro")

 'Here I have to call the macro in other excel file.. need help here
 Call processR1

'And then the results from the resultant process has to be copied to the summary_logic ranges

End Sub

'calls each macro from all the excel files and copies the data

【问题讨论】:

  • sheet named "Summary_logic" which opens a list of folders Sheets 无法打开文件夹,即使可以,也看不到完成上述操作的代码。简而言之,不明白你的问题/问题。

标签: vba excel call


【解决方案1】:

试试这条线:

Application.Run "'" & MyFolder & "\" & MyFile & "'!" & "processR1"

【讨论】:

    猜你喜欢
    • 2017-04-25
    • 2022-01-02
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 2010-11-06
    相关资源
    最近更新 更多