+ Reply to Thread
Results 1 to 9 of 9

Open & loop through worksheets based on filename cell value

Hybrid View

  1. #1
    Registered User
    Join Date
    02-27-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    20

    Open & loop through worksheets based on filename cell value

    It would be great if someone could please help me with a macro to open worksheets by looping through a list of cells that contain the file name and folder location and then perform a macro on each of the worksheets.

    I have the list of file names in cells C3:C20 and their related folders from D3:D20. Row 20 might change and should not be fixed.

    I would like to open the file based on the cell values of C3 and the folder address in D3, perform a task then close the file before moving onto the next cell value in C4 and D4.

    Many thanks for your help. I'm not yet at the stage where I can easily modify code but hopefully this will come with time.

  2. #2
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Open & loop through worksheets based on filename cell value

    Hi,

    This should get you started:
    Private Sub CommandButton1_Click()
    	Dim lastRow As Long
    	Dim i As Integer
    	Dim WB As Workbook
    	
    	lastRow = 20
    	
    	For i = 3 To lastRow
    		Set WB = Workbooks.Open(Me.Cells(i, "D").Value & "\" & Me.Cells(i, "C").Value)
    		
    		'Call your macro here. If need be, use WB to refer to the new workbook
    		
    		WB.Close
    		Set WB = Nothing
    	Next i
    End Sub
    Let me know

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

    Re: Open & loop through worksheets based on filename cell value

    Hi, ajryan88,

    maybe use
    		WB.Close True
    to save the workbook after manipulating. Any reason why LastRow is Long and i only Integer?

    Private Sub CommandButton1_Click()
      Dim lastRow As Long
      Dim i As Long
      Dim WB As Workbook
      
      On Error Resume Next
      For i = 3 To Range("D" & Rows.Count).End(xlUp).Row
        Set WB = Workbooks.Open(Cells(i, "D").Value & "\" & Cells(i, "C").Value)
        If Not WB Is Nothing Then
          'Call your macro here. If need be, use WB to refer to the new workbook
          
          WB.Close True
        Else
          MsgBox "Please check path & forlder for Row " & i, vbExclamation
          Exit Sub
        End If
      Next i
      Set WB = Nothing
    End Sub
    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

  4. #4
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Open & loop through worksheets based on filename cell value

    @HaHoBe:
    Your first comment: good point. I didn't test this code, it was just off the top of my head
    Your second comment: habit. That's the only reason. It should also be of type Long

    Thanks for spotting those

  5. #5
    Registered User
    Join Date
    02-27-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Open & loop through worksheets based on filename cell value

    Thank you for the quick replies.
    I am receiving an error re: a folder cannot be found. It's referring to the root directory where the macro file is located.
    Do I need to add code to set the folder location because the files I am opening are stored in another folder location?

  6. #6
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Open & loop through worksheets based on filename cell value

    Yes you will need to set the folder location, either by using absolute directories in column D ("C:\..."), or by using the ChDir("C:\...") command in VBA.

    I hope this helps

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

    Re: Open & loop through worksheets based on filename cell value

    Hi, ajryan88,

    IŽd preferrred to use the complete address including drive and path to folder, ChDir would only work if all folders are located on the same drive otherwise ChDrive needs to be included as well before ChDir would work. And IŽd stored the original drive and path in a variable in order to restore it at the end of the macro and not leave the end user in the last used folder but in the one that was assigned prior to starting the macro.

    Ciao,
    Holger

  8. #8
    Registered User
    Join Date
    02-27-2014
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Open & loop through worksheets based on filename cell value

    Thank you very much. It's working. I'll mark as resolved.

  9. #9
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Open & loop through worksheets based on filename cell value

    No worries.

    Please don't forget to click on the * next to the post(s) that have been helpful to you to say thanks to the poster(s)

+ 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. VBA to use cell contents to loop through folder to see if filename exists
    By bdunphy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-14-2013, 11:15 AM
  2. Open workbooks with variables in filename using loop
    By as1234 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-31-2011, 01:17 PM
  3. Lookup & open file based on partial filename
    By Im2bz2p345 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-11-2011, 07:04 PM
  4. [SOLVED] set filename to <filename-date> on open
    By bob engler in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-13-2006, 12:15 AM
  5. Macro to read filename in cell and open new file
    By April in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-27-2005, 10:05 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