如果单元格包含部分string,则更改单元格值

我列出了数百家公司的名单。 有重复的公司input不同。 (即“耐克”与“耐克公司”)

我正在尝试在excel中编程一个macros来循环遍历列来search“耐克”,如果发现它将取代那个单元格中的内容,使其成为一致的公司名称(即“耐克公司”)。目标是有很多公司的macros观search。

我感谢任何帮助!

这里是耐克福特的例子

Sub Company() ary = Array("Nike", "Ford") Dim v As String, i As Long, j As Long For i = LBound(ary) To UBound(ary) v = ary(i) For j = 1 To Cells(Rows.Count, "A").End(xlUp).Row If InStr(1, Cells(j, "A"), v) > 0 Then Cells(j, "A") = v End If Next j Next i End Sub 

根据需要展开ary()

 dim configSht as WorkSheet, compArray() as String, x as Integer set configSht = thisWorkbook.Sheets("Config")'This identifies your config sheet redim compArray(0) for x = 1 to Cells(Rows.Count, "A").End(xlUp).Row if Cells(x,"A")<>"" then compArray(UBound(compArray) = Cells(Rows.Count, "A") End IF reDim Preserve compArray(UBound(compArray)+1) Next 

这是一个非常基本的想法。 那么你可以在@ Gary的Student解决scheme中使用compArray代替ary 。 比较数组将包含您希望单元格在运行代码后读取的信息。 所以如果你想要“耐克公司” 对于“耐克”,“耐克公司” 会在你的configuration。