I have a log file in CSV format that I need to truncate periodically. What I am looking for is a way to search for the last entry in the file, get the row number for the record, then keep only the previous 100 rows, and delete the remaining rows. I need this to run on a once a week basis.
something like this
search column A for Null
find row number for that Null entry
create an array for that row number -101 rows to create an array of the last 100 rows of data
delete all rows outside of that array - leaving only the last 100 rows.
I have changed my approach and instead am using VBS (since a macro can't be saved in a CSV file). I am asking the code to check cell A450 for any content. If there is something there, then rows 1-250 should be deleted.
Option Explicit
Dim Val
Dim ExcelApp
Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.Visible = True
ExcelApp.Workbooks.Open("c:\CodeTest\P16UPSHEAD_In.csv")
Val = ExcelApp.ActiveSheet.Range("A450")
If Val <> "" Then
ExcelApp.ActiveSheet.Range("A1:A250").EntireRow.Delete
End If
ExcelApp.Workbooks.Save("c:\CodeTest\P16UPSHEAD_In.csv")
The problem is that the file is opening as Read-Only - so the Save command is not working. What am I doing wrong?