+ Reply to Thread
Results 1 to 5 of 5

Variable in path causing problems in loop through files in a folder

  1. #1
    Forum Contributor
    Join Date
    04-07-2009
    Location
    Victoria, Australia
    MS-Off Ver
    365l
    Posts
    170

    Variable in path causing problems in loop through files in a folder

    I'm trying to use the sheet name (Year) in code to open all files from that year. The sub "All Files" works and I know I could simply change the year. But I've spent so much time trying to get "All Meetings" to work, it's frustrating me. The current edition seems to run, but nothing happens. I've had it take me to the correct folder, but then tell me that the first file in that folder couldn't be found. It's my use of "Year" & "MyFolder" I think, but What am I doing wrong?

    Any help appreciated

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

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

    Cool Try this !


    You are just confusing a string and a variable so your path is wrong as it does not exist !

    So removing the useless a VBA demonstration for starters :

    PHP Code: 
    Sub Demo1()
        
    Dim P$, F$
            
    "C:\Racing\Racing " ActiveSheet.Name "\System 1\Races\"
            F = Dir$(P & "
    *.xls*")
      While F > ""
        With Workbooks.Open(P & F, 0)
           'Do stuff
           .Close True
        End With
            F = Dir$
      Wend
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  3. #3
    Forum Contributor
    Join Date
    04-07-2009
    Location
    Victoria, Australia
    MS-Off Ver
    365l
    Posts
    170

    Re: Try this !

    Thank you Marc.

    That works (of course) but I haven't learnt anything. Can you tell me what $ does please and the zero in workbooks.open? The rest I follow.

    Steve

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

    Arrow Re: Variable in path causing problems in loop through files in a folder


    First, thanks for the rep' !

    The zero is an argument well explained in VBA help, a must read !

    $ is the old fashion string variable type character declaration like you can see too in VBA help …

    As it seems you do not want to see the obvious so according to your codeline MyPath = "C:\Racing\MyFolder\System 1\Races\"
    does the folder 'C:\Racing\MyFolder' exists ?
    Obviously not as you are confusing a string between double quotes and a variable !
    And you already have the solution within post #2 how to incorporate a variable part within a string …

  5. #5
    Forum Contributor
    Join Date
    04-07-2009
    Location
    Victoria, Australia
    MS-Off Ver
    365l
    Posts
    170

    Re: Variable in path causing problems in loop through files in a folder

    Alright. Thanks.

+ 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. move files from Source Folder to Destination Folder, based on file name & Path
    By seenai in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-31-2020, 09:29 AM
  2. [SOLVED] Assign Folder path to string variable
    By ImranBhatti in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-04-2018, 01:07 PM
  3. [SOLVED] Loop Through Folder, Create Emails with Sub Folder Names in Subject, Attach files in sub
    By Rschwar23 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-30-2015, 10:06 AM
  4. Replies: 12
    Last Post: 03-09-2015, 05:52 PM
  5. [SOLVED] Copy files from one folder to another folder (path in textbox)
    By yoko21 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-24-2014, 02:45 AM
  6. [SOLVED] Create txt file in folder using variable as path & name
    By Orada in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-04-2012, 11:07 PM
  7. Problems with variable path
    By isty_szy in forum Excel General
    Replies: 2
    Last Post: 01-19-2011, 01:55 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