我目前有一个宏,它从列中的单元格中获取输入,删除文本和空格,缩短为最后13位,然后输出到下一列。
我在之前的一个问题中看到了一些代码,它使用正则表达式来删除文本(我之前已经将空格删除了)。但是,我不确定如何使用当前代码运行函数并输出到单元格。
目前所做的只是缩短到13位,所以我认为该功能没有正确运行/调用。
当前的宏是:
Sub RemoveSpaces_Click()
i = 7
j = 7
Do While ActiveWorkbook.ActiveSheet.Cells(i, 1) <> ""
InputString = ActiveWorkbook.ActiveSheet.Cells(i, 1)
removeAlpha (InputString) ' <--- How to call this?
' Right() after removing to avoid text counting as length
InputString = Right(InputString, 13)
j = j + 1
' Output to second column to preserve original input
ActiveWorkbook.ActiveSheet.Cells(i, 2) = InputString
i = i + 1
Loop
End Sub
Function removeAlpha(strInput As String) As String
strInput = Replace(strInput, " ", "")
With CreateObject("vbscript.regexp")
.Pattern = "[A-Za-z]"
.Global = True
removeAlpha = .Replace(strInput, "")
End With
End Function
答案 0 :(得分:0)
它将自动运行,只要你在A7中有东西,它将使用A7中的文本并通过你的函数运行它。该函数将使用InputString
。
Option Explicit
Sub RemoveSpaces_Click()
Dim i As Long, j As Long
Dim InputString As String
Dim OutputString As String
Dim ResultString As String
i = 7
j = 7
' If A7 is not empty continue and continue the loop until cell in column A is empty
Do While ActiveWorkbook.ActiveSheet.Cells(i, 1) <> ""
' This string will contain data from A7
InputString = ActiveWorkbook.ActiveSheet.Cells(i, 1)
' This will use the InputString value in the function and save it to OutputString
OutputString = removeAlpha(InputString) ' This will be called automatically
' Right() after removing to avoid text counting as length
' Use the OutputString in the Right-function and save to ResultString
ResultString = Right(OutputString, 13)
' Loop incremental
j = j + 1
' Output to second column to preserve original input
ActiveWorkbook.ActiveSheet.Cells(i, 2) = ResultString
' Loop incremental for your rows (7, 8 etc.)
i = i + 1
Loop
End Sub