+ Reply to Thread
Results 1 to 11 of 11

Export based on Value in Column, Illegal Character for Windows file path

  1. #1
    Registered User
    Join Date
    05-06-2017
    Location
    Canada
    MS-Off Ver
    Office 365 subscription
    Posts
    44

    Talking Export based on Value in Column, Illegal Character for Windows file path

    I found this neat piece of code and it does what I want: Export based on Values in a specific value in a column.

    I inspected the code and this line here is responsible for generating the file name:

    Please Login or Register  to view this content.
    THE PROBLEM is this: Sometimes the value in the column which gets expoerted after which the file name is created, has an illegal character that cannot be in a windows file path, like / or :

    I know I could replace the values in the column with other characters such as "-", however, the problem is that this column is also used in a VLOOKUP-Formula, and if I change the value in it, VLOOKUP cannot find the corresponding values.

    So my current workaround is to create another column in which I remove the illegal characters, export the files and delete the column afterwards, but this is awkward.

    Is there a way to have the export-skript omit or replace these illegal characters right in th VBA Code?

    Thanks!
    Last edited by MinisoftEggshell; 02-27-2022 at 01:28 PM.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,312

    Re: Export based on Value in Column, Illegal Character for Windows file path

    Use this function in your module, like


    Please Login or Register  to view this content.

    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,026

    Re: Export based on Value in Column, Illegal Character for Windows file path

    I use the attached (Sucuri flagged it as SQL injection and I have no idea why so I have to attach it as a text file)

    You could use it like this. The default replacement character is "-" but you can call it specifying any character or string you want.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Re: Export based on Value in Column, Illegal Character for Windows file path


    Hi Jeff !

    As this weird Securi does not like a double replace see the trick with your code revamped with just a single character :

    Please Login or Register  to view this content.
    Works also with a space instead of $ …
    Last edited by Marc L; 02-24-2022 at 12:28 PM.

  5. #5
    Registered User
    Join Date
    05-06-2017
    Location
    Canada
    MS-Off Ver
    Office 365 subscription
    Posts
    44

    Re: Export based on Value in Column, Illegal Character for Windows file path

    Thanks I'm sure I'm doing something wrong... Is it important where I put the Public Function?

    I inserted a new Module for the public function and the other part in Sub ExportData() -- I get the error code 13.

    Or is it because of Option Explicit? But I declared all Variables... ?

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,312

    Re: Export based on Value in Column, Illegal Character for Windows file path

    Make sure that you don't use \ in whatever function you use (which deletes it) - I think you are passing the entire path and file name to it, and \ is a necessary part of that value.

  7. #7
    Registered User
    Join Date
    05-06-2017
    Location
    Canada
    MS-Off Ver
    Office 365 subscription
    Posts
    44

    Re: Export based on Value in Column, Illegal Character for Windows file path

    So what you're saying is that the Variable SavePath, which retrieves its String value from a cell, for example C:\Users\someone\test, has its "\"s also replaced?

    I cannot post the code here, unfortunately...

    https://pastebin.com/zty6AfqU
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,312

    Re: Export based on Value in Column, Illegal Character for Windows file path

    You have this line:

    Please Login or Register  to view this content.
    This part

    Please Login or Register  to view this content.
    creates a string that PROBABLY is structured like


    "C:\Folder1\SubFolder\Filename 2022-02-24.xlsx"

    The function ValidWindowsFileName that was posted has this:

    Please Login or Register  to view this content.
    Which you should change to this, to keep the \ characters that are needed for the filesave:

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    05-06-2017
    Location
    Canada
    MS-Off Ver
    Office 365 subscription
    Posts
    44

    Re: Export based on Value in Column, Illegal Character for Windows file path

    So in other words if any value in a cell that has this character, say for example " Products\Ownership" I have no chance but to manually remove the "\" character within the cell.

    why do people name their stuff using these characters 😰

  10. #10
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,312

    Re: Export based on Value in Column, Illegal Character for Windows file path

    No - you just need to handle your file name in parts, like

    ActiveWorkbook.SaveAs SavePath & ValidWindowsFileName(ArrayOfUniqueValues(ArrayItem)) & Format(Now(), " YYYY-MM-DD hhmmss") & ".xlsx", 51


    That will work as long as SavePath ends in a \ character

  11. #11
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,026

    Re: Export based on Value in Column, Illegal Character for Windows file path

    Thanks to Bernie Deitrick. My code was designed to just look at the file name, not the whole path. I overlooked that.

+ 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. export windows contact file
    By uwe_ in forum Outlook Programming / VBA / Macros
    Replies: 0
    Last Post: 08-16-2016, 05:31 AM
  2. Replies: 2
    Last Post: 07-19-2013, 01:02 AM
  3. Excel 2007 : Export Selection As A Windows Dos Bat File
    By vivekhalder in forum Excel General
    Replies: 8
    Last Post: 09-28-2011, 02:39 PM
  4. [SOLVED] Windows API to Determine if File is Local/Convert Path to Drive Letter
    By Johnny in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-10-2006, 09:30 AM
  5. Export CSV File, set Windows Read-Only attribute
    By InNeedOfHelp! in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-27-2005, 04:06 PM

Tags for this Thread

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