在Excel中查找和replace多行
我有一个产品长度的文档 产品名称| 和产品价格
我用我在网上find的这个代码replace产品价格:
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 fndList As Variant Dim rplcList As Variant Dim x As Long fndList = Array("2 ft", "PVC", "390") rplcList = Array("2 ft", "PVC", "290") 'Loop through each item in Array lists For x = LBound(fndList) To UBound(fndList) 'Loop through each worksheet in ActiveWorkbook For Each sht In ActiveWorkbook.Worksheets sht.Cells.Replace What:=fndList(x), Replacement:=rplcList(x), _ LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _ SearchFormat:=False, ReplaceFormat:=False Next sht Next x End Sub
我遇到的问题是这个VB脚本也取代了产品名称丙烯酸的定价。
任何帮助将不胜感激。 如果需要,我还可以提供任何其他信息。
文件的图像链接: http : //prntscr.com/h1votx
你只有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
基本上你以前的代码只是运行整个表格,如果它们在数组中,则更改所有的值。 此函数逐行进行比较,并专门比较包含您的值的列