我试图创建一个宏,从数据中删除特殊字符,可以显示在许多不同的列取决于电子表格。我想突出显示该列,并使用宏从该列中的数据中删除特殊字符。数据如下所示:
AE/ABCD/001234/ABCD
AE/ABCD/001234//ABCD
AE.ABCD.001234.ABCD
AE.ABCD.001234..ABCD
感谢您的帮助
我在下面尝试了这个,但是我不能让它工作,我不想创建一个新的列
Sub Test()
Dim myString As String
Dim newString As String
myString = "I"
newString = Replace(Replace(myString, ".", " "), "/", " ")
MsgBox newString
End Sub
基于@Scott Craner的评论,你可以这样替换你的字符串:
Sub Test()
' Select the area where you want to replace
ActiveSheet.UsedRange.Select
Selection.Replace ".", " "
Selection.Replace "/", " "
End Sub
另外,如果您想要获得替换发生的列引用,那么最好是遍历整个(通过两个循环)表并逐个替换单元格。如果替换发生了,你将知道你是哪一列,你可以做任何你想做的。
Sub Test()
' Select the area where you want to replace
ActiveSheet.UsedRange.Select
Selection.Replace "a", "X"
Selection.Replace "d", "1"
LastRow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
LastColumn = ActiveSheet.UsedRange.Columns(ActiveSheet.UsedRange.Columns.Count).Column
For i = 1 To LastRow
For j = 1 To LastColumn
Dim colorColumn As Boolean
If InStr(Cells(i, j), ".") Then
Selection.Replace ".", " "
colorColumn = True
End If
If InStr(Cells(i, j), "/") Then
Selection.Replace "/", " "
colorColumn = True
End If
If colorColumn Then
' Do column coloring
End If
Next j
Next i
End Sub