+ Reply to Thread
Results 1 to 16 of 16

Find if filename exists and update variable within the filename structure

  1. #1
    Registered User
    Join Date
    09-18-2008
    Location
    Australia
    MS-Off Ver
    2010
    Posts
    66

    Find if filename exists and update variable within the filename structure

    Hi again everyone.

    I have searched many different forums and found a number "IF FILE EXISTS" codes, but cannot get them to work in my particular situation.

    I have a userform macro that populates info from a number of public declarations. When I attempt pre-fill my variation invoices, I would like the code to check if a file already exists and update the variable contained withthin the file name.

    The filename structure consists of a job number, then "v" then variation number (the variable) and the other details.

    It ends up looking like this "3000v1 - job address", with the "1" being the variable if that variation # already exists.

    Below is what I have come up with so far but cannot get it to update the value of "k" when it places information on the invoice.

    Range "I5" on the invoice should reflect the updated (part) filename.

    Cheers in advance for taking the time to look at this.

    Martin


    Please Login or Register  to view this content.
    Last edited by Lungfish; 03-18-2013 at 02:43 AM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Find if filename exists and update variable within the filename structure

    Martin

    If v should go in I5 perhaps you should change variname to v here.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    09-18-2008
    Location
    Australia
    MS-Off Ver
    2010
    Posts
    66

    Re: Find if filename exists and update variable within the filename structure

    Hi.

    I5 should contain 3000v1.
    When file 3000v1 already exists, I5 should update to 3000v2.

    That file name, in I5, is made up of:

    3000 = pwjnum
    "v" is v for variation and remains constantly v!
    1,2,3,etc ("k") is the variation number that gets updated depending on if files exist or not.

    Updating the value of k in the code, is where i'm stuck.
    At the moment, if the file 3000v1 exists "k" won't update to 3000v2.

    Cheers,
    Martin

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Martin

    Have you tried stepping through the code with F8?

    That should give you an idea of what's happening.

  5. #5
    Registered User
    Join Date
    09-18-2008
    Location
    Australia
    MS-Off Ver
    2010
    Posts
    66

    Re: Find if filename exists and update variable within the filename structure

    mmm... That only highlights (in yellow) every line of code one after the other.

    Please Login or Register  to view this content.
    variName = pwjnum & "v" & k

    pwjnum = 3000
    v="v"

    1(k) = variable depending on previous variation invoice numbers


    Other find file functions that I have in this workbook, work fine using :
    Please Login or Register  to view this content.
    But if the file DOES exist, then the code should
    Please Login or Register  to view this content.
    This is where (I think) the problem occurs.If the file DOES exist then then code doesn't do:
    Please Login or Register  to view this content.
    .

    Which would make k=2 if the file 3000v1 existed. and there fore the new string in I5 would be 3000v2.
    If 3000v2 existed, then k=3 and I5 = 3000v3, etc.

    Cheers

    Martin
    Please Login or Register  to view this content.
    Last edited by Lungfish; 03-13-2013 at 07:45 AM.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Find if filename exists and update variable within the filename structure

    Martin

    It also shows you how the code is working, for example what's happening at the If statements.

    You can also monitor the values of the various variables in the code.

    For example you could check the value of variName as the code is being executed.

    By the way which sheet is I5 on?

    I'm asking that because there's no worksheet reference here, so it will point to whatever sheet is active when this line of code is executed.
    Please Login or Register  to view this content.
    That could mean that variName is going into I5, but not I5 on the correct worksheet.

  7. #7
    Registered User
    Join Date
    09-18-2008
    Location
    Australia
    MS-Off Ver
    2010
    Posts
    66

    Re: Find if filename exists and update variable within the filename structure

    Hi, thanks for your time Norie, but I'm not quite sure we're 'in the zone' together on this one?!?!?

    Correct. I5 Populates on the active worksheet, which is the variation invoice template. which is called by :

    Please Login or Register  to view this content.
    I5 populates with 3000v1, every time, without fail.

    Please Login or Register  to view this content.
    I5 = variName = pwjnum & "v" & k

    pwjnum is the assigned job number

    "v" simply identifies that invoice 3000v is a variation invoice

    k = variation number. (sometimes there are many variations for one job)

    On the first instance of a variation being invoiced (and saved) for a job, k=1

    if k=1 and I5 = 3000v1 & job address and... that file already exists, then k (should) = 2, so I5 = 3000v2 & job address.

    If the file [I5 (3000v1) & job address] already exists, I would like the code to identify the existing file and change I5 to 3000v2 & job address, using a procedure similar to :

    Please Login or Register  to view this content.
    I will try tomorrow with F8 though the code again and see if that sheds some light on the problem.

    Cheers

    Martin

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Find if filename exists and update variable within the filename structure

    Martin

    In the code variName is never updated.

    So it will always stay the same value as it's given here.
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    09-18-2008
    Location
    Australia
    MS-Off Ver
    2010
    Posts
    66

    Re: Find if filename exists and update variable within the filename structure

    Norie, Thanks for your time on this.

    I think we might be getting somewhere.

    variName, in the first instance (if file does not exist), k=1. Agreed?!

    When the code in one of the previous posts, finds an existing file (3000v1) and then goes to :

    Please Login or Register  to view this content.
    I thought that the new instance of variName would automatically update the value of k and become 3000v2, because variName is made up of pwjnum & "v" & k

    Do I need to re-declare variName @ "Do Until"....

    How do we update the value of k in variName?

    Cheers

    Martin
    Last edited by Lungfish; 03-15-2013 at 03:08 AM.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Martin

    It doesn't work that way.

    There is no 'instance' of variName, it's a variable and the value won't change unless you explicitly change it.

    Try repeating the line that sets variName after you've updated v.
    Last edited by Norie; 03-15-2013 at 06:04 AM.

  11. #11
    Registered User
    Join Date
    09-18-2008
    Location
    Australia
    MS-Off Ver
    2010
    Posts
    66

    Re: Find if filename exists and update variable within the filename structure

    Norie,

    "v" doesn't get updated it's just a letter used as an identifier.

    The value of k is what gets updated.
    Please Login or Register  to view this content.
    I have tried repeating the variName setting in different (relevant) locations in the code but it makes no difference.

    I've had a go at stepping through the code and it shows that the value of k = 1 throughout the code.

    When stepping with F8, it skips over the highlighted section of code below.
    Please Login or Register  to view this content.
    The line of code :
    Please Login or Register  to view this content.
    Works fine in other macros in the userform. To say that it finds the file and produces a message box "file found/exists, please try again"

    I am wondering why this part of the procedure:
    Please Login or Register  to view this content.
    either, doesn't execute or doesn't find the existing file, and update the value of k accordingly.

    Am I using the wrong kind of function for "find file" in this example?

    Cheers

    Martin
    Last edited by Lungfish; 03-16-2013 at 11:44 PM.

  12. #12
    Registered User
    Join Date
    09-18-2008
    Location
    Australia
    MS-Off Ver
    2010
    Posts
    66

    Re: Find if filename exists and update variable within the filename structure

    Norie.

    OK. I reckon I finally understood where you were pointing me.

    After getting a case of square eyes, and some very prolonged periods of daydreaming, not only did I find out that I can stare blankly for long stints but I have found a solution that works.

    And.... (Drum Roll), Here it is:
    Please Login or Register  to view this content.
    Possibly not the most elegant way to do it in the world of Pro-Coders, But it works.

    Any suggestions that would tidy it up would be welcome.

    I sincerely thank you for you generosity of time on this.

    Best regards

    Martin

  13. #13
    Registered User
    Join Date
    09-18-2008
    Location
    Australia
    MS-Off Ver
    2010
    Posts
    66

    Re: Find if filename exists and update variable within the filename structure

    Sorry duplicate post. stupid slow internet.
    Last edited by Lungfish; 03-17-2013 at 05:08 AM. Reason: duplicate post

  14. #14
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    v, k whatever, I knew it was something like that.

    The important thing was that variName was never being updated.

    I've not had a proper look at the working code but if it works then it works.

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Find if filename exists and update variable within the filename structure

    Had a proper look at the code, perhaps you could tidy it up like this.
    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    09-18-2008
    Location
    Australia
    MS-Off Ver
    2010
    Posts
    66

    Re: Find if filename exists and update variable within the filename structure

    Awesome work Norie. Thank's again.

    I can see why you're a forum guru.

    Cheers
    Martin

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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