0
我有一個關於我的代碼的問題不知何故我得到以下錯誤(我嘗試了一些不同的解決方案在網上,但不知何故它不能解決我的問題)。結束循環後(當一切在循環中工作時),我得到以下錯誤:運行時間錯誤'91'對象變量或未設置塊變量。我希望你們中的一位能幫助我!還有一個筆記,我得到的錯誤。VBA運行時錯誤91同時完成循環
代碼:
Public Function FilterButton() As Integer
Dim SrcSheet As Worksheet, ParSheet As Worksheet
Dim SourceRange As Range
Dim SrcCell As Range, DestCell As Range
Dim firstAddress As String
Dim iLastRow As Long, zLastRow As Long
Dim Collection As String, System As String, Tag As String
Dim iRowInWsPar As Long
Dim iError As Integer
Dim TagAndSystem As String, Value As String
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'~~> Set your sheet
Set SrcSheet = Sheets("Imported Data")
Set ParSheet = Sheets("Parameters")
'~~> Set your ranges
'~~> Find Last Row in Col A in the source sheet
iLastRow = SrcSheet.Range("A" & SrcSheet.Rows.Count).End(xlUp).Row
Set SourceRange = SrcSheet.Range("A2:A" & iLastRow)
'~~> Search values
Collection = Trim(Range("lblImportCollection").Value)
System = Trim(Range("lblImportSystem").Value)
Tag = Trim(Range("lblImportTag").Value)
TagAndSystem = System & Tag
With SourceRange
'~~> Match 1st Criteria ("Collection")
Set SrcCell = .Find(What:=Collection, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
'~~> If found
If Not SrcCell Is Nothing Then
firstAddress = SrcCell.Address
Do
'If match 2nd Criteria
If ((Len(Trim(System)) = 0) Or (UCase(SrcCell.Offset(, 1).Value) = UCase(System))) Then
'Match 3rd criteria
If ((Len(Trim(Tag)) = 0) Or (UCase(SrcCell.Offset(, 2).Value) = UCase(TagAndSystem))) Then
iRowInWsPar = FindCellfromWsPar(System, Tag)
Value = SrcCell.Offset(, 4).Value
'Found in the parameter worksheet
If iRowInWsPar <> -1 Then
iError = ChangeValueinWsPar(iRowInWsPar, Value)
End If
End If
End If
Set SrcCell = .FindNext(After:=SrcCell)
Loop While (Not SrcCell Is Nothing) And (SrcCell.Address <> firstAddress) 'here i get the error
End If
End With
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
FilterButton = 0
End Function
'This function will return the row (if found) of the "Parameters" worksheet
Public Function FindCellfromWsPar(sSystem As String, sTag As String) As Integer
Dim ParSheet As Worksheet
Dim ParRange As Range
Dim SrcCell As Range
Dim firstAddress As String
Dim iLastRow As Long
Set ParSheet = Sheets(mcsWorksheetParameters)
With ParSheet
iLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
Set ParRange = ParSheet.Range("A2:A" & iLastRow)
FindCellfromWsPar = -1
With ParRange
'~~> Find sSystem in the "System" column
Set SrcCell = .Find(What:=sSystem, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
'~~> If found
If Not SrcCell Is Nothing Then
firstAddress = SrcCell.Address
Do
'If match Tag
If (UCase(SrcCell.Offset(, 1).Value) = UCase(sTag)) Then
FindCellfromWsPar = SrcCell.Row
End If
Set SrcCell = .FindNext(After:=SrcCell)
Loop While (Not SrcCell Is Nothing) And (SrcCell.Address <> firstAddress)
End If
End With
End Function
Public Function ChangeValueinWsPar(iRow As Long, sValue As String)
Dim ParSheet As Worksheet
Dim sValCol As String
sValCol = "G"
Set ParSheet = Sheets(mcsWorksheetParameters)
ParSheet.Range(sValCol & CStr(iRow)).Value = sValue
End Function
嗯,這聽起來合乎邏輯,你建議作爲一種解決方案? – user2868444
檢查編輯,如果這對您有意義。 – Trace
我現在正在調試,我會檢查我是否得到正確的解決方案 – user2868444