Regex 在子对象中调用Myrange函数

Regex 在子对象中调用Myrange函数,regex,excel,vba,Regex,Excel,Vba,我创建了一个regex函数,该函数将删除列中的所有空白,当我在工作表中使用它时,我只需键入=simplecellregex(),然后在新列中针对所有条目运行该函数。我这样做的原因是因为TRIM()并不总是有效,所以我寻找了一种有效的方法 Function simpleCellRegex(Myrange As Range) As String Dim Regex As New RegExp Dim strPattern As String Dim strInput As

我创建了一个
regex
函数,该函数将删除列中的所有空白,当我在工作表中使用它时,我只需键入
=simplecellregex()
,然后在新列中针对所有条目运行该函数。我这样做的原因是因为
TRIM()
并不总是有效,所以我寻找了一种有效的方法

Function simpleCellRegex(Myrange As Range) As String

    Dim Regex As New RegExp
    Dim strPattern As String
    Dim strInput As String
    Dim strReplace As String
    Dim strOutput As String

    strPattern = "\s+$"

        If strPattern <> "" Then
            strInput = Myrange.Value
            strReplace = ""

        With Regex
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = strPattern
        End With

        If Regex.Test(strInput) Then
            simpleCellRegex = Regex.Replace(strInput, strReplace)
        Else
            simpleCellRegex = strInput
        End If
    End If
End Function



Sub regex1()

Column.Add
Range("D2").Value = simpleCellRegex(Myrange, String)

End Sub
函数simpleCellRegex(Myrange作为范围)作为字符串
Dim Regex作为新的RegExp
作为字符串的Dim strPattern
像弦一样的模糊的条纹
变暗字符串替换为字符串
作为字符串的暗输出
strPattern=“\s+$”
如果strPattern“”则
strInput=Myrange.Value
strReplace=“”
用正则表达式
.Global=True
.MultiLine=True
.IgnoreCase=False
.Pattern=strPattern
以
如果正则表达式测试(strInput),则
simpleCellRegex=Regex.Replace(strInput,strReplace)
其他的
simpleCellRegex=strInput
如果结束
如果结束
端函数
子regex1()
列。添加
范围(“D2”)。值=simpleCellRegex(Myrange,字符串)
端接头
这就是设置,所以每当我得到工作簿时,我只要单击我想要函数运行的列,它就会运行正则表达式,并将它吐出它旁边的列。计划是将此设置为宏,这样我就可以在excel菜单功能区上添加一个按钮,并使此正则表达式易于运行。

编辑: 如果要选择一个范围,然后按下按钮,请使用以下命令

Option Explicit
Public Sub RemoveEndWhiteSpace()
    Dim arr(), i As Long, myRange As Range
    Set myRange = Selection
    If myRange.Columns.Count > 1 Or myRange Is Nothing Then Exit Sub
    If myRange.Count = 1 Then
        myRange = RTrim$(myRange.Value)
        Exit Sub
    Else
        arr = myRange.Value
        For i = LBound(arr, 1) To UBound(arr, 1)
            arr(i, 1) = RTrim$(arr(i, 1))
        Next i
        myRange = arr
    End If
End Sub
要输出到其他列,请执行以下操作:

myRange.Offset(, 1) = arr '<==use offset to put result in a different column e.g. one to the right
更可靠的色谱柱使用范围为:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count = Columns(1).Cells.Count And Target.Columns.Count = 1 Then
       ' MsgBox "running"
        Dim lastRow As Long, myRange As Range
        lastRow = Cells(Rows.Count, Target.Column).End(xlUp).Row
        Set myRange = Range(Cells(1, Target.Column), Cells(lastRow, Target.Column))
        RemoveEndWhiteSpace myRange
    End If
End Sub


TRIM并不用于删除所有空白。还有问题吗?如果你能很清楚地说明问题是什么,以及在实施过程中遇到的问题,这会有所帮助。问题是什么?你在哪里被卡住了?@Aneta这个辅助呼叫根本不起作用。它不会占用我的范围,并根据宏按钮吐出函数,而您的函数需要一个范围参数。调用它时,您正在传递2个参数。并且,您要传递的第二个变量可能会混淆编译器,即使您的函数预期为两个,因为您要传递一个名为String的变量,该变量也是datatype.Myrange.Value,它将生成一个数组而不是字符串,因此您的正则表达式将无法正常工作。您需要一种方法来替换范围内的所有事件(不是单个单元格)。使用整列也是非常低效的。请参阅最后一段代码。可以将其与放置在功能区上的按钮相关联。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count = Columns(1).Cells.Count And Target.Columns.Count = 1 Then
       ' MsgBox "running"
        Dim lastRow As Long, myRange As Range
        lastRow = Cells(Rows.Count, Target.Column).End(xlUp).Row
        Set myRange = Range(Cells(1, Target.Column), Cells(lastRow, Target.Column))
        RemoveEndWhiteSpace myRange
    End If
End Sub