我在A列和B列中有数据。我需要一个宏来循环遍历这些列并删除最后的文本。列看起来像这样:
Column A Column B
1234(AZ) X1258Q156(ABCD)
3259(AZ) 81R58Q1V6(ABCD)
7849(AZ) X1X58-156(ABCD)
1489(AZ) XX258Q1(ABCD)
我需要宏来遍历A列和B列,并删除末尾的字符。在A列的示例中,从中删除所有(AZ),在B列的示例中,从中删除所有(ABCD)。文本长度将在A列和B列中有所不同。谢谢。
答案 0 :(得分:0)
您必须编写循环并将值插入范围,但这将在循环中执行您想要的操作:
Range().Value = Left(Range().Value, InStr(1, Range().Value, Chr(40)) - 1)
编辑:Left()
更有意义......
Edit2:查找/替换方法:
Range(ENTIRE RANGE HERE).Replace("(*)", "", LookAt:=xlPart)
答案 1 :(得分:0)
Dim str1 As String
Dim str2 As String
Dim rngTemp As Range
Dim rngCell As Range
str1 = "(AZ)"
str2 = "(ABCD)"
'Set rngTemp
Set rngTemp = Cells(1, 1).CurrentRegion 'You range goes here
'Loop through range and replace string
For Each rngCell In rngTemp
If InStr(1, rngCell, str1) > 0 Then
rngCell = Replace(rngCell.Value, str1, "")
End If
If InStr(1, rngCell, str2) > 0 Then
rngCell = Replace(rngCell.Value, str2, "")
End If
Next rngCell
答案 2 :(得分:-1)
此代码应该适合您。假设您的代码包含您正在处理的列以及要删除的文本。
Sub YourSub()
RemoveCharactersFromComumn "A", "(AZ)"
RemoveCharactersFromComumn "B", "(ABCD)"
End Sub
Sub RemoveCharactersFromComumn(col As String, find As String)
Dim rng As Range
Set rng = Range(col + ":" + col)
rng.Replace What:=find, Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub