2017-01-23 36 views
0

我有一個代碼,用於將數據從一張紙複製到另一張使用if和vlookup功能但不工作Excel vba運行時錯誤438(如果Cl.Value像str和Rng2.Cl.Value <>「」Then)

Sub CopyRows() 
Dim Rng As Range 
Dim Cl As Range 
Dim str As String 
Dim RowUpdCrnt As Long 

Set UsedRange = Sheets("Jan").Range("b5:Am81") 
Set Rng = Sheets("Jan").UsedRange 'the range to search ie the used range 
Set Rng2 = Sheets("Feb").Range("I5:AK5") 
str = "WRK." 'string to look for 
Sheets("Feb").Range("B5:B81").Value = "" 

RowUpdCrnt = 5 

' In my test data, the "WRK."s are in column AN. This For-Each only selects column AN. 
' I assume all my "WRK."s are in a single column. Replace "B" by the appropriate 
' column letter for your data. 

With Sheets("Jan") 
' loop until last row with data in Column AN (and not the entire column) to save time 
    For Each Cl In .Range("AN1:AN" & .Cells(.Rows.Count, "AN").End(xlUp).Row) 
    If Cl.Value Like str And Rng2.Cl.Value <> "" Then 
    'if the cell contains the correct value copy it to next empty row on sheet 2 & delete the row 
     If Not IsError(Application.Vlookup(.Range("B" & Cl.Row).Value, Sheets("Master").Range("H7:H200"), 1, 0)) Then ' <-- verify the VLookup was successful 
     Sheets("Feb").Range("B" & RowUpdCrnt).Value = Application.Vlookup(.Range("B" & Cl.Row).Value, Sheets("Master").Range("H7:H200"), 1, 0) 
     RowUpdCrnt = RowUpdCrnt + 1  
     End If 
    End If 
    Next Cl 
End With 

Application.CutCopyMode = False 
End Sub 
+2

這裏不工作:'Rng2.Cl.Value'。 'Cl'是表單('Jan')中的一個範圍(單元格)。您不能像這樣在不同的工作表上訪問該範圍變量。我認爲你想在那裏訪問同一個單元格地址?嘗試'如果Cl.Value像str和Rng2.Range(Cl.Address).Value <>「」Then',而不是。 – LocEngineer

+0

'表格(「Foobar」)'隱式指向活動工作簿。局部變量'UsedRange'和'Rng2'沒有聲明,'Rng'和'UsedRange'被分配但從未被引用。考慮在模塊頂部指定Option Explicit。您可能會發現我的[Rubberduck](http://www.rubberduckvba.com/)加載項也可用於識別其他問題 - 包括可能導致運行時錯誤438的情況。例如,如果我聲明'Rng2作爲範圍'我得到[此檢查結果](https://i.stack.imgur.com/6iCyj.png)(v2.0.12,即將發佈),這正是你在這裏遇到的問題。 –

+0

另一件事:使用''Like''而不是通配符就像比較'Equals'一樣工作。更好的是(猜測這裏的通配符屬於哪裏):'str =「WRK。*」' – LocEngineer

回答

0

你有很多未申報的當地人;給定您的代碼顯然運行,我認爲Option Explicit沒有指定。

這使得本地變量Rng2在運行時被現場聲明爲Variant/Range。這是偉大的,不同的是,好了,在設計時,你沒有得到智能感知,以確保你寫的代碼..嗯,有道理。就像這樣:

Rng2.Cl.Value 

Cl是本地Range對象變量,但你使用它,如果它是的Rng2成員 - 因爲你不聲明Rng2,VBA編譯愉快代碼,而不是在編譯時抱怨,在運行時發生錯誤438「對象不支持這個屬性或方法」。換句話說,Range.Cl不存在,和VBA不知道怎麼用它做什麼 - 作爲Rubberduck當前版本(V2.0.12,預定於發行二月初)(一個開放源代碼的VBE加載我積極參與了)可以告訴你:

Member 'Cl' is not declared on the interface for type 'Range'.

所以不是這樣的:

If Cl.Value Like str And Rng2.Cl.Value <> "" Then 

你可以嘗試LocEngineer's suggestion,這依賴於你想要做什麼:

If Cl.Value Like str And Rng2.Range(Cl.Address).Value <> "" Then 
1

我剛剛再次探討了這一點。 你在這一塊做的幾個誤區:

  1. UsedRange:

    Set UsedRange = Sheets("Jan").Range("b5:Am81")

    Set Rng = Sheets("Jan").UsedRange

這不就是我想你認爲它會。也考慮到你以後如何處理範圍,你不需要這些。

  • Set Rng2 = Sheets("Feb").Range("I5:AK5")
  • 如你不與該特定範圍做任何事情,它不需要任一。

    1. Rng2.Cl.Value是一個無效構造,並且Like "WRK."沒有我的評論中提到的通配符。

    在此基礎上我已經剝奪你的這些unneccessaries的代碼和糾正錯誤的位置是直接可見的:

    Option Explicit 
    
    Sub CopyRows() 
    
    Dim Cl As Range 
    Dim str As String 
    Dim RowUpdCrnt As Long 
    
    str = "WRK.*" 'string to look for 
    Sheets("Feb").Range("B5:B81").Value = "" 
    
    RowUpdCrnt = 5 
    
    ' In my test data, the "WRK."s are in column AN. This For-Each only selects column AN. 
    ' I assume all my "WRK."s are in a single column. Replace "B" by the appropriate 
    ' column letter for your data. 
    
    With Sheets("Jan") 
    ' loop until last row with data in Column AN (and not the entire column) to save time 
        For Each Cl In .Range("AN1:AN" & .Cells(.Rows.Count, "AN").End(xlUp).Row) 
        If Cl.Value Like str And Sheets("Feb").Range(Cl.Address).Value <> "" Then 
        'if the cell contains the correct value copy it to next empty row on sheet 2 & delete the row 
         If Not IsError(Application.Vlookup(.Range("B" & Cl.Row).Value, Sheets("Master").Range("H7:H200"), 1, 0)) Then ' <-- verify the VLookup was successful 
         Sheets("Feb").Range("B" & RowUpdCrnt).Value = Application.Vlookup(.Range("B" & Cl.Row).Value, Sheets("Master").Range("H7:H200"), 1, 0) 
         RowUpdCrnt = RowUpdCrnt + 1  
         End If 
        End If 
        Next Cl 
    End With 
    
    Application.CutCopyMode = False 
    End Sub 
    

    不能保證給,因爲我目前還沒有足夠的時間來重新創建一個假的的數據來嘗試和測試。應該工作。

    +0

    是的。 'UsedRange'在這裏是一個未定義的局部變量。 OP需要開始使用'Option Explicit'! –