+ Reply to Thread
Results 1 to 6 of 6

Varaible for file name or this file pathname

Hybrid View

  1. #1
    Registered User
    Join Date
    07-27-2012
    Location
    Sydney Aust
    MS-Off Ver
    Excel 2010
    Posts
    92

    Varaible for file name or this file pathname

    How could i get the below code to work with a variable as the file name?

    Or could I use something like "thisfile" to determine where to import to.

    
    Dim SV5 as String
    
    SV5 = My File.xlsm
    
    Do While fileName <> ""
        Workbooks.Open (directory & fileName)
            
        For Each sheet In Workbooks(fileName).Worksheets
            total = Workbooks("exportedCSV.csv").Worksheets.Count
            Workbooks(fileName).Worksheets(sheet.Name).Copy _
            After:=Workbooks("My File.xlsm").Worksheets(total)
            'After:=Workbooks("SV5").Worksheets(total)
            
        Next sheet
           
        Workbooks(fileName).Close
        fileName = Dir()
    Loop
    thanks in advance

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Varaible for file name or this file pathname

    Hi, ratdogexcel,

    ThisWorkbook could be used to rely on the workbook with the code:
    Dim wbThis As Workbook
    
    Set wbThis = ThisWorkbook
    
    Do While Filename <> ""
        Workbooks.Open (directory & Filename)
            
        For Each Sheet In Workbooks(Filename).Worksheets
            Total = Workbooks("exportedCSV.csv").Worksheets.Count
            Workbooks(Filename).Worksheets(Sheet.Name).Copy _
                After:=wbThis.Worksheets(wbThis.Worksheets.Count)
            'After:=Workbooks("SV5").Worksheets(total)
            
        Next Sheet
           
        Workbooks(Filename).Close
        Filename = Dir()
    Loop
    You should rename Sheet to a different name.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    07-27-2012
    Location
    Sydney Aust
    MS-Off Ver
    Excel 2010
    Posts
    92

    Re: Varaible for file name or this file pathname

    HaHoBe

    that works a treat. thanks

    Out of interest........... Why should I rename my sheet?

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Varaible for file name or this file pathname

    Hi, ratdogexcel,

    I for my part try to use different names for variables and leave the VBA Keywords alone because using them inside a procedure may interfere with their normal usage as the local variable sort of overwrites the VBA meaning for that procedure.

    Ciao,
    Holger

  5. #5
    Registered User
    Join Date
    07-27-2012
    Location
    Sydney Aust
    MS-Off Ver
    Excel 2010
    Posts
    92

    Re: Varaible for file name or this file pathname

    oh so you are talking about this name

    Workbooks("exportedCSV.csv").Worksheets.Count

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Varaible for file name or this file pathname

    Hi, ratdogexcel,

    not exactly, I was after
    For Each sheet In Workbooks(fileName).Worksheets
    Ciao,
    Holger

+ 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. Replies: 0
    Last Post: 08-16-2013, 12:04 PM
  2. [SOLVED] Relative file pathname in macro
    By ERoemer in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-03-2013, 07:46 AM
  3. Open 2nd file(CSV) from cell reference, copy columns to main file & close 2nd file
    By Langchop in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-31-2013, 05:09 AM
  4. Capture "Save As" Pathname and Save another File.
    By modytrane in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-10-2009, 01:24 PM
  5. Lookup file with relative pathname
    By Hall in forum Excel General
    Replies: 3
    Last Post: 11-23-2005, 04:40 PM

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