2016-05-24 39 views
0

我試圖比較地址數據。我當前的宏比較兩列,並輸入「丟失的數據」時,他們不匹配。問題是大量的這些值沒有被丟棄,而是被集成到另一個單元中。我想更改我的宏,以便能夠使用VBA的類似運算符找到缺失的值。例如,它會在「9825 Spectrum Dr Bldg 3」中找到「Bldg 3」。我能夠從網上查看這些代碼,但我不確定Range("C65536")正在選擇的範圍。使用帶有通配符的運算符的VBA

編輯:我看到人們建議我使用Instr函數,它似乎做我想做的事情。我不知道如何讓它在我的宏中工作/讓它引用正確的單元格。它也(從我的理解)返回的值等於發現的字符數。因此,在我給出的例子中,如果包含空格,它將返回值6。

Sub droppeddata() 

Application.ScreenUpdating = False 

lr = Range("C65536").End(xlUp).Row 
For a = lr To 1 Step -1 
If Not IsEmpty(Cells(a, 13).Value) And IsEmpty(Cells(a, 19)) Then 
Cells(a, 10).Select 
Selection.NumberFormat = "General" 
    ActiveCell.FormulaR1C1 = "N" 
Cells(a, 11).Select 
Selection.NumberFormat = "General" 
    ActiveCell.FormulaR1C1 = "Dropped Data" 
End If 
Next a 

Application.ScreenUpdating = True 
End Sub 
+2

Range語句通過從C65536開始並向上跳轉來查找列C中最後一個使用的行(lr)。如果你想在一個單元格值中查找一部分字符串,你需要看'InStr'函數... – Dave

+2

如果你只是想檢查單元格是否包含某些字符,你可以使用instr http:// www。 exceltrick.com/formulas_macros/vba-instr-function/如果我正確理解了這個問題 –

+1

'InStr'告訴你你要查找的內容是否在字符串中(並且因此是名字InStr的起源) 。不要猜測你是否理解正確,你可以閱讀鏈接的頁面@Rob或閱讀函數的文檔。 –

回答

1

您當前的宏不會比較任何您想要的方式,它只檢查兩列是否爲空。

您還沒有非常具體的與你正在嘗試做的,所以這段代碼由位的猜測工作的完成:

Sub droppeddata() 
    Dim lr As Long ' Declare the variable 
    lr = Range("C65536").End(xlUp).Row ' Set the variable 
    ' lr now contains the last used row in column C 

    Application.ScreenUpdating = False 

    For a = lr To 1 Step -1 
     If IsEmpty(Cells(a, 19)) Or InStr(1, Cells(a, 13).Value, Cells(a, 19).Value, vbTextCompare) > 0 Then 
     ' If Cells(a, 19) is empty OR 
     ' Search Cells(a, 13) for the value contained in Cells(a, 19) 
     ' If INSTR returns a match greater than 0, it means the string we're looking for is present 
     ' Enter the loop if either condition is true 

      ' In this section, avoiding SELECT is preferable. Working directly on the ranges is good. 
      With Cells(a, 10) 
       .NumberFormat = "General" 
       .Value = "N" 
      End With 

      With Cells(a, 11) 
       .NumberFormat = "General" 
       .Value = "Dropped Data" 
      End With 
     End If 
    Next a 

    Application.ScreenUpdating = True 
End Sub 

變化範圍/細胞您的需要 - 目前其中一些並不意味着工作,我只是根據您現有的代碼猜測。