VBA用于比较多个列值,然后在多个工作表中进行替换

时间:2016-04-29 07:44:08

标签: excel vba excel-vba replace excel-2010

我需要比较E列和E列中的值。如果E& E,则工作簿的F超过400张。 F相等,用新文本替换F列。 我是excel的新手,我尝试过谷歌的许多不同方法。

以下(来自谷歌)适用于1张,但我需要它来完成所有纸张。

Sub findcomparereplace()
Dim wsh As Worksheet, i As Long, lngEndRowInv As Long
Set wsh = ActiveSheet

i = 2
lngEndRowInv = wsh.Range("E" & wsh.Rows.Count).End(xlUp).Row
While i <= lngEndRowInv
If Cells(i, "E") Like "*myvalue1*" And Cells(i, "F") Like "*myvalue1*" 
Then
Cells(i, "F").Value = "myvalue2"
End If
i = i + 1
Wend
End Sub

1 个答案:

答案 0 :(得分:0)

您可以像这样迭代表格:

Sub findcomparereplace()
    Dim wsh As Worksheet, i As Long, lngEndRowInv As Long

    For Each wsh In ActiveWorkbook.Worksheets
        i = 2
        lngEndRowInv = wsh.Range("E" & wsh.Rows.Count).End(xlUp).Row
        While i <= lngEndRowInv
            If wsh.Cells(i, "E") Like "*myvalue1*" And wsh.Cells(i, "F") Like "*myvalue1*" Then
                wsh.Cells(i, "F").Value = "myvalue2"
            End If
            i = i + 1
        Wend
    Next
End Sub