+ Reply to Thread
Results 1 to 23 of 23

Trim specific number of characters from multiple files

  1. #1
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Trim specific number of characters from multiple files

    I have a userform. I want to enter a number in the textbox and click a button that will allow me to select a folder. I then want all the files in that folder to be trimmed/reduced/shortened by the number entered in the textbox. For example, if the name of a file is 'appleboxforstorage' and I entered the number 9 in the textbox, then the new filename would be 'orstorage'. By the way, no filename extensions are shown in the folder as I have that feature turned off (i.e. hide extensions for known filetypes).

    I have attached a workbook with what I have done so far. I only have the code to open and browse for a folder.

    I picture of the userform is below also:

    010119.png

    Thank you!
    Attached Files Attached Files
    If you have received help from a user, please feel free to Rep that person.
    Also, do remember to click Thread Tools and mark your thread as solved, if you have achieved a solution to your thread.

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Trim specific number of characters from multiple files

    I incorporated the truncation code into your folder picker code.

    Please Login or Register  to view this content.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: Trim specific number of characters from multiple files

    Thanks and happy new year!

    I tried it and it did not work. I got a debug runtime error 52 with the below line highlighted in yellow:

    Please Login or Register  to view this content.
    Also, regarding the below line, why did you include .xls? The files which I am trying to shorten their filenames are not Excel files.

    Please Login or Register  to view this content.
    Also, maybe the error is caused because some of the characters in the filename are non-alphanumeric (Chinese) characters. There must be a way to bypass non-alphanumeric I think and hope.
    Last edited by AliGW; 01-03-2019 at 02:43 AM.

  4. #4
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Trim specific number of characters from multiple files

    Change this line as shown below. I used ".xls*" because I assumed that your files would be Excel files since not other types were specified in the OP. The code worked in testing with Excel files, so it should work with other files if the wildcard is used by itself as an extension.

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: Trim specific number of characters from multiple files

    Thank you. You are right, it works but only with filenames containing alphanumeric characters. I tested it with various file types containing alphanumeric filenames and it worked. However the files I am using contain mostly Chinese characters and the code creates a run-time error 52 as shown below unfortunately.

    error020219.png

    There must be some way to have a code written that bypasses and ignores non alphanumeric characters I believe. I just don't know how.

    I have attached an example file containing a non alphanumeric filename so you can see the error yourself.

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,728

    Re: Trim specific number of characters from multiple files

    Please don't quote whole posts, especially when you are responding to the one immediately preceding your own - it's just clutter. It's OK to quote if you are responding to a post out of sequence, but limit quoted content to a few relevant lines that makes clear to whom and what you are responding. Thanks!

    For normal conversational replies, try using the QUICK REPLY box below.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  7. #7
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: Trim specific number of characters from multiple files

    @AliGW, oh - sorry boss. That makes sense. Especially saving on server space too. By the way, are you able to lend your expertise to help me get a solution to bypass non-alphanumeric characters?

    Happy new year.

  8. #8
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,728

    Re: Trim specific number of characters from multiple files

    No, sorry - I don't do VBA. Just an administrative note.

  9. #9
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: Trim specific number of characters from multiple files

    Try Below Code

    Please Login or Register  to view this content.
    Regards,
    MohanS


    "Perfection is not attainable, but if we chase perfection we can catch excellence." - Vince Lombardi

    You can simply say thanks by clicking "*Add Reputation" icon

  10. #10
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: Trim specific number of characters from multiple files

    mohan.r1980, thanks but it did not work. First it only got as far as opening the folder picker and then the below error showed:

    b03.01.19.png

  11. #11
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: Trim specific number of characters from multiple files

    working here
    see attachment
    Attached Files Attached Files

  12. #12
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: Trim specific number of characters from multiple files

    mohan.r1980, I tried it every possible way and it is not working. It keeps on prompting to open a folder and does not carry out the process. Then the runtime error 9 shows. JLGWhiz's code worked but only with alphanumeric filenames. Non-alphanumeric (e.g. Chinese) character filenames don't work and I am looking for a code that can work with non alphanumeric filenames.

    Thanks for trying anyway...

  13. #13
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: Trim specific number of characters from multiple files

    Just checking, is there anyone here with the expertise to help me find a solution to this problem? Much appreciated to anyone who can solve this. Thank you!

    Emma

  14. #14
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: Trim specific number of characters from multiple files

    thanks but it did not work. First it only got as far as opening the folder picker and then the below error showed:
    My code is for multiple file picker.
    you have to select multiple files in your folder

  15. #15
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: Trim specific number of characters from multiple files

    Hi mohan.r1980, thanks for your reply. I tried picking files and still got an error.

    As a test, can you please rename any test file with the below filename and try your code? Do you get an error or were you able to trim the filename? It seems I cannot trim filenames that have Chinese (non-alphanumeric) characters.


    Please Login or Register  to view this content.
    Please help me solve this.

    Emma


  16. #16
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: Trim specific number of characters from multiple files

    show me the desire Input and output of given filename (which you exactly wanted).

  17. #17
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: Trim specific number of characters from multiple files

    Thanks for your reply.

    For example, let us say I have a file in a folder with the below filename:

    Please Login or Register  to view this content.
    The above is 29 characters in length.

    What if I want to trim the above file name and reduce the amount of characters from left to right. For example, I may want to remove the first 20 characters. I will therefore enter 20 in the textbox of the userform and when I click the command button, the new file name will be as shown below:

    Please Login or Register  to view this content.
    Therefore 20 characters is trimmed from the filename, leaving only 9 characters.

    That is what I want but I need a code that can recognize and process the Chinese characters. These are non-alphanumeric characters. The codes that you and others have suggested do not seem to support Chinese characters. I Googled online and saw that there is a code that can be used that will make some sort of Function call and tell the code to recognize the Chinese characters but I don't know how to write the code.

    Thank you.

  18. #18
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: Trim specific number of characters from multiple files

    mohan.r1980, any luck?

    Emma

  19. #19
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Trim specific number of characters from multiple files

    Do you want to change;

    "Vegetables正在画的房子GardenVariety" to "GardenVariety"

    "Flooring米饭可以购买不同的质&#37327.xlsx" to "37327.xlsx"
    ?

  20. #20
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: Trim specific number of characters from multiple files

    Hi jindon, yes pretty much you are correct. It all depends on the value that I will input in the textbox. But yes, basically I want to trim the filenames.

    Thanks,
    Emma

  21. #21
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Trim specific number of characters from multiple files

    OK

    The problem is that vba can not read these characters directly.
    So if you copy all the file names that you want to change in a Text file, and if the file can be opened by Excel, it can be done.

    Anyway, you can not handle these files as they are.

  22. #22
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: Trim specific number of characters from multiple files

    That is a pity as there are about 3000 files; too much time to copy them.
    I thought there was some clever way for VBA to bypass the Chinese characters in the code.

  23. #23
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Trim specific number of characters from multiple files

    I don't think so, unfortunately.

+ 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] Copy Specific Number of Characters From One Column to Another
    By CircleG in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-17-2014, 11:20 AM
  2. [SOLVED] Limit LOOKUP to a specific number of characters
    By floydian in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-25-2013, 09:34 PM
  3. open all files in a folder with fixed number of characters in filename
    By Telemick in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 01-18-2012, 03:31 AM
  4. Trim a files name to a specific amount of characters using vba.
    By 111StepsAhead in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-18-2011, 11:54 AM
  5. Reading specific characters from closed files
    By chalie1105 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-30-2006, 03:27 PM
  6. [SOLVED] Finding cells with a specific number of characters
    By Kamran in forum Excel General
    Replies: 6
    Last Post: 03-29-2006, 06:10 PM
  7. [SOLVED] trim a string by specific number of characters
    By windyoldman in forum Excel General
    Replies: 2
    Last Post: 07-13-2005, 09:05 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