使用Excel中的宏在循环中查找和替换

时间:2017-03-29 04:03:26

标签: excel vba excel-vba

我有两张纸。表1包含以下数据

第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

任何提示或指导表示赞赏。

2 个答案:

答案 0 :(得分:2)

Cells.Replace会更改What Replacement所有次出现。

您需要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

答案 1 :(得分:1)

这应该有效。循环在sht1中搜索每个Name和City,并替换列#34; A"中的第一个匹配项。和" C" sht2:

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