我有以下代码
Sub CleanCat()
Dim i As Integer
For i = 1 To 50
Columns("A").Replace What:="Cat" & i, Replacement:="Category" & i, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Columns("A").Replace What:="Cat " & i, Replacement:="Category" & i, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Columns("A").Replace What:="Category " & i, Replacement:="Category" & i, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Columns("A").Replace What:="Category" & i, Replacement:="Category" & i, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Columns("A").Replace What:="cat" & i, Replacement:="Category" & i, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Columns("A").Replace What:="cat " & i, Replacement:="Category" & i, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Columns("A").Replace What:="category " & i, Replacement:="Category" & i, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Columns("A").Replace What:="category" & i, Replacement:="Category" & i, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Next
End Sub
我想要的是循环遍历A列中的每个单元格并执行显示的替换(我正在通过推文循环),但这并不能取代所有内容。我收到了诸如something cat 13 here
左
示例推文:
@thisaccount I nominate @thataccountfor category 12 #somehashtag
Cat 12 I nominate @thisaccount #somehashtag
有什么想法吗?
答案 0 :(得分:2)
没有数字循环的另一个选项。 (注意:我建立在@ Jeeped的答案上)
它还将单词放在数组中以便于更新。
Sub CleanCat()
Dim i As Long
Dim srch() As Variant
Dim srchPart As Variant
srch = Array("Category ", "Category", "Cat ", "Cat") ' make sure this is in order longest to shortest.
With Worksheets("Sheet1")
.Columns("A") = .Evaluate("INDEX("'" & A:A,)")
.Columns("A").Replace What:=Chr(160), Replacement:=Chr(32), LookAt:=xlPart
For Each srchPart In srch
.Columns("A").Replace What:=srchPart, Replacement:="}}}}", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Next srchPart
.Columns("A").Replace What:="}}}}", Replacement:="Category", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End With
End Sub
我还偷了@ Jeeped的公式来测试:
答案 1 :(得分:1)
这就是你应该要求的全部。
Option Explicit
Sub CleanCat()
Dim i As Long
With Worksheets("sheet1")
.Columns("A").Replace What:=Chr(160), Replacement:=Chr(32), LookAt:=xlPart
For i = 50 To 1 Step -1
.Columns("A").Replace What:="Cat" & i, Replacement:="Category" & i, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
.Columns("A").Replace What:="Cat " & i, Replacement:="Category" & i, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
.Columns("A").Replace What:="Category " & i, Replacement:="Category" & i, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Next i
End With
End Sub
我的样本数据是用
创建的=CHOOSE(RANDBETWEEN(1, 3), "cat", "Cat", "category")&CHOOSE(RANDBETWEEN(1, 3), TEXT(,), CHAR(32), CHAR(160))&RANDBETWEEN(1, 50)