2013-10-14 114 views
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 

回答

0

我認爲你是因爲你問什麼代碼在這裏做了錯誤:

Loop While (Not SrcCell Is Nothing) And (SrcCell.Address <> firstAddress) 'here i get the error 

您是否SrcCell是什麼。但是爲了防止srcCell無效,它不能返回給你地址,因爲範圍沒有設置。

您可以通過在第一個條件中嵌套第二個條件來解決此問題。 這樣,你不能要求一個空對象的地址。

編輯:例如,您可以執行的操作是在第一個條件下循環,然後在下一個循環的開始處輸入第二個條件。

+0

嗯,這聽起來合乎邏輯,你建議作爲一種解決方案? – user2868444

+0

檢查編輯,如果這對您有意義。 – Trace

+0

我現在正在調試,我會檢查我是否得到正確的解決方案 – user2868444