2017-03-29 51 views
2

我有兩張表。片1包含下列數據使用Excel中的宏在循環中查找和替換

表1:

Column 1 column 2 
Hotel A New York 
Hotel B Melbourne 

我希望以該值,以取代在片材2的值

片2是這樣的:

Column 1 Column 2 Column 3 

Name  ....  ..... 
.....  ....  City 
....  ....  .... 
Name  ....  ..... 
....  .....  City 

我的理想輸出將是:

Column1  Column 2 Column 3 

Hotel A  ....  ..... 
.....  ....  New York 
....  ....  .... 
Hotel B  ....  ..... 
....  ....  Melbourne 

所以,我想經過一個循環,sheet 1和閱讀的名字和酒店的城市,去sheet 2和找到的話NameCity,並與我在sheet 1閱讀取代它們。我在VBA中非常新,並開始了我的代碼,它甚至進入循環。爲什麼這樣?

Sub testLoopPaste() 
    Dim j, k, L, b As String 
    Dim i As Long 
    Dim wb As Workbook 
    Dim sht1 As Worksheet 
    Dim sht2 As Worksheet 

    Set wb = ThisWorkbook 
    Set sht1 = wb.Sheets("Sheet1") 
    Set sht2 = wb.Sheets("Sheet2") 

    j = "Name" 
    b = "City" 

    For i = 1 To 2 

    k = sht1.Range("A" & i) 
    L = sht1.Range("B" & i) 

    sht2.Cells.Replace what:=j, replacement:=k, lookat:=xlWhole, MatchCase:=False 
    sht2.Cells.Replace what:=b, replacement:=L, lookat:=xlWhole, MatchCase:=False 

    Next i 

End Sub 

任何提示或指導表示讚賞。

+0

當'Name'是'Hotel A'或'Hotel B '? –

+0

@RobinMackenzie'sheet 2'中的第一個'Name'是'hotel A',因爲它是'Sheet 1'中的第一行,第二個'Name'是'hotel B',因爲它是'sheet 1中的第二行'。換句話說,「Sheet 2」中的「Name」的數量等於「sheet 1」中的行數。 – MFR

回答

2

Cells.Replace將改變全部發生WhatReplacement

你需要Find你正在尋找的單元格,然後就用這種細胞替換值:

Sub testLoopPaste() 
    'Note: existing code was declaring j, k and L as Variant 
    Dim j As String, k As String, L As String, b As String 
    Dim i As Long 
    Dim wb As Workbook 
    Dim sht1 As Worksheet 
    Dim sht2 As Worksheet 
    Dim found As Range 

    Set wb = ThisWorkbook 
    Set sht1 = wb.Sheets("Sheet1") 
    Set sht2 = wb.Sheets("Sheet2") 

    j = "Name" 
    b = "City" 

    For i = 1 To 2 
     ' always advisable to specify .Value rather than assuming it will be the default property  
     k = sht1.Range("A" & i).Value 
     L = sht1.Range("B" & i).Value 

     Set found = sht2.Cells.Find(What:=j, _ 
            LookIn:=xlValues, _ 
            LookAt:=xlWhole, _ 
            After:=sht2.Cells(sht2.Rows.Count, sht2.Cells.Count), _ 
            SearchOrder:=xlByRows, _ 
            SearchDirection:=xlNext, _ 
            MatchCase:=False, _ 
            SearchFormat:=False) 
     If Not found Is Nothing Then 
      found.Value = k 
     End If 

     Set found = sht2.Cells.Find(What:=b, _ 
            LookIn:=xlValues, _ 
            LookAt:=xlWhole, _ 
            After:=sht2.Cells(sht2.Rows.Count, sht2.Cells.Count), _ 
            SearchOrder:=xlByRows, _ 
            SearchDirection:=xlNext, _ 
            MatchCase:=False, _ 
            SearchFormat:=False) 
     If Not found Is Nothing Then 
      found.Value = L 
     End If 
    Next i 

End Sub 
+0

我測試了代碼。 A酒店和B酒店倒閉。這些城市是正確的。 – June7

+0

@ June7 - 我很擔心 - 我希望你會在第一排有標題,因此第一場比賽不會在A1格。給我幾分鐘,我會更新答案,開始在單元格(Rows.Count,Columns.Count)搜索。 – YowE3K

+0

@ June7 - 確定 - 我已經強制'Find'從表格的最後一個單元格開始。這樣,如果第一次匹配在單元格A1中,它仍然會找到它。 – YowE3K

1

這應該工作。循環在sht1中搜索每個Name和City,並替換sht2的列「A」和「C」中的第一個匹配項:

Sub testLoopPaste() 
    Dim i As Long 
    Dim wb As Workbook 
    Dim sht1 As Worksheet, sht2 As Worksheet 

    Set wb = ThisWorkbook 
    Set sht1 = wb.Sheets("Sheet1") 
    Set sht2 = wb.Sheets("Sheet2") 

    Dim Loc As Range 
    For i = 1 To sht1.Range("A" & sht1.Rows.Count).End(xlUp).row 
    Set Loc = sht2.Columns(1).Find(What:="Name") 
    If Not Loc Is Nothing Then Loc.Value = sht1.Cells(i, "A") 
    Set Loc = sht2.Columns(3).Find(What:="City") 
    If Not Loc Is Nothing Then Loc.Value = sht1.Cells(i, "B") 
    Next i 

End Sub