【问题标题】:Object variable or With block variable not set error occurring with Macro对象变量或块变量未设置宏发生错误
【发布时间】:2019-01-31 06:35:39
【问题描述】:

编辑我忘了提到错误发生在.activate 行。

我有一个简单的宏设置,可以用一些数据更新一个单独的文档。我希望宏在用户保存时自动激活,但每当我尝试使用宏时都会收到此错误“对象变量或未设置块变量”。

我在ThisWorkbook 模块中有代码。我已将代码放在一个新模块中,它工作正常,没有错误。仅当代码在 ThisWorkbook 模块中时才会出现该错误。

Option Explicit

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim wbMe, wbOut As Workbook

    Application.ScreenUpdating = False

    Set wbMe = ActiveWorkbook

    'Sets the destination for the data as well as automatically sending the data to the sheet that corresponds with the date inputted.
    Set wbOut = Workbooks.Open("/Users/MathieuKlein/Desktop/ValveStockMaster.xlsx")

    'This section deals with the actual process of copying the data and pasting it to the other excel file.
    With wbOut
        .Activate

        ' The following sets up the total of valves
        wbOut.Sheets("Stock").Range("D2:H20") = wbMe.Sheets("Macro  Data").Range("K104:O122").Value
        wbOut.Sheets("Stock").Range("D22:H37") = wbMe.Sheets("Macro Data").Range("K123:O138").Value
        wbOut.Sheets("Stock").Range("D39:H46") = wbMe.Sheets("Macro Data").Range("K139:O146").Value
        wbOut.Sheets("Stock").Range("D48:H50") = wbMe.Sheets("Macro Data").Range("K147:O149").Value
        wbOut.Sheets("Stock").Range("D52:H53") = wbMe.Sheets("Macro Data").Range("K150:O151").Value
        wbOut.Sheets("Stock").Range("D55:H58") = wbMe.Sheets("Macro Data").Range("K152:O155").Value
        wbOut.Sheets("Stock").Range("D61:H61") = wbMe.Sheets("Macro Data").Range("H14,H3,H5,H7,H9").Value
    End With

    With wbOut
        .Save
        .Close
    End With

    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub

我希望宏在用户保存文档时激活,但弹出此错误会阻止该操作。

【问题讨论】:

  • 似乎回想一下工作簿是否已经打开,然后尝试使用 workbooks.open 打开同一个工作簿不会返回对该工作簿的引用。当With 运行时,wbOut 是否设置为工作簿?
  • 我在这段代码中看不到任何会导致此错误的内容。我确实在Sheets("Macro Data")Sheets("Macro Data") 中看到了一个可能的错字,这可能会导致错误9 下标超出范围,但不是对象变量或未设置块变量。您可能正在运行其他一些导致此问题的代码。前往the exact line 发生的地方。
  • Range("H14,H3,H5,H7,H9") 联合未按该顺序传递值(如果有的话)。
  • 你不能在多区域范围内使用 .Value
  • 不相关但应该使用Set wbMe = ActiveWorkbook 而不是Set wbMe = ThisWorkbook

标签: excel vba


【解决方案1】:

请检查您的路径分隔符(斜杠或反斜杠):

Debug.Print "Application.PathSeparator: ", Application.PathSeparator

如果它已经打开,请尝试,否则尝试使用完整路径。

On Error Resume Next
Set wbOut = Workbooks("ValveStockMaster.xlsx") ' already open?
If Err.Number <> 0 Then
    On Error Goto 0
    ' use full path:
    Set wbOut = Workbooks.Open("C:\Users\MathieuKlein\Desktop\ValveStockMaster.xlsx")
    ' ...
End If
On Error Goto 0
' ...

请这样声明:

' instead of Dim wbMe, wbOut As Workbook
Dim wbMe As Workbook, wbOut As Workbook

如果它稍后没有放在某人的桌面上,请尝试与这些路径之一连接 e。 G。喜欢Application.DefaultFilePath &amp; "\Test.xlsx"

Debug.Print "ActiveWorkbook.Path: ", ActiveWorkbook.Path
Debug.Print "ThisWorkbook.Path: ", ThisWorkbook.Path
Debug.Print "Application.DefaultFilePath: ", Application.DefaultFilePath
Debug.Print "Application.RecentFiles(1).Path: ", Application.RecentFiles(1).Path
Debug.Print "Path: ", Application.Path
Debug.Print "LibraryPath: ", Application.LibraryPath
Debug.Print "StartupPath: ", Application.StartupPath
Debug.Print "AltStartupPath: ", Application.AltStartupPath 
Debug.Print "AutoRecover.Path: ", Application.AutoRecover.Path
Debug.Print "TemplatesPath: ", Application.TemplatesPath
Debug.Print "UserLibraryPath: ", Application.UserLibraryPath 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多