2017-04-06 185 views
1

我有以下代码:Excel的VBA“对象变量或带块变量未设置”

Sub CheckDates() 

bAlarm = False 

i = 1 
Do Until ActiveSheet.Cells(row_header, i).Value = "" 
    If ActiveSheet.Cells(row_header, i).Value = searchText Then 
     col = i 
     Exit Do 
    End If 
i = i + 1 
Loop 

i = 1 
Do Until ActiveSheet.Cells(row_header, i).Value = "" 
    If ActiveSheet.Cells(row_header, i).Value = searchNameText Then 
     col_name = i 
     Exit Do 
    End If 
i = i + 1 
Loop 

If col = 0 Then 
    MsgBox searchText & " basliklar arasinda bulunamadi" 
    Exit Sub 
Else 
    N = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row 
    For i = row_header + 1 To N 
     If isInDateRange(ActiveSheet.Cells(i, col).Value) Then 
      With UserForm1.ListBox1 
       .AddItem 
       .List(.ListCount - 1, 0) = ActiveSheet.Cells(i, col_name).Value 
       .List(.ListCount - 1, 1) = ActiveSheet.Cells(i, col).Value 
      End With 
      bAlarm = True 
     End If 
    Next i 
End If 

If bAlarm = True Then 
    UserForm1.Show 
End If 

我收到就行了Object variable or With block variable not set (Error 91)误差Do Until ActiveSheet.Cells(row_header, i).Value = ""。当我调试时,我可以看到hprlink.Range确实有一个值。任何想法我做错了什么?

回答

1

除非有代码从你的问题遗漏我觉得这个问题确实是:

Do Until ActiveSheet.Cells(row_header, i).Value = "" 

据我所知,row_header在这一点空的,因此可能会被解析为:

Do Until ActiveSheet.Cells(0, 1).Value = "" 

没有第0行,所以你得到你的错误。

在开始循环定位字段之前,请将值设为row_header(也许1 - 我看不到您的工作表?)。

此外,你可能想看看WorksheetFunction.Match,因为它会做你的第一个两个循环只用一条线做同样的事情。

最后,您可能应该在整个子版本中始终停止提及ActiveSheet,可以在开始时使用With块或Set对象作为参考。

相关问题