+ Reply to Thread
Results 1 to 5 of 5

Loop to open successive filenames...?

Hybrid View

  1. #1
    Registered User
    Join Date
    02-18-2020
    Location
    London
    MS-Off Ver
    2021
    Posts
    79

    Loop to open successive filenames...?

    Hi

    I have a speadsheet Analysis.xls which using VBA pulls in lines of totals from 12 other spreadsheets named Spending001.xls to Spending012.xls. This is a simple copy and paste exercise and the vba I am using is in the attached .txt file.

    Rather than repeating the same code 12 times, please could you suggest a loop that would increment the filename (001-012) each time around? I do understand simple loops, but this is giving me problems.

    Thanks for your advice
    Robin
    Attached Files Attached Files

  2. #2
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Loop to open successive filenames...?

    Something like this (albeit untested)
    Sub test()
    
    Dim WBS As Workbook
    Dim str As String
    Dim filename As String
    
    Range("J5").Select
    
    '
        
    '
    For i = 1 To 12
        str = Format(CStr(i), "000")
        filename = "D:\Spending" & str & ".xlsx"
    
        Set WBS = Workbooks.Open(filename)
        WBS.Activate
        Range("AJ372:CU372").Select
        Selection.Copy
        Windows(Analysis.xlsm).Activate
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Selection.Offset(1, 0).Select
    
    Next i
    
    
    End Sub

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,355

    Re: Loop to open successive filenames...?

    Hi vonsworld,

    See if this works for you. You need to make a string from those numbers. I've used CStr() but then how many "0" to add..
    Sub StrCtr()
        Dim NumCtr As Double
        Dim StrCtr As String
    
        For NumCtr = 1 To 12
            If Len(CStr(NumCtr)) = 1 Then
                StrCtr = "00" & CStr(NumCtr)
            Else
                StrCtr = "0" & CStr(NumCtr)
            End If
            
            Set WBS = Workbooks.Open("D:\Spending" & StrCtr & ".xlsx")
            ' Your Code
    
        Next NumCtr
    
    End Sub
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Registered User
    Join Date
    02-18-2020
    Location
    London
    MS-Off Ver
    2021
    Posts
    79

    Re: Loop to open successive filenames...?

    Thanks, that's great and works well.

    Please just one last question...

    How could VBA import the end of loop figure of 12 as an integer from a cell in the spreadsheet, so I can change this figure by entering it in a cell rather than editing the VB code?

    Thanks
    Robin

  5. #5
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Loop to open successive filenames...?

    
    For i = 1 to Range("A1").Value

+ 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. How to loop through filenames and perform an action based on filename criteria
    By webwyzard in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 10-31-2018, 07:51 PM
  2. Pasting a range in successive columns on a loop.
    By LewisGoose in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-14-2017, 09:45 AM
  3. Replies: 1
    Last Post: 05-12-2017, 02:53 AM
  4. [SOLVED] Use Filenames In A Spreadsheet To Open Files
    By AlexRoberts in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-16-2011, 07:53 AM
  5. Loop filenames
    By Radam in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-02-2009, 09:43 AM
  6. XL97 changes filenames upon open
    By CLR in forum Excel General
    Replies: 5
    Last Post: 12-06-2005, 02:00 PM
  7. [SOLVED] Excel Visual Basic - need help to create a loop of filenames
    By dgates12 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-12-2005, 09:06 AM

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