2013-08-19 64 views
0

我被一個簡單的代碼錯誤1004或Sub未定義,具體取決於我使用的代碼。我需要幫助比較兩張不同工作表中的數字,然後在其中一個rowa中替換一個值。 (EX工作表Sheet1 「A1」= 809565場比賽Sheet2的 「A28」= 809565,然後我必須改變當前字符串爲 「地上(I)」。)如何在一個colunmn中查找值並在不同列中替換值

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_# = "" 

循環,直到搜索_#= 「」

+0

您是否在使用x one或x ell? – pnuts

+0

1,但我決定設置一個固定的範圍,因爲我知道牀單有多大。 – user2569708

回答

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 
相關問題