+ Reply to Thread
Results 1 to 9 of 9

Deleting leading and trailing spaces from a number

Hybrid View

maacmaac Deleting leading and trailing... 03-31-2009, 04:28 PM
shg Re: Deleting leading and... 03-31-2009, 04:51 PM
maacmaac Re: Deleting leading and... 03-31-2009, 05:30 PM
shg Re: Deleting leading and... 03-31-2009, 05:32 PM
shg Re: Deleting leading and... 03-31-2009, 07:55 PM
  1. #1
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,176

    Deleting leading and trailing spaces from a number

    I am using the following code to import an excel file into an existing sheet. The problem I am having is the way some of the cells are being imported. Some of the cells have spaces before and after the number. I need to delete the leading and trailing spaces for each cell affected. Is there a code I can incorporate to do this? BTW…the columns effected are columns F & G. Thanks

    Sub MuniAnalysis()
    
    Dim i               As Long
    Dim j               As Long
    Dim n               As Long
    Dim LastRow         As Long
    Dim Prompt          As String
    Dim Title           As String
    Dim Path            As String
    Dim SourceWkb       As Workbook
    Dim DestinationWkb  As Workbook
    Dim Rng             As Range
    
        Set DestinationWkb = ThisWorkbook
        
        Prompt = "Select the Excel file to process."
        Path = Application.GetOpenFilename("Excel Files (*.xls), *.xls", , Prompt)
        If Path = "False" Then
            GoTo ExitSub:
        End If
    
        Application.DisplayAlerts = False
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        
        Workbooks.Open Filename:=Path
        
        Set SourceWkb = ActiveWorkbook
     
            Range("A1").Select
              
            Set Rng = Selection.CurrentRegion
                Rng.Resize(Rng.Rows.Count, 16).Copy
    
            With DestinationWkb.Worksheets("Muni Analysis")
                LastRow = .Range("A" & Rows.Count).End(xlUp).Row
                .Range("A" & LastRow + 1).PasteSpecial Paste:=xlValues, Operation:=xlNone, _
                    SkipBlanks:=False, Transpose:=False
            End With
        
        SourceWkb.Close SaveChanges:=False
    
    ExitSub:
        
        Application.DisplayAlerts = True
        Application.EnableEvents = True
        Application.ScreenUpdating = True
        Application.StatusBar = False
        
        Set SourceWkb = Nothing
        
    End Sub
    Last edited by maacmaac; 03-31-2009 at 10:35 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Deleting leading and trailing spaces from a number

    The Trim function removes leading and trailing spaces. You'd need to loop through the cells to apply it.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,176

    Re: Deleting leading and trailing spaces from a number

    I tried using the Trim Function in the code but no luck. I even tried to run the function on it's own for each cell in columns F, G, & H and that did not work either. I am stumped. I attached both files for review if needed. The file I am importing from has hyperlinks to each number but I am only pasting values. I'm not sure if that is part of the problem with the trim function not working. Thanks.
    Attached Files Attached Files

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Deleting leading and trailing spaces from a number

    Did you check to see if the leading/trailing characters are indeed blanks, and not, for example, non-breaking spaces?

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Deleting leading and trailing spaces from a number

    It is indeed a non-breaking space.

    In VBA, try
    cell.value=Trim(Replace(cell.text, Chr(160), ""))

  6. #6
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,176

    Re: Deleting leading and trailing spaces from a number

    Shg,

    Thank you for information. The VBA code is working as expected. One more question if you don't mind? How are you able to tell the difference between a "blank" and a "non-breaking space"? And what is the difference? Thanks again.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1