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
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