如果单元格包含部分字符串,则更改单元格值
题
我在一个专栏中有数百家公司的名单。有重复的公司被输入不同。(即"耐克"vs"耐克公司")
我正在尝试在excel中编程一个宏,以循环遍历列以搜索"Nike",如果找到它,它将替换该单元格中的内容到一致的公司名称(即"耐克公司。")的目标是对许多公司进行宏观搜索。
我感谢任何帮助!
解决方案
以下是 Nike 和 Ford
的示例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
这是一个非常基本的想法。那你就用 compArray
代替 ary
在@Gary的学生解决方案中。比较数组将包含您希望单元格在运行代码后读取的信息。所以,如果你想要"耐克公司。"对于"耐克"然后"耐克公司的所有实例。"会在你的配置中。
不隶属于 StackOverflow