Hello everyone,
Disclaimer: i´ve found the code below on this forum, posted by bertie on a similar topic. (many thanks bertie)
I only want to adjust it a bit, but my VBA skills are close to 2 (out of 100).
What i´m facing is that i´d like to copy the entire column from the workbooks(csv format) in the folder, not only the specific cells as the code does.
of course before copying i´d like to check if there is anything in the cells and then copy.
the source files don´t always have the same number of rows filled in, that´s why i reffered to copying the entire column data to be more precise
i´m pretty sure this is very simple and i would really, really appreciate your support to adjust it:
Disclaimer: i´ve found the code below on this forum, posted by bertie on a similar topic. (many thanks bertie)
I only want to adjust it a bit, but my VBA skills are close to 2 (out of 100).
What i´m facing is that i´d like to copy the entire column from the workbooks(csv format) in the folder, not only the specific cells as the code does.
of course before copying i´d like to check if there is anything in the cells and then copy.
the source files don´t always have the same number of rows filled in, that´s why i reffered to copying the entire column data to be more precise
i´m pretty sure this is very simple and i would really, really appreciate your support to adjust it:
VBA Code:
Option Explicit
Const FOLDER_PATH = "C:\temp\Users\User1\My Documents\AHT Tracker\" 'REMEMBER END BACKSLASH
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("Tracker")
'loop through the CSV files in the folder
sFile = Dir(FOLDER_PATH & "*.csv*")
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("B4").Value
.Range("B" & rowTarget).Value = wsSource.Range("C4").Value
.Range("C" & rowTarget).Value = wsSource.Range("D4").Value
.Range("D" & rowTarget).Value = wsSource.Range("E4").Value
'optional source filename in the last column
.Range("G" & rowTarget).Value = Left(sFile, 2)
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
Private Function FileFolderExists(strPath As String) As Boolean
If Not Dir(strPath, vbDirectory) = vbNullString Then FileFolderExists = True
End Function
Last edited by a moderator: