本文介绍了尝试打开一个工作簿并在该文件中运行一个宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个工作簿打开另一个工作簿(文件名是基于单元格值),然后在该文件中运行一个名为Single_sector的宏。它打开文件完好无损,但不运行宏。任何想法?
Sub run_all()
Dim位置
在错误恢复下一个
'要打开的文件的位置
位置=工作表(主)。范围(folder_location)值
'Open F& V File
Application.Workbooks.Open Location&范围(fv_file)。值
'运行宏
运行(Single_sector)
End Sub
解决方案
将以下代码放在调用其他工作簿的宏中:
位置=工作表(主)。范围(folder_location)值
设置wb = Workbooks.Open(位置和范围(fv_file)。值)
Application.Run'& wb.Name& ! &安培; strSubToRun,参数
设置wb = Nothing
参数是一组您想要的参数通过,所以在另一个工作簿中的子类应该看起来像
Public Sub TheSub(ParamArray X())
Dim i As Long
Sheet1.Cells(1,1).Value =参数传递:
对于i = 0到UBound(X(0 )
Sheet1.Cells(i + 2,1).Value = CStr(X(i))
下一个
End Sub
I have a workbook which opens up another workbook (filename is based on a cell value) and then runs a macro called Single_sector within that file.
It opens the file perfectly fine but doesn't run the macro. Any ideas?
Sub run_all()
Dim Location
On Error Resume Next
'Location of file to open
Location = Worksheets("Main").Range("folder_location").Value
'Open F&V File
Application.Workbooks.Open Location & Range("fv_file").Value
'Run Macro
Run ("Single_sector")
End Sub
解决方案
Place the following code in the macro calling the other workbook:
Location = Worksheets("Main").Range("folder_location").Value
Set wb = Workbooks.Open(Location & Range("fv_file").Value)
Application.Run "'" & wb.Name & "'!" & strSubToRun, Parameters
Set wb = Nothing
Parameters is an array of arguments that you want to pass, so the sub in the other workbook should look something like
Public Sub TheSub(ParamArray X())
Dim i As Long
Sheet1.Cells(1, 1).Value = "Parameters passed:"
For i = 0 To UBound(X(0))
Sheet1.Cells(i + 2, 1).Value = CStr(X(i))
Next
End Sub
这篇关于尝试打开一个工作簿并在该文件中运行一个宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!