简体   繁体   English

具有.xlsm文件但未包含.xlsx的VBA运行时错误91

[英]VBA runtime error 91 with .xlsm file but not .xlsx

My program works as intended with non macro excel files but for whatever reason when I try to use it with macro workbooks it gives me run time error 91. The variable doesn't look like it loads anything into the Fname when it is a macro book. 我的程序按预期与非宏excel文件一起工作,但是由于任何原因,当我尝试将其与宏工作簿一起使用时,它会给我运行时错误91。当它是宏本时,该变量看起来不像是将任何内容加载到Fname中。 I suspect this may be because the company settings are for the user to enable macros on use. 我怀疑这可能是因为公司设置供用户在使用时启用宏。 This snippet seems to fail to load the file path for macro books. 此代码片段似乎无法加载宏书的文件路径。

Fname = Application.GetOpenFilename( _
    FileFilter:="XLS Files (*.xls),*.xls,XLSM Files (*.xlsm),*.xlsm", _
    Title:="Select a workbook", _
    MultiSelect:=False)

SheetName = Application.InputBox("Please enter the name of the output tab", , , , , , , 2)

pulled = sh1.Range("B1:B30")
WSoutputs = sh1.Range("B1:B30")

sh4.Range("J1:J28").ClearContents
Set sh2 = Workbooks.Open(Fname)

Is there a way to read the data from a macrobook without needing to enable it? 有没有一种方法可以从宏簿读取数据而无需启用它? If not how would I prompt the user to open the file like normal and hit enable macros? 如果没有,我将如何提示用户像正常一样打开文件并点击启用宏?

Full Code: 完整代码:

Option Explicit

Sub Pull()
Dim sh1 As Worksheet
Dim sh2 As Workbook
Dim sh3 As Worksheet
Dim sh4 As Worksheet
Dim Path As String
Dim i As Integer
Dim arg As String
Dim pulled
Dim WSoutputs
Dim Comp(30, 0) As Variant
Dim Fname As Variant
Dim SheetName

Set sh1 = ThisWorkbook.Sheets("Refs")
Set sh3 = ThisWorkbook.Sheets("Output")
Set sh4 = ThisWorkbook.Sheets("Inputs")

On Error GoTo Err:

Fname = Application.GetOpenFilename( _
    FileFilter:="XLS Files (*.xls),*.xls,XLSM Files (*.xlsm),*.xlsm,XLSX Files (*.xlsx),*.xlsx", _
    Title:="Select a workbook", _
    MultiSelect:=False)

SheetName = Application.InputBox("Please enter the name of the output tab", , , , , , , 2)

pulled = sh1.Range("B1:B30")
WSoutputs = sh1.Range("B1:B30")

sh4.Range("J1:J28").ClearContents
Set sh2 = Workbooks.Open(Fname)



For i = (LBound(pulled) + 2) To UBound(pulled)
    pulled(i, 1) = Trim(pulled(i, 1))
    pulled(i, 1) = sh2.Sheets(SheetName).Range(pulled(i, 1))
    WSoutputs(i, 1) = sh3.Range(WSoutputs(i, 1))
    If pulled(i, 1) = WSoutputs(i, 1) Then
        Comp(i - 3, 0) = "Match"
    Else
        Comp(i - 3, 0) = pulled(i, 1)
    End If
Next i
sh2.Close

sh4.Range("J1:J28") = Comp

Exit Sub

Err:
MsgBox ("Make sure the Workbook and tab names are correct. Make sure the output format matches this workbook")
sh4.Range("J1:J28").ClearContents
If Fname <> "False.xlsx" Or Fname <> "False.xlsm" Then
    sh2.Close
End If
End Sub

The loop doesn't seem to do what I want as somewhere in it Sh2 becomes empty. 循环似乎没有执行我想要的操作,因为其中Sh2变为空白。 Also, pulled and Wsoutput contain cell references (ex B3) and the input and output format are the same between worksheets. 此外,pull和Wsoutput包含单元格引用(例如B3),并且工作表之间的输入和输出格式相同。

I think this, but you should post as much code as possible. 我认为,但是您应该发布尽可能多的代码。 I do not know how the following are defined. 我不知道以下定义。 Try this anyhow. 不管怎样尝试。

Set pulled = sh1.Range("B1:B30")
Set WSoutputs = sh1.Range("B1:B30")

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

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