gpt4 book ai didi

regex - 如何使用Excel VBA脚本删除某些字符

转载 作者:行者123 更新时间:2023-12-02 09:31:53 24 4
gpt4 key购买 nike

以下 VBA 脚本删除了不需要的字符,但遗憾的是仅删除了数字。

您能帮助我吗,它也需要删除字母,如下表示例(粗体)所示。

范围可以是 0 到 15000+ 个单元格之间的任意位置

...................................................... ......

aa纽约aa

bb纽约bb

cc 约克 c 沃特敦 c 纽约 c

6大道66约克6城市6

...................................................... ......

VBA 脚本:

Sub Remove()

Application.ScreenUpdating = False
Dim R As RegExp, C As Range
For Each C In Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
If R Is Nothing Then
Set R = New RegExp
R.Global = True
R.Pattern = "\D"
C.Offset(0, 1) = R.Replace(C, "")
R.Pattern = "\d"
C = R.Replace(C, "")
End If
Set R = Nothing
Next C
Application.ScreenUpdating = True
End Sub

编辑1

Sub Remove()
Call BackMeUp

Dim cell As Range
Dim RE As Object
Dim Whitecell As Range
Dim strFind As String, strReplace As String
Dim lLoop As Long
Dim Loop1 As Long

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Range("A3:L3").Select
Selection.Delete Shift:=xlUp
'--------------------------------------------------Remove JUNK
Range("A3:A" & Cells(Rows.Count, 1).End(xlUp).Row).Select
On Error Resume Next
For lLoop = 1 To 100
strFind = Choose(lLoop, "~?»", "~®", "~.", "~!", "~ï", "~-", "~§", "~$", "~%", "~&", "~/", "~\", "~,", "~(", "~)", "~=", "~www", "~WWW", "~.com", "~.net", "~.org", "~{", "~}", "~[", "~]", "~ï", "~¿", "~½", "~:", "~;", "~_", "~µ", "~@", "~#", "~'", "~|", "~€", "~ä", "~ö", "~ü", "~Ä", "~Ü", "~Ö", "~+", "~<", "~>", "~nbsp", "~â", "~¦", "~©", "~Â", "~–", "~¼", "~?")
strReplace = Choose(lLoop, " ")

Selection.Replace What:=strFind, Replacement:=strReplace, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

Next lLoop
'--------------------------------------------------Remove Numbers
Range("A3:A" & Cells(Rows.Count, 1).End(xlUp).Row).Select
On Error Resume Next
For Loop1 = 1 To 40
strFind = Choose(lLoop, "~1", "~2", "~3", "~4", "~5", "~6", "~7", "~8", "~9", "~0")
strReplace = Choose(Loop1, " ")

Selection.Replace What:=strFind, Replacement:=strReplace, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

Next Loop1
'--------------------------------------------------Remove Single Letters
Set RE = CreateObject("vbscript.regexp")
RE.Global = True
RE.MultiLine = True
RE.Pattern = "^[a-z]\b | \b[a-z]\b"

For Each cell In Range("A3:A" & Cells(Rows.Count, 1).End(xlUp).Row)
cell.Value = RE.Replace(cell.Value, "")

Next

'--------------------------------------------------Remove WHITE SPACES

For Each Whitecell In Range("A3:A" & Cells(Rows.Count, 1).End(xlUp).Row)
Whitecell = WorksheetFunction.Trim(Whitecell)
Next Whitecell

'--------------------------------------------------Remove DUPES

ActiveSheet.Range("A3:A" & Cells(Rows.Count, 1).End(xlUp).Row).RemoveDuplicates Columns:=1, Header:=xlYes
ActiveWorkbook.Worksheets(ActiveSheet.Name).Sort.SortFields.Clear

'--------------------------------------------------Copy to B - REPLACE ALL WHITE IN B

Range("A3:A" & Cells(Rows.Count, 1).End(xlUp).Row).Select
Selection.Copy
Range("B3:B" & Cells(Rows.Count, 1).End(xlUp).Row).Select
ActiveSheet.Paste
Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Columns("A:L").EntireColumn.AutoFit

'--------------------------------------------------END
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Range("a1").Select
End Sub

最佳答案

编辑(删除了原始答案,因为在收到有关您想要的内容的更多信息后不适用,但留下了建议)

  • 您正在每个单元格创建/销毁 RE 对象,即昂贵/不必要
    • 如果其他用户将使用该函数,请在代码中创建对象,而不是添加引用
    • 最后不需要将正则表达式对象设置为空 - 变量在函数结束时从内存中释放自动
    • 改进变量命名并使用正确的缩进有助于提高可读性并使其更易于编辑
    • 添加多行选项,以防单元格内有换行符。
    • 如果处理大量单元格,您可能需要使用变体数组

UDPATE 2

根据下面的评论,以下是如何仅获取两个或多个小写字符以及中间的单个空格的出现。我个人认为,一个好的方法是提取您想要的内容,而不是替换您不想要的内容。我在这个网站上分享了很多下面的功能,因为它非常有用。下面是如何对 A 列的内容调用它并将结果放入 B 列的示例。

Sub test()

' Show how to run this on cells in A and transpose result in B
Dim varray As Variant
Dim i As Long

Application.ScreenUpdating = False
varray = Range("A1:A15000").Value

For i = 1 To UBound(varray, 1)
varray(i, 1) = RegexExtract(varray(i, 1), "([a-z]{2,})", " ")
Next

Range("B1").Resize(UBound(varray, 1)).Value = _
Application.WorksheetFunction.Transpose(varray)

Application.ScreenUpdating = True

End Sub

并确保它在模块中:

Function RegexExtract(ByVal text As String, _
ByVal extract_what As String, _
Optional seperator As String = "") As String

Dim i As Long
Dim j As Long
Dim result As String
Dim allMatches As Object
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")

RE.Pattern = extract_what
RE.Global = True
Set allMatches = RE.Execute(text)

For i = 0 To allMatches.Count - 1
For j = 0 To allMatches.Item(i).submatches.Count - 1
result = result & seperator & allMatches.Item(i).submatches.Item(j)
Next
Next

If Len(result) <> 0 Then
result = Right$(result, Len(result) - Len(seperator))
End If

RegexExtract = result

End Function

关于regex - 如何使用Excel VBA脚本删除某些字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8195996/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com