I have this code that works fine before, now the source csv file data is arranged in descending order, how to add code to sort the csv file to ascending order first, before extracting the last no. rows of data,
some csv are having header in row 1 (Date Time ......) and some don't have header, how to ignore/delete the header before applying the codes.
How to modify the following code, thanks
Option Explicit
Sub ConvertCSVs()
Dim wkbDest As Workbook
Dim wksDest As Worksheet
Dim numrows As Integer
Dim startrows As Integer
Dim linrows As Integer
Dim strPath As String
Dim strFile As String
Dim strData As String
Dim X As Variant
Dim Cnt As Long
Dim r As Long
Dim c As Long
Dim i As Long
Application.ScreenUpdating = False
strPath = "D:\Docs\CSV\" ' Change Path of files
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
strFile = Dir(strPath & "*.csv")
Do While Len(strFile) > 0
Cnt = Cnt + 1
If Cnt = 1 Then
Set wkbDest = Workbooks.Add(xlWBATWorksheet)
End If
Open strPath & strFile For Input As #1
numrows = 0
Do Until EOF(1)
Line Input #1, strData
numrows = numrows + 1
Loop
Close #1
startrows = numrows - 1000 'Extract last 1000 Rows from CSV files
Open strPath & strFile For Input As #1
Set wksDest = wkbDest.Worksheets.Add
wksDest.Name = Left(strFile, InStr(1, strFile, ".csv") - 1)
r = 2
c = 1
linrows = 0
Do Until EOF(1)
Line Input #1, strData
linrows = linrows + 1
If linrows > startrows Then
X = Split(strData, ",")
For i = LBound(X) To UBound(X)
Cells(r, c).Value = X(i)
c = c + 1
Next i
r = r + 1
c = 1
End If
Loop
Close #1
strFile = Dir
Loop
If Cnt > 0 Then
Application.DisplayAlerts = False
wkbDest.Worksheets(wkbDest.Worksheets.Count).Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "Completed...", vbInformation
Else
Application.ScreenUpdating = True
MsgBox "No CSV files found...", vbExclamation
End If
End Sub
Bookmarks