Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

excel - I want to create a macro to remove the space After the String in the range. Tried Trim and doesn't do anything


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This will get the trailing spaces..

Sub TrimTrailingSpaces()

    Dim LR As Long 'Use Long as Integer can only handle 32,767
    Dim myRng As Range 'I am going to name the used range
    Dim ws As Worksheet 'Declare worksheet as variable
    Dim cll As Range 
    
    Set ws = ThisWorkbook.Worksheets("Sheet2") 'Want to be specific on the worksheet used
    
    LR = ws.Cells(Rows.Count, 1).End(xlUp).Row 'Find the last row in Col A.  Do not include the whole column of over 1 million cells
    
    Set myRng = ws.Range(ws.Cells(2, 1), ws.Cells(LR, 1)) 'Declare the range used
    
    For Each cll In myRng 'cll is a Cell in a Collection of Cells in a Range that we defined
        cll = RTrim(cll) 'Looping through, modify each cell
    Next cll 'Go to the next Cell
    
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...