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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Forum statistics

Threads
1,214,982
Messages
6,122,575
Members
449,089
Latest member
Motoracer88

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