+ Reply to Thread
Results 1 to 7 of 7

Excel VBA Make sure a string follows a pattern and the ones that don't make them follow it

  1. #1
    Registered User
    Join Date
    07-12-2012
    Location
    Montreal
    MS-Off Ver
    Office 2016, Office 365
    Posts
    4

    Excel VBA Make sure a string follows a pattern and the ones that don't make them follow it

    So, like the title says... I think

    Let me explain the situation a little bit:

    I've made a compiler that takes a bunch of files. All the files are supposed to be named something like 123456 - Q.ABC-A - Person's Name.txt

    Problem I'm facing is that it seems like people can't seem to follow simple instructions, and I end up with files with names like 123456-Q.ABC-A-Person's Name.txt, which was easy to get around in my code by removing spaces as each file is processed and then adding them where I actually want them, which is around the first and third Hyphens.

    My problem is the people that seem to have a harder time following instructions and I end up with files named something like 123456 - Q.ABC.A - Person's Name.txt or 123456 - Q.ABC-A Person's Name.txt, and have even had people using em dashes instead of hyphens and any number of other things that throws wrenches into my code.

    I've been reading about things like using the "like" function, but that would only return true or false on whether or not the string matches the pattern, right? I'd still have to try and guess all the ways someone might mess up naming their file.

    TL;DR

    Is there a way to code a macro so that it will force a string into a specified pattern? So that I always get 123456 - Q.ABC-A - Person's Name.txt no matter if someone replaces a dot for a hyphen or a hyphen for a dot or whatever else they might do?

    I'm trying to figure out how to import all the files and have the string get formatted to the proper pattern no matter how people seem to try and break things by naming things wrong without having to try and guess all the ways they might try to do so ahead of time.

    Note:
    • 123456 isn't always a six digit number. Sometimes there are less, but 6 should be the max.
    • Sometimes the Q.ABC.A part has a double letter such as Q.ABC-AA

    Thanks in advance for any help. I've been wracking my brain over this for a while. I'm no VBA pro, so I thought it was about time I asked for some help.
    Attached Files Attached Files
    Last edited by Dexluther; 07-19-2018 at 01:27 AM. Reason: Attaching the file I attacked below

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

    Re: Excel VBA Make sure a string follows a pattern and the ones that don't make them follo

    Try something like this
    Please Login or Register  to view this content.
    Last edited by jindon; 07-11-2018 at 12:40 AM.

  3. #3
    Valued Forum Contributor
    Join Date
    12-22-2015
    Location
    HK
    MS-Off Ver
    2010
    Posts
    532

    Re: Excel VBA Make sure a string follows a pattern and the ones that don't make them follo

    Please Login or Register  to view this content.
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    07-12-2012
    Location
    Montreal
    MS-Off Ver
    Office 2016, Office 365
    Posts
    4

    Re: Excel VBA Make sure a string follows a pattern and the ones that don't make them follo

    Sorry for not checking back here sooner. I've been out sick for a few days and wasn't in a state where I wanted to work on anything nor would I have been productive if I had tried.

    Thanks For the suggestions, but not exactly what I was thinking of and I'm not sure I understand it enough to modify anything to suit me.

    Here's a bit of code that I'm working with:

    It's pretty patched together to get around errors thrown because people can't name their files properly.

    Please Login or Register  to view this content.

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

    Re: Excel VBA Make sure a string follows a pattern and the ones that don't make them follo

    Upload a workbook with the file names that user might enter in one column and the result that you want in other column.
    Otherwise, no one can understand what you are really trying to do.

    Attach a sample workbook (not a picture or pasted copy). Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

  6. #6
    Registered User
    Join Date
    07-12-2012
    Location
    Montreal
    MS-Off Ver
    Office 2016, Office 365
    Posts
    4

    Re: Excel VBA Make sure a string follows a pattern and the ones that don't make them follo

    Basically the sheet is a hardware inventory tool. People place text files that are supposed to be named a certain way. From each of the text files it imports it uses two parts of the file name to create a temporary tab and populates the first two columns. Then it searches for certain information in the temporary tab that was made from the text file and populates the rest of the columns with the information then moves onto the next line, which is the next file in the folder.

    The problem I'm running into is that people can't seem to always name their text files correctly, which causes errors when trying the code is trying to do it's thing on them (Can't find where to split, etc). I've gotten around the issue mostly by trying to guess what kind of mistakes people might make and have the code correct for it or just ignore errors altogether.

    To use the sheet, run a DXdiag on your machine, and then save the results to a text file in a folder. Then on the sheet, click the button at the top left and navigate to the folder where you saved the DXDiag file and click ok. The file should be named something like:

    [Assest Tag] - Q.[Desk number]-[Desk Letter] - [Person's Name]
    123456 - Q.123-B - John Smith.txt or
    000587 - Q.456-AQ - John Die.txt


    Errors happen when people do things like:
    123456 - Q.123B - John Smith.txt or
    123456 - Q.123.B- John Smith.txt or
    123456 — Q.123 — B — John Smith.txt (I don't know why/how they get em dashes by mistake, which makes me think they are trying to be funny) or
    any number of other mistakes, people not paying enough attention and/or ways someone might try to be funny that I haven't thought of yet.

    This is why I wanted to try and make the code force things into the correct pattern no matter what, although there might be a better solution that I haven't thought of.

    The correct pattern is always

    [Asset tag of up to six digits] - Q.[Desk number]-[Desk Letter] - [The person's name]
    • The Asset tag is between 2 and six digits.
    • The Desk number always starts with Q, followed by a 3 digit number
    • Desk letter is usually a single letter [A-Z], but in a few cases the Desk Letter is a double letter such as AB or HW
    • The file name has the person's name, but I don't use it for my tracking, so the code just doesn't do anything with it.


    Here's the sheet:

    EDIT: I don't know why, but when clicking on Go Advanced the post I had written up appears completely blank.
    Attached Files Attached Files
    Last edited by Dexluther; 07-19-2018 at 01:28 AM.

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

    Re: Excel VBA Make sure a string follows a pattern and the ones that don't make them follo

    Try the pattern that user possibly name the txt file you can think of.
    Please Login or Register  to view this content.

+ 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] Inserting a row that will make formulas follow
    By OUO in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 07-22-2015, 10:32 AM
  2. Is there a way to make formulas follow cells?
    By mikey42979 in forum Excel General
    Replies: 3
    Last Post: 03-05-2015, 11:16 PM
  3. make content follow a hyperlink
    By ltjohnson in forum Excel General
    Replies: 0
    Last Post: 06-12-2012, 08:58 PM
  4. VLOOKUP - How to make cell format follow?
    By Mr. Green Genes in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-28-2009, 03:13 AM
  5. make excel close sheet and follow hyperlink
    By faireedoll in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-16-2008, 02:48 AM
  6. [SOLVED] How do I make a row or information, follow as you scroll down..?
    By Dave_86 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-11-2005, 11:06 AM
  7. How do I make the fill color of a cell on one sheet follow the co.
    By SteelerfanDan in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-08-2005, 06: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