SOLVED

Remove Spaces (modify a current macro)

Steel Contributor

Hello, I have a macro I use to remove the word "USD" from columns E and F.  

I need to add the following to the macro code:

TRIM(SUBSTITUTE(this needs to be column E and F,CHAR(160),""))

this will remove all spaces in the cells. 

I need to apply it to columns E and F only.  

How could I do this?  

 

Sub RemoveUSDText()
'
' RemoveUSDText Macro
' Col E and F

Columns("E:F").Select
Selection.Replace What:="USD", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Range("E1").Select
End Sub

3 Replies
best response confirmed by Tony2021 (Steel Contributor)
Solution

 

 

Sub RemoveUSDText()
'
' RemoveUSDText Macro
' Col E and F
    With Range("E:F")
        .Replace What:="USD", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
        .Replace What:=Chr(160), Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    End With
End Sub

 

@Tony2021 

@Tony2021 

And if you want to remove ALL spaces:

Sub RemoveUSDText()
'
' RemoveUSDText Macro
' Col E and F
    With Range("E:F")
        .Replace What:="USD", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
        .Replace What:=Chr(160), Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
        .Replace What:=" ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    End With
End Sub
Perfect! thank you very much. I used the one with the single space chr(160)
1 best response

Accepted Solutions
best response confirmed by Tony2021 (Steel Contributor)
Solution

 

 

Sub RemoveUSDText()
'
' RemoveUSDText Macro
' Col E and F
    With Range("E:F")
        .Replace What:="USD", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
        .Replace What:=Chr(160), Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    End With
End Sub

 

@Tony2021 

View solution in original post