Function StripChar(Txt As String) As String
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "\D"
StripChar = .Replace(Txt, "")
End With
End Function
所以我试图通过单元格循环在底部范围内应用此功能
Sub Alphabetremove()
Dim ws As Worksheet
Dim Lastrow As Integer
Set ws = ActiveSheet
Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Range("F2:F" & Lastrow).Select
With Selection
.Value = StripChar(.Value)
End With
End Sub
问题是,您无法一次(按照您的尝试)在一个范围内运行函数.Value = StripChar(.Value)
,而只能在单个单元格上运行。因此,您需要从2
行循环到LastRow
,然后将该函数应用于每个单个单元格。
还要注意,行计数变量的类型必须为Long
,因为Excel的行数多于Integer
不能处理的行数。实际上,我建议使用always to use Long
instead of Integer
,因为在VBA中使用Long
没有任何好处。
此外,如果将工作表设置为变量Integer
,则需要将此变量用于all Integer
和ws
对象,否则将无用。
.Cells
最后,我强烈建议您阅读.Range
并将其应用于所有代码。