[英]VBA runtime error 91 with .xlsm file but not .xlsx
我的程序按預期與非宏excel文件一起工作,但是由於任何原因,當我嘗試將其與宏工作簿一起使用時,它會給我運行時錯誤91。當它是宏本時,該變量看起來不像是將任何內容加載到Fname中。 我懷疑這可能是因為公司設置供用戶在使用時啟用宏。 此代碼片段似乎無法加載宏書的文件路徑。
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)
有沒有一種方法可以從宏簿讀取數據而無需啟用它? 如果沒有,我將如何提示用戶像正常一樣打開文件並點擊啟用宏?
完整代碼:
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
循環似乎沒有執行我想要的操作,因為其中Sh2變為空白。 此外,pull和Wsoutput包含單元格引用(例如B3),並且工作表之間的輸入和輸出格式相同。
我認為,但是您應該發布盡可能多的代碼。 我不知道以下定義。 不管怎樣嘗試。
Set pulled = sh1.Range("B1:B30")
Set WSoutputs = sh1.Range("B1:B30")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.