Compare/Check Previous Cell Value Before Importing CSV File

kumail666

New Member
Joined
Oct 16, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am using a macro to import a CSV file into my master excel file where I am performing some analysis on data imported from the source CSV file. I am importing each CSV file one by one and performing analysis before importing the next one. However, often I am finding it difficult to see if the next CSV file has already been imported or not (CSV files are saved in a folder and there could be 20 or more - each one saved with time and data stamp as a filename for e.g. CSV_2022-09-26_002315).

The first cell of each of the CSV files contains the date and time stamp as well. I am using the following code to import the CSV file into a new sheet in my master document. Please see below:

Sub readExcelData(sTheSourceFile)
On Error GoTo ErrHandler
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False

Dim src As Workbook
Set src = Workbooks.Open(sTheSourceFile, True, True)

Dim iRowsCount As Integer
iRowsCount = src.Worksheets(1).UsedRange.Rows.Count

Dim iColumnsCount As Integer
iColumnsCount = src.Worksheets(1).UsedRange.Columns.Count

Dim iRows, iCols, iStartRow As Integer
iStartRow = 0

For iRows = 1 To iRowsCount
For iCols = 1 To iColumnsCount
ThisWorkbook.Worksheets(ThisWorkbook.Sheets.Count).Cells(iRows + iStartRow, iCols) = src.Worksheets(1).Cells(iRows, iCols)
Next iCols
Next iRows

iStartRow = iRows + 1
iRows = 0

src.Close False
Set src = Nothing

ErrHandler:
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
ActiveSheet.DisplayPageBreaks = True
End Sub

However, what I am struggling to do is - that I would like to be able to check if the next CSV has already been imported by checking through the values of the previously imported CSV files. So, essentially check A1 on Sheet1, A1 on Sheet2, A1 on Sheet3 - if none of those values match the A1 of the file which is going to be imported next, only then continue with the import.

I hope my explanation makes sense, and please let me know if I can clarify any further.

Thanks all!


kumail666
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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