我得到運行時錯誤91「對象變量或塊變量未設置」與這行代碼。困擾我的是有時候它運行的很好,而其他的它只是給了我錯誤。當我搜尋那個錯誤時,一切似乎都已到位。我試圖刪除重複標題和由零填充的行的行。任何人都可以用它找到問題嗎?嘗試通過vba刪除標題,但得到運行時錯誤91
Sub RemoveHeaders()
Const HdrTextOne As String = "*Station*"
Const HdrTextTwo As String = "*Export File For Future Analysis*"
Const HdrTextThree As String = "*0*"
Const HdrKeepRowOne As Long = 3
Const HdrKeepRowTwo As Long = 1
Const HdrKeepRowThree As Long = 19
Dim c As Range
Dim lr As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Data")
Application.ScreenUpdating = False
lr = Range("B" & Rows.Count).End(xlUp).Row
With Range("B" & HdrKeepRowOne & ":B" & lr)
Set c = .Find(HdrTextOne, LookIn:=xlValues, SearchDirection:=xlNext)
If Not c Is Nothing And c.Row <> HdrKeepRowOne Then
Do
c.Resize(5).EntireRow.Delete
Set c = .Find(HdrTextOne, LookIn:=xlValues, SearchDirection:=xlNext)
Loop While Not c Is Nothing And c.Row <> HdrKeepRowOne
End If
End With
lr = Range("B" & Rows.Count).End(xlUp).Row
With Range("B" & HdrKeepRowTwo & ":B" & lr)
Set c = .Find(HdrTextTwo, LookIn:=xlValues, SearchDirection:=xlNext)
If Not c Is Nothing And c.Row <> HdrKeepRowTwo Then
Do
c.Resize(5).EntireRow.Delete
Set c = .Find(HdrTextTwo, LookIn:=xlValues, SearchDirection:=xlNext)
Loop While Not c Is Nothing And c.Row <> HdrKeepRowTwo
End If
End With
lr = Range("D" & Rows.Count).End(xlUp).Row
With Range("D" & HdrKeepRowThree & ":D" & lr)
Set c = .Find(HdrTextThree, LookIn:=xlValues, SearchDirection:=xlNext)
If Not c Is Nothing And c.Row <> HdrKeepRowThree Then
Do
c.Resize(5).EntireRow.Delete
Set c = .Find(HdrTextThree, LookIn:=xlValues, SearchDirection:=xlNext)
Loop While Not c Is Nothing And c.Row <> HdrKeepRowThree
End If
End With
ws.Columns("C:C").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Application.ScreenUpdating = True
末次
嘗試在突出顯示的「If」子句後面設置「Set c = Nothing」。 – Ralph
@newguy發佈代碼 – Djhatche