Excel VBA – 使用数组来查找和replacestring

我正在使用数组来查找某些string,并用更标准的值replace它们:

Dim Keywords As Variant Dim MachineNames As Variant Keywords = Array("SAM ", "Press ", "Robot", "Robot 1", "Robot 2", "Robot 3", "Robot 4", "Robot 5", "Robot 6", "FA ", "FA 1", "FA1", "FA 2", "FA2", "FA 3", "FA3", "FA 4", "FA4", "FA 5", "FA5", "FA 6", "FA6", "FA 7", "FA7", "FA 8", "FA8", "FA 9", "FA9", "FA 10", "FA10", "FA 11", "FA11", "FA 12", "FA12", "St 120", "St 95", "St 90C", "Flex Arc", "Flex Arch", "Hammond", "Acme", "Polish", "Tank", "Fender", "Welder", "Balance", "PICO", "Gravity", "Vin Mark", "Vin Stamp", "Telesis", "Pinstamp", "Pin stamp", "Buff", "Wet", "E-Coat", "E Coat", "Ecoat", "Carrier", "Line", "Line 1", "Line1", "Line 2", "Line2", "Line 3", "Line3", "Line 4", "Line4", "Line 5", "Line5", "Line 6", "Line6", "St 100", "St 30", "St 150", "Laser", "Laser 1", "Laser1", "Laser 2", "Laser2", "Laser 3", "Laser3", "Laser 4", "Laser4", "Laser 5", "Laser5", "Laser 6", "Laser6", "Laser Seamer", "Laser Seam", "Laser Seemer", _ "Vin Laser", "Monode", "Sub", "Tip", "Tip Change", "Swingarm Press", "Swing arm Press", "Bearing Press", "Medallion Press", "Footboard Press", "AIDA", "Cushion", "Press 1", "Press1", "Press 2", "Press2", "Press 3", "Press3", "Press 4", "Press4") MachineNames = Array("SAM", "Press", "Robot", "Robot 1", "Robot 2", "Robot 3", "Robot 4", "Robot 5", "Robot 6", "FA", "FA 1", "FA 1", "FA 2", "FA 2", "FA 3", "FA 3", "FA 4", "FA 4", "FA 5", "FA 5", "FA 6", "FA 6", "FA 7", "FA 7", "FA 8", "FA 8", "FA 9", "FA 9", "FA 10", "FA 10", "FA 11", "FA 11", "FA 12", "FA 12", "FA 4", "FA 3", "FA 10", "FA", "FA", "Polish (Hammond)", "Polish (Acme)", "Polish", "Tank", "Fender", "Welder", "Balance", "PICO", "Gravity", "Vin Stamp", "Vin Stamp", "Pinstamp", "Pinstamp", "Pinstamp", "Buff", "Wet", "E-Coat", "E-Coat", "E-Coat", "Carrier", "Line", "Line 1", "Line 1", "Line 2", "Line 2", "Line 3", "Line 3", "Line 4", "Line 4", "Line 5", "Line 5", "Line 6", "Line 6", "Laser", "Laser", "Laser", "Laser", "Laser 1", "Laser 1", "Laser 2", "Laser 2", "Laser 3", "Laser 3", "Laser 4", "Laser 4", "Laser 5", "Laser 5", "Laser 6", "Laser 6", "Laser (Seamer)", "Laser (Seamer)", "Laser (Seamer)", _ "Laser (Vin)", "Laser (Monode)", "Sub", "Tip", "Tip", "Press (Swingarm)", "Press (Swingarm)", "Press (Bearing)", "Press (Medallion)", "Press (Footboard)", "Press (AIDA)", "Press", "Press 1", "Press 1", "Press 2", "Press 2", "Press 3", "Press 3", "Press 4", "Press 4") Range("A2").Activate Do Until ActiveCell.Offset(0, 10) <> "" 'there are cells filled at the bottom of the sheet already. 'There are 49 array items, so 0 - 48 For i = 0 To 48 Set C = ActiveCell.Find(Keywords(i), LookIn:=xlValues) If Not C Is Nothing Then ActiveCell.Offset(0, 10).Value = MachineNames(i) Next i ActiveCell.Offset(1, 0).Activate Loop 

我有这个问题,有时候会find价值,有时候却不是。 我意识到,在数组中的项目的顺序可以导致这一点。 例如:A2包含“Press”B2包含“Line”(*注意,两个单元格都不包含这两个string。)

当“Press”位于数组列表的后面时,它会打印“Other Machines”,但是当它位于数组列表的前面时,它会打印“Press”,但是之后用于打印“Line”的位置打印“其他机器”。

任何帮助表示赞赏!

如果你决定使用VBA这将是一个更有效的版本 – 你不需要激活单元格


 Option Explicit Public Sub UpdateMachineNames() Dim keywords As Variant Dim machineNames As Variant Dim k As Long keywords = Array("SAM ", "Press ", "Robot", "Robot 1", "Robot 2", "Robot 3", "Robot 4", "Robot 5", "Robot 6", "FA ", "FA 1", "FA1", "FA 2", "FA2", "FA 3", "FA3", "FA 4", "FA4", "FA 5", "FA5", "FA 6", "FA6", "FA 7", "FA7", "FA 8", "FA8", "FA 9", "FA9", "FA 10", "FA10", "FA 11", "FA11", "FA 12", "FA12", "St 120", "St 95", "St 90C", "Flex Arc", "Flex Arch", "Hammond", "Acme", "Polish", "Tank", "Fender", "Welder", "Balance", "PICO", "Gravity", "Vin Mark", "Vin Stamp", "Telesis", "Pinstamp", "Pin stamp", "Buff", "Wet", "E-Coat", "E Coat", "Ecoat", "Carrier", "Line", "Line 1", "Line1", "Line 2", "Line2", "Line 3", "Line3", "Line 4", "Line4", "Line 5", "Line5", "Line 6", "Line6", "St 100", "St 30", "St 150", "Laser", "Laser 1", "Laser1", "Laser 2", "Laser2", "Laser 3", "Laser3", "Laser 4", "Laser4", "Laser 5", "Laser5", "Laser 6", "Laser6", "Laser Seamer", "Laser Seam", "Laser Seemer", _ "Vin Laser", "Monode", "Sub", "Tip", "Tip Change", "Swingarm Press", "Swing arm Press", "Bearing Press", "Medallion Press", "Footboard Press", "AIDA", "Cushion", "Press 1", "Press1", "Press 2", "Press2", "Press 3", "Press3", "Press 4", "Press4") machineNames = Array("SAM", "Press", "Robot", "Robot 1", "Robot 2", "Robot 3", "Robot 4", "Robot 5", "Robot 6", "FA", "FA 1", "FA 1", "FA 2", "FA 2", "FA 3", "FA 3", "FA 4", "FA 4", "FA 5", "FA 5", "FA 6", "FA 6", "FA 7", "FA 7", "FA 8", "FA 8", "FA 9", "FA 9", "FA 10", "FA 10", "FA 11", "FA 11", "FA 12", "FA 12", "FA 4", "FA 3", "FA 10", "FA", "FA", "Polish (Hammond)", "Polish (Acme)", "Polish", "Tank", "Fender", "Welder", "Balance", "PICO", "Gravity", "Vin Stamp", "Vin Stamp", "Pinstamp", "Pinstamp", "Pinstamp", "Buff", "Wet", "E-Coat", "E-Coat", "E-Coat", "Carrier", "Line", "Line 1", "Line 1", "Line 2", "Line 2", "Line 3", "Line 3", "Line 4", "Line 4", "Line 5", "Line 5", "Line 6", "Line 6", "Laser", "Laser", "Laser", "Laser", "Laser 1", "Laser 1", "Laser 2", "Laser 2", "Laser 3", "Laser 3", "Laser 4", "Laser 4", "Laser 5", "Laser 5", "Laser 6", "Laser 6", "Laser (Seamer)", "Laser (Seamer)", "Laser (Seamer)", _ "Laser (Vin)", "Laser (Monode)", "Sub", "Tip", "Tip", "Press (Swingarm)", "Press (Swingarm)", "Press (Bearing)", "Press (Medallion)", "Press (Footboard)", "Press (AIDA)", "Press", "Press 1", "Press 1", "Press 2", "Press 2", "Press 3", "Press 3", "Press 4", "Press 4") If UBound(keywords) <> UBound(machineNames) Then MsgBox "Invalid arrays" Exit Sub Else Application.ScreenUpdating = False With ActiveSheet.UsedRange.Columns("J") For k = 0 To UBound(keywords) .Replace What:=keywords(k), Replacement:=machineNames(k), LookAt:=xlPart, MatchCase:=False Next End With Application.ScreenUpdating = True End If End Sub 

VBA代码通知你,如果数组没有相同数量的项目,更容易维护,而不是更新所有单元格的公式,每次更改数组

另外,如果您有大量数据,则公式可以增加文件大小

dwirony看到我忘了更新我的i = 0 to 48 – > i = 0 to 109 。 我添加了更多的项目数组,但忘了更新这一块!

更新:更好的是, i = 0 to UBound(Keywords)使i = 0 to UBound(Keywords)

然而,我发现类似的东西可以通过一个公式来完成(对于那些不想用VBA工作,或者需要将其设置为单元格的人):

 =IFERROR(INDEX({"SAM","Robot","Robot 1","Robot 2","Robot 3","Robot 4","Robot 5","Robot 6","FA","FA 1","FA 1","FA 2","FA 2","FA 3","FA 3","FA 4","FA 4","FA 5","FA 5","FA 6","FA 6","FA 7","FA 7","FA 8","FA 8","FA 9","FA 9","FA 10","FA 10","FA 11","FA 11","FA 12","FA 12","FA 4","FA 3","FA 10","FA","FA","Polish (Hammond)","Polish (Acme)","Polish","Tank","Fender","Welder","Balance","PICO","Gravity","Vin Stamp","Vin Stamp","Pinstamp","Pinstamp","Pinstamp","Buff","Wet","E-Coat","E-Coat","E-Coat","Carrier","Line","Line 1","Line 1","Line 2","Line 2","Line 3","Line 3","Line 4","Line 4","Line 5","Line 5","Line 6","Line 6","Laser","Laser","Laser","Laser","Laser 1","Laser 1","Laser 2","Laser 2","Laser 3","Laser 3","Laser 4","Laser 4","Laser 5","Laser 5","Laser 6","Laser 6","Laser (Seamer)","Laser (Seamer)","Laser (Seamer)","Laser (Vin)","Laser (Monode)","Sub","Tip","Tip","Press (Swingarm)","Press (Swingarm)","Press (Bearing)","Press (Medallion)","Press (Footboard)","Press (AIDA)","Press","Press 1","Press 1","Press 2","Press 2","Press 3","Press 3","Press 4","Press 4","Press"}, MATCH(TRUE,ISNUMBER(SEARCH({"SAM ","Robot","Robot 1","Robot 2","Robot 3","Robot 4","Robot 5","Robot 6","FA ","FA 1","FA1","FA 2","FA2","FA 3","FA3","FA 4","FA4","FA 5","FA5","FA 6","FA6","FA 7","FA7","FA 8","FA8","FA 9","FA9","FA 10","FA10","FA 11","FA11","FA 12","FA12","St 120","St 95","St 90C","Flex Arc","Flex Arch","Hammond","Acme","Polish","Tank","Fender","Welder","Balance","PICO","Gravity","Vin Mark","Vin Stamp","Telesis","Pinstamp","Pin stamp","Buff","Wet","E-Coat","E Coat","Ecoat","Carrier","Line","Line 1","Line1","Line 2","Line2","Line 3","Line3","Line 4","Line4","Line 5","Line5","Line 6","Line6","St 100","St 30","St 150","Laser","Laser 1","Laser1","Laser 2","Laser2","Laser 3","Laser3","Laser 4","Laser4","Laser 5","Laser5","Laser 6","Laser6","Laser Seamer","Laser Seam","Laser Seemer","Vin Laser","Monode","Sub","Tip","Tip Change","Swingarm Press","Swing arm Press","Bearing Press","Medallion Press","Footboard Press","AIDA","Cushion","Press 1","Press1","Press 2","Press2","Press 3","Press3","Press 4","Press4","Press "}, A1)),0)),"Other Machines")