尝试运行 sub 时出现错误“对象变量或未设置块变量”

Mic*_*ael 1 excel vba

我没有创建代码,但我正在尝试对 excel 文件进行故障排除,而原作者不在(被公司解雇并且不愿意提供帮助)。

以下行生成错误,“对象变量或未设置块变量”

Private Sub Workbook_Open()
Sheet1.Starttimer
End Sub
Run Code Online (Sandbox Code Playgroud)

我查看了 Sheet1 代码并找到了以下内容,所以我不确定问题是什么:

Sub Starttimer()
    Application.DisplayAlerts = False
    If Not Sheet4.ListObjects(1).DataBodyRange Is Nothing Then
        Sheet4.ListObjects(1).DataBodyRange.Rows.Delete
    End If
    ActiveWorkbook.RefreshAll
    Application.Calculate
    SetProductionZeros
    ActiveWorkbook.Save
    ThisWorkbook.Close
End Sub
Run Code Online (Sandbox Code Playgroud)

更新 将调试设置为在所有错误上中断后,导致错误的行似乎是下面子中的“r = Sheet4.ListObjects(1).DataBodyRange.Rows.Count”:

Sub SetProductionZeros()

Dim tb1 As ListObject
Dim x As Long
Dim y As Long
Dim r As Long
Dim c As Long

'Set path for Table variable'
  Set tb1 = Sheet4.ListObjects(1)

Sheet4.Activate
r = Sheet4.ListObjects(1).DataBodyRange.Rows.Count
c = Sheet4.ListObjects(1).DataBodyRange.Columns.Count

'Loop Through Each DataBody Row in Table

  For y = 1 To r
     'Loop Through Each Column in Table
    For x = 1 To c
      If IsEmpty(Sheet4.ListObjects(1).DataBodyRange.Cells(y, x)) Then Sheet4.ListObjects(1).DataBodyRange.Cells(y, x) = 0
    Next x
  Next y
  
  Sheet4.Columns(5).EntireColumn.Delete
  
    Dim lastrow As Long, lastcol As Long, thiscol As Long
    Dim totalrow As Long, totalcol As Long, thisrow As Long
    
        totalrow = 7 + Sheet4.ListObjects(1).Range.Rows.Count
        totalcol = 2 + Sheet4.ListObjects(1).Range.Columns.Count
 On Error GoTo Errorcatch
        'lastrow = Cells(Rows.Count, 1).End(xlUp).row
        'lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
          
         Sheet4.Cells(totalrow, 3).Value = "Total"
        For thiscol = 5 To totalcol - 1
           Sheet4.Cells(totalrow, thiscol).Select
           ActiveCell.Value = WorksheetFunction.Sum(Sheet4.Range(Sheet4.Cells(1, ActiveCell.Column), ActiveCell))
        Next
       Sheet4.Rows(totalrow).Font.Bold = True
       
       Sheet4.Cells(7, totalcol).Value = "Total"
        For thisrow = 8 To totalrow
           Sheet4.Cells(thisrow, totalcol).Select
            ActiveCell.Value = WorksheetFunction.Sum(Sheet4.Range(Sheet4.Cells(ActiveCell.row, 5), ActiveCell))
        Next
        Sheet4.Columns(totalcol).Font.Bold = True
        'Sheet4.Columns(2).HorizontalAlignment = xleft
        
        For y = totalrow To 8 Step -1
            If Sheet4.Cells(y, 2) = "T" And Sheet4.Cells(y, totalcol).Value = 0 Then
                Sheet4.Rows(y).EntireRow.Delete
            End If
        Next
        
       Exit Sub
       
Errorcatch:
MsgBox Err.Description
        
End Sub
Run Code Online (Sandbox Code Playgroud)

chr*_*sen 6

遵循以下逻辑:

  • 当您打开工作簿时,您调用 Sheet1.StartTimer

  • Sheet1.StartTimer 包括

      If Not Sheet4.ListObjects(1).DataBodyRange Is Nothing Then
          Sheet4.ListObjects(1).DataBodyRange.Rows.Delete
      End If
    
    Run Code Online (Sandbox Code Playgroud)
  • 此时Sheet4.ListObjects(1).DataBodyRange将是Nothing(因为您删除了它的所有行)

  • 然后你打电话 SetProductionZeros

  • SetProductionZeros 包括 r = Sheet4.ListObjects(1).DataBodyRange.Rows.Count

  • 但由于Sheet4.ListObjects(1).DataBodyRangeNothing,这将引发一个错误。(同样适用于.Columns.Count

您可以将引用包装DataBodyRange

If Not Sheet4.ListObjects(1).DataBodyRange Is Nothing Then
    ' ...
End If
Run Code Online (Sandbox Code Playgroud)

但是您需要考虑在没有行时要实现的目标 Sheet4.ListObjects(1)