+ Reply to Thread
Results 1 to 8 of 8

VBA Trim with variables????

  1. #1
    Registered User
    Join Date
    10-10-2017
    Location
    UK
    MS-Off Ver
    365
    Posts
    28

    VBA Trim with variables????

    Afternoon all,

    I am trying to copy cells from a specified sheet from a specified workbook.

    In order to do so, the file directory and sheet name are being saved as the variables File and MON respectively.

    This is how the code would work without the variables;

    Please Login or Register  to view this content.
    however when I try and put variables in, it won't work;

    Please Login or Register  to view this content.
    Anyone have any ideas??

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA Trim with variables????

    Hi,

    Does File end with a square bracket? If not, you need to concatenate that in
    Please Login or Register  to view this content.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Registered User
    Join Date
    10-10-2017
    Location
    UK
    MS-Off Ver
    365
    Posts
    28

    Re: VBA Trim with variables????

    Quote Originally Posted by xlnitwit View Post
    Hi,

    Does File end with a square bracket? If not, you need to concatenate that in
    Hi,

    I've tried that, however I then get too many square brackets. If you type in the file directory as opposed to using a variable, It turns C:\Users\[Test.xslx] into just [Test.xslx].

    I am able to use a variable for the file name alone however which is why i'm sure this is possible!

    This code below uses the File variable correctly however it takes the data from the first sheet every time;

    Please Login or Register  to view this content.
    This code below uses the MON variable fine however it requires me to manually enter the file directory;

    Please Login or Register  to view this content.
    I just want to be able to combine them both!

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA Trim with variables????

    What exactly is in the File variable?

  5. #5
    Registered User
    Join Date
    10-10-2017
    Location
    UK
    MS-Off Ver
    365
    Posts
    28

    Re: VBA Trim with variables????

    Quote Originally Posted by xlnitwit View Post
    What exactly is in the File variable?
    I have been able to get the original code to work. The issue was that the string saved into the File variable was 'C:\User\Test.xslx'. I have found that if I put in square brackets so it reads 'C:\User\[Test.xslx]'.

    Is there a way I can change the initial code so I don't have to manually add these brackets every time?

    The file directory will change every time.

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA Trim with variables????

    Yes.
    Please Login or Register  to view this content.
    for example.

  7. #7
    Registered User
    Join Date
    10-10-2017
    Location
    UK
    MS-Off Ver
    365
    Posts
    28

    Re: VBA Trim with variables????

    Quote Originally Posted by xlnitwit View Post
    Yes.
    Please Login or Register  to view this content.
    for example.
    That works great, thank you for your help

  8. #8
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA Trim with variables????

    You're welcome. Please remember to mark the thread Solved using the Thread Tools link at the top of the page.

+ 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. [SOLVED] trim to match two sets of input-trim spaces around a character
    By nigelog in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-21-2017, 12:33 PM
  2. VLOOKUP + TRIM, TRIM Lookup table array...
    By mangesh.mehendale in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 10-19-2016, 02:10 AM
  3. [SOLVED] Trim Text Based on multiple character series variables
    By mysticmoron109 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 05-03-2016, 07:36 PM
  4. [SOLVED] VBA TRIM is won't TRIM active worksheet
    By Groovicles in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 06-06-2014, 09:55 AM
  5. Trying to trim a trailing space char and the Trim function isn't working
    By Psychochook in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-30-2007, 11:28 PM
  6. trim function-how would one incorporate the trim function within the if statement?
    By specter@doom.com in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-29-2005, 11:10 PM
  7. VBA Trim and Application.worksheetfunction.Trim
    By Hari Prasadh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-19-2005, 11:06 AM

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