2016-11-07 6 views
0

現在、列のセルから入力を受け取り、テキストとスペースを削除し、最後の13桁に短縮してから次の列に出力するマクロがあります。入力時にさまざまな関数を実行してセルに出力

以前の質問でregexを使ってテキストを削除するコードを表示しました(これまでは削除されていたスペースがありました)。しかし、私はどのように関数を実行し、現在のコードでセルに出力することができないのか分かりません。

現在、すべての機能は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

'InputString = removeAlpha(InputString) ' –

答えて

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 
+0

マクロが何をしているのか分かりましたが、ありがとうございました!つまり、「Option Explicit」やさまざまな定義を追加しても機能しませんでした。 –

+0

更新されたバージョンでもう一度お試しください。あなたのためにそれをコメントしようとしました。 – Niclas

+0

F8キーを使用してコードをステップ実行します。あなたはそれを使用するときにプロセスに従うことができ、マクロの実行方法を見ることができます。 – Niclas

関連する問題