Loop through Rows

rollingzep

Board Regular
Joined
Nov 18, 2013
Messages
224
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have this code which loops through all the files in a folder and copies the rows and pastes the values into the Target worksheet of the new Workbook.
It works well.
But this code reads only the first row values from all the files. How do I read all the rows in all the files? How to specify the range?

Const FOLDER_PATH = "S:\IT\GP\DigitalSubscription\Users\"

VBA Code:
Sub ImportWorksheets()
   '=============================================
   'Process all Excel files in specified folder
   '=============================================
   Dim sFile As String           'file to process
   Dim wsTarget As Worksheet
   Dim wbSource As Workbook
   Dim wsSource As Worksheet
   Dim rowTarget As Long         'output row
  
   rowTarget = 2
  
   'check the folder exists
   If Not FileFolderExists(FOLDER_PATH) Then
      MsgBox "Specified folder does not exist, exiting!"
      Exit Sub
   End If
  
   'reset application settings in event of error
   On Error GoTo errHandler
   Application.ScreenUpdating = False
  
   'set up the target worksheet
   Set wsTarget = Sheets("Existing Users")
  
   'loop through the Excel files in the folder
   sFile = Dir(FOLDER_PATH & "*.xls*")
   Do Until sFile = ""
      
      'open the source file and set the source worksheet - ASSUMED WORKSHEET(1)
      Set wbSource = Workbooks.Open(FOLDER_PATH & sFile)
      Set wsSource = wbSource.Worksheets(1) 'EDIT IF NECESSARY
      
      'import the data
      With wsTarget
         .Range("A" & rowTarget).Value = wsSource.Range("C2").Value
         .Range("B" & rowTarget).Value = wsSource.Range("A2").Value
         .Range("C" & rowTarget).Value = wsSource.Range("B2").Value
        
         'optional source filename in the last column
         .Range("N" & rowTarget).Value = sFile
      End With
      
      'close the source workbook, increment the output row and get the next file
      wbSource.Close SaveChanges:=False
      rowTarget = rowTarget + 1
      sFile = Dir()
   Loop
  
errHandler:
   On Error Resume Next
   Application.ScreenUpdating = True
  
   'tidy up
   Set wsSource = Nothing
   Set wbSource = Nothing
   Set wsTarget = Nothing
End Sub

TIA
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
So, it looks like you are wanting to copy over columns A, B, and C.
For every row in your source file that has data, will all three columns ALWAYS have data in the last row with data, or just certain columns?
We are just trying to determine the best column to look at in the source file to determine where the data ends.
 
Upvote 0
So, it looks like you are wanting to copy over columns A, B, and C.
For every row in your source file that has data, will all three columns ALWAYS have data in the last row with data, or just certain columns?
We are just trying to determine the best column to look at in the source file to determine where the data ends.
all the three contain data in all sheets
 
Upvote 0
Try this code:
VBA Code:
Sub ImportWorksheets()
   '=============================================
   'Process all Excel files in specified folder
   '=============================================
   Dim sFile As String           'file to process
   Dim wsTarget As Worksheet
   Dim wbSource As Workbook
   Dim wsSource As Worksheet
   Dim lrS As Long
   Dim lrT As Long
  
   lrT = 1
  
   'check the folder exists
   If Not FileFolderExists(FOLDER_PATH) Then
      MsgBox "Specified folder does not exist, exiting!"
      Exit Sub
   End If
  
   'reset application settings in event of error
'   On Error GoTo errHandler
   Application.ScreenUpdating = False
  
   'set up the target worksheet
   Set wsTarget = Sheets("Existing Users")
  
   'loop through the Excel files in the folder
   sFile = Dir(FOLDER_PATH & "*.xls*")
   Do Until sFile = ""
      
      'open the source file and set the source worksheet - ASSUMED WORKSHEET(1)
      Set wbSource = Workbooks.Open(FOLDER_PATH & sFile)
      Set wsSource = wbSource.Worksheets(1) 'EDIT IF NECESSARY
      
      'Find last row in column A with data
      lrS = wsSource.Cells(Rows.Count, "A").End(xlUp).Row
      
      'import the data
      With wsTarget
         wsSource.Range(Cells(2, "C"), Cells(lrS, "C")).Copy .Cells(lrT + 1, "A")
         wsSource.Range(Cells(2, "A"), Cells(lrS, "A")).Copy .Cells(lrT + 1, "B")
         wsSource.Range(Cells(2, "B"), Cells(lrS, "B")).Copy .Cells(lrT + 1, "C")
        
         'optional source filename in the last column
         .Range(.Cells(lrT + 1, "N"), .Cells(lrT + lrS - 1, "N")).Value = sFile
      End With
      
      'close the source workbook, increment the output row and get the next file
      wbSource.Close SaveChanges:=False
      lrT = wsTarget.Cells(Rows.Count, "A").End(xlUp).Row
      sFile = Dir()
   Loop
  
errHandler:
   On Error Resume Next
   Application.ScreenUpdating = True
  
   'tidy up
   Set wsSource = Nothing
   Set wbSource = Nothing
   Set wsTarget = Nothing
   
End Sub
 
Upvote 0
Solution
Try this code:
VBA Code:
Sub ImportWorksheets()
   '=============================================
   'Process all Excel files in specified folder
   '=============================================
   Dim sFile As String           'file to process
   Dim wsTarget As Worksheet
   Dim wbSource As Workbook
   Dim wsSource As Worksheet
   Dim lrS As Long
   Dim lrT As Long
 
   lrT = 1
 
   'check the folder exists
   If Not FileFolderExists(FOLDER_PATH) Then
      MsgBox "Specified folder does not exist, exiting!"
      Exit Sub
   End If
 
   'reset application settings in event of error
'   On Error GoTo errHandler
   Application.ScreenUpdating = False
 
   'set up the target worksheet
   Set wsTarget = Sheets("Existing Users")
 
   'loop through the Excel files in the folder
   sFile = Dir(FOLDER_PATH & "*.xls*")
   Do Until sFile = ""
    
      'open the source file and set the source worksheet - ASSUMED WORKSHEET(1)
      Set wbSource = Workbooks.Open(FOLDER_PATH & sFile)
      Set wsSource = wbSource.Worksheets(1) 'EDIT IF NECESSARY
    
      'Find last row in column A with data
      lrS = wsSource.Cells(Rows.Count, "A").End(xlUp).Row
    
      'import the data
      With wsTarget
         wsSource.Range(Cells(2, "C"), Cells(lrS, "C")).Copy .Cells(lrT + 1, "A")
         wsSource.Range(Cells(2, "A"), Cells(lrS, "A")).Copy .Cells(lrT + 1, "B")
         wsSource.Range(Cells(2, "B"), Cells(lrS, "B")).Copy .Cells(lrT + 1, "C")
      
         'optional source filename in the last column
         .Range(.Cells(lrT + 1, "N"), .Cells(lrT + lrS - 1, "N")).Value = sFile
      End With
    
      'close the source workbook, increment the output row and get the next file
      wbSource.Close SaveChanges:=False
      lrT = wsTarget.Cells(Rows.Count, "A").End(xlUp).Row
      sFile = Dir()
   Loop
 
errHandler:
   On Error Resume Next
   Application.ScreenUpdating = True
 
   'tidy up
   Set wsSource = Nothing
   Set wbSource = Nothing
   Set wsTarget = Nothing
 
End Sub
It worked perfectly!
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,148
Members
452,615
Latest member
bogeys2birdies

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