2017-03-29 53 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