+ Reply to Thread
Results 1 to 3 of 3

VBA - Trim Range Copy/Paste

  1. #1
    Registered User
    Join Date
    02-03-2016
    Location
    Brasil
    MS-Off Ver
    2013
    Posts
    10

    VBA - Trim Range Copy/Paste

    Hi guys,

    I would like to know if its possible to use trim in a copy and paste code with range, such as the below:

    Sub Macro1()
    '
    ' Macro1 Macro
    '

    '
    Range("A1:F13").Select
    Selection.Copy
    Range("I1").Select
    ActiveSheet.Paste
    End Sub


    I need to trim when I paste this range on "I1", because some of the cells got blank spaces.

    Thanks

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: VBA - Trim Range Copy/Paste

    Hi rafapmotta,
    Here is what I think you are trying to do:
    Please Login or Register  to view this content.
    You may have found a very interesting thing about the TRIM() function when use in Excel vs VBA.

    In Excel (in a cell) the TRIM() function removes spaces in front and behind a string AND also replaces double spaces with a single space.

    Example: = Trim(" Space In Front and End " ) gives back "Space In Front and End"
    While if you use the VBA form of Trim it only trims the leading and trailing spaces and does not compress multiple spaces into a single space.
    Example: = Trim(" Space In Front and End " ) gives back "Space In Front and End"

    Note: The examples above don't look correct as our web site compresses multiple spaces into single spaces. There are extra spaces between words in the VBA Trim() function when I posted it and/or edit it.

    If you are trying to replace multiple spaces to a single space using the Trim function using VBA you will need to use the Excel Worksheet function as shown above.
    WorksheetFunction.Trim().

    I hope that answers your question as you've stumbled onto differences between VBA and Excel function of TRIM() differences.
    Last edited by MarvinP; 02-12-2016 at 01:34 PM.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: VBA - Trim Range Copy/Paste

    Hello rafapmotta,

    Maybe you would want to try just a slight adjustment to the code by MarvinP.

    Please Login or Register  to view this content.
    Regrads.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Loop through files in folder, copy range, Paste range to original workbook
    By knevil in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 03-09-2016, 05:33 PM
  2. [SOLVED] Help! - Copy Range (Sheet1,A10:F50), Paste Range (Sheet 2,A1:F41), Offset Range & Repeat
    By cjtimmer in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-30-2015, 06:27 PM
  3. copy paste macros
    By rrk2008 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2014, 06:42 AM
  4. Replies: 4
    Last Post: 03-17-2013, 05:58 AM
  5. Copy/Paste range of data excluding certain rows and copy to another workbook
    By HoerbigAdm in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-12-2012, 02:51 PM
  6. Change range of cells within VBA macro and copy and paste to fixed cell range
    By Mannyny in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-20-2012, 11:51 AM
  7. Copy dynamic range from previous sheet and paste after last used row in a range
    By Mistweaver in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-04-2010, 06:24 PM

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