How to Copy only cells with data VBA

Vincitore

New Member
Joined
Dec 13, 2019
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
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:

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:

Some videos you may like

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,238
Do you want to copy columns A:D from the source files to columns B:E in the target sheet? Please clarify.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,870
Messages
5,627,359
Members
416,245
Latest member
Xterminat

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
Top