2017-04-06 116 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對象作爲參考。

相關問題