在Excel中查找和替换多行

时间:2017-10-25 14:49:53

标签: excel excel-vba ms-office vba

我有一个产品长度为|的文档产品名称|和产品价格

我使用了我在网上找到的代码来替换产品价格:

{[error: string]:any}

我遇到的问题是这个VB脚本也取代了产品名称Acrylic的定价。

非常感谢任何帮助。如果需要,我还可以提供任何其他信息。

Doc的图片链接:http://prntscr.com/h1votx

1 个答案:

答案 0 :(得分:0)

你只有1张吗?此代码将与打印屏幕中的工作表一起使用,但是它会硬编码到值落入的当前列。如果您需要它来处理不同的值或不同的列,它可以放松一点。

Sub Multi_FindReplaceALL_pvc_replace_new()
'PURPOSE: Find & Replace a list of text/values throughout entire workbook
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault

Dim sht As Worksheet
Dim i As Integer

Dim lastRow As Integer

'get the last row in the sheet
With ActiveSheet
  lastRow = .Cells(.Rows.Count, "I").End(xlUp).Row
End With

'Loop through each row in the sheet
 For i = 1 To lastRow
'Loop through each worksheet in ActiveWorkbook
    If Cells(i, 9).Value = "2 ft" And Cells(i, 11).Value = "PVC" Then
        Cells(i, 20).Value = "290"
    End If
  Next i

End Sub

基本上,之前的代码只是在整个工作表中运行,如果它们在该数组中,则更改所有值。此函数逐行进行,并专门比较包含值

的列