如何在一列中查找值并替换不同列中的值

时间:2013-08-19 20:43:15

标签: excel vba excel-vba

我遇到了一个简单的代码错误1004,或Sub未定义,具体取决于我使用的代码。我需要帮助比较两个不同工作表中的数字,然后替换其中一个rowa中的值。 (EX。sheet1“A1”= 809565匹配Sheet2“A28”= 809565,然后我必须将当前字符串更改为“地面以上(I)”。) < / p>

Set dbsheet = ThisWorkbook.Sheets("Sheet1")
Set dbsheet_1 = ThisWorkbook.Sheets("Export_For_WMIS_Recon")

Col_Len = dbsheet.Cells(Rows.Count, 1).End(x1UP).Row
Col_Len_1 = dbsheet_1.Cells(Rows.Count, 1).End(x1UP).Row

For x = 1 To Col_Len

     For i = 1 To Col_Len_1
        Search_num = dbsheet.Cells(x, 1)
        Comp_num = dbsheet_1.Cells(i, 1)
        Comp_word = dbsheet_1.Cells(i, 3)
        If Search_# = Comp_# And Comp_word = "Aboveground" Then
            Comp_word = "ABOVE GROUND(I)"
        End If
     Next i
Next x
End Sub

代码2:

row_number = 0
r_number_2 = 0

Do
DoEvent
r_number_2 = r_number_2 + 1
Search_# = ThisWorkbook.Sheets("Sheet1").Range("A" & row_number)
Comp_# = ThisWorkbook.Sheets("Export_For_WMIS_Recon").Range("A" & row_number)
    If Search_# = Comp_# And ThisWorkbook.Sheets("Export_For_WMIS_Recon").Range("C" & row_number) = "Aboveground" Then
        ThisWorkbook.Sheets("Export_For_WMIS_Recon").Range("C" & row_number) = "ABOVE GROUND(I)"
    End If
Loop Until Comp_# = ""

循环直到Search_#=“”

1 个答案:

答案 0 :(得分:0)

我认为这就是你要找的东西:

Sub tgr()

    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim rngFound As Range
    Dim varFind As Variant
    Dim strFirst As String

    Set ws1 = ThisWorkbook.Sheets("Sheet1")
    Set ws2 = ThisWorkbook.Sheets("Export_For_WMIS_Recon")

    For Each varFind In ws1.Range("A1", ws1.Cells(Rows.Count, "A").End(xlUp)).Value
        Set rngFound = ws2.Columns("A").Find(varFind, ws2.Cells(Rows.Count, "A"), xlValues, xlWhole)
        If Not rngFound Is Nothing Then
            strFirst = rngFound.Address
            Do
                If LCase(ws2.Cells(rngFound.Row, "C").Value) = "aboveground" Then
                    ws2.Cells(rngFound.Row, "C").Value = "ABOVE GROUND(I)"
                End If
                Set rngFound = ws2.Columns("A").Find(varFind, rngFound, xlValues, xlWhole)
            Loop While rngFound.Address <> strFirst
        End If
    Next varFind

    Set ws1 = Nothing
    Set ws2 = Nothing
    Set rngFound = Nothing

End Sub