Looping a macro

Scott Browner

New Member
Joined
Mar 28, 2017
Messages
8
I have a macro goes through a single process. I would like to add a loop to the coding to read through a list on a spreadsheet and go through the process multiple times.

Here’s what I have. “L1” and “E1” refer to cells on the spreadsheet

Sub FindFilename()
Dim FSO As Object
Dim sourcepath As String
Dim destinationPath As String
Dim fileExtn As String
Dim ActiveWorksheet As String
Dim TempFileName As String
Dim FileName As String
Dim FileNum As Long

FileNum = Range("L1").Value
sourcepath = ActiveWorkbook.Sheets(1).Range("E1").CurrentRegion.Value
destinationPath = "C:\Users\Jnet\Documents\~ 1ATEST\Ending Folder"

fileExtn = "*.pdf"

Set FSO = CreateObject("scripting.filesystemobject")

If FSO.FolderExists(sourcepath) = False Then
MsgBox sourcepath & " does not exist"
Exit Sub
End If

If FSO.FolderExists(destinationPath) = False Then
MsgBox sourcepath & " does not exist"
Exit Sub
End If

FSO.CopyFile Source:=sourcepath & FileNum & fileExtn, Destination:=destinationPath

copy_files_from_subfolders

MsgBox "Your files have been copied"

End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I added some additional lines to your code for a loop.

Code:
Sub DoIt()

    Dim c As Range, rng As Range, LstRw As Long, SH As Worksheet
    'other code

    Set SH = ActiveSheet    ' worksheet name
    With SH    '==================

        LstRw = .Cells(.Rows.Count, "L").End(xlUp).Row    '===================

        Set rng = .Range("L1:L" & LstRw)    '=========================

        For Each c In rng.Cells    '==============================

            FileNum = c.Value    '-------
            SourcePath = c.Offset(, -7).Value    '--------
            destinationPath = "C:\Users\Jnet\Documents\~ 1ATEST\Ending Folder"

            fileExtn = "*.pdf"

            Set FSO = CreateObject("scripting.filesystemobject")

            If FSO.FolderExists(SourcePath) = False Then
                MsgBox SourcePath & " does not exist"
                Exit Sub
            End If

            If FSO.FolderExists(destinationPath) = False Then
                MsgBox SourcePath & " does not exist"
                Exit Sub
            End If

            FSO.CopyFile Source:=SourcePath & FileNum & fileExtn, Destination:=destinationPath

            copy_files_from_subfolders

            MsgBox "Your files have been copied"

        Next c    '=================
    End With    '==================




End Sub
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,395
Members
449,081
Latest member
JAMES KECULAH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top