Delete rows in multiple CSV files

davidp13

New Member
Joined
Jun 7, 2011
Messages
25
Good day,

I'm looking for some guidance as to why a piece of code I have does not function as expected. The code ultimately opens multiple .csv files, then looks for certain date values in Column E and decides whether to delete the row or not. The code to delete the row works fine if I run it as stand alone like below:

Sub test()
Dim LR As Long
Application.ScreenUpdating = False
LR = ActiveSheet.Range("E" & Rows.Count).End(xlUp).Row
For r = LR To 2 Step -1
If ((Weekday(Date) >= 3 And Weekday(Date) <= 6 And Range("E" & r).Value <> Date - 1) Or (Weekday(Date) = 2 And _
Range("E" & r).Value <> Date - 3)) Then
Rows(r).Delete
Application.ScreenUpdating = True
End If
Next r
End Sub

However, when I add it to the code that looks through a certain folder for multiple .csv it does not complete the operation. The code is below:

Public Sub Setup1()

Dim StrFile As String
Dim LR As Long

StrFile = Dir("C:\Users\admi n\Downloads\*.csv")
While StrFile <> ""

Workbooks.Open fileName:=StrFile
Application.ScreenUpdating = False
LR = ActiveSheet.Range("E" & Rows.Count).End(xlUp).Row
For r = LR To 2 Step -1
If (Range("E" & r).Value <> Date - 1) Then
Rows(r).Delete
Application.ScreenUpdating = True
End If
Next r
StrFile = Dir
Wend
End Sub

Can anyone perhaps explain to me why this does not work?

Thanks
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
davidp13,

Maybe it is working and you're just not seeing it. Try moving the location of the "Application.ScreenUpdating = True" line...

Code:
Public Sub Setup1()
Dim StrFile As String
Dim LR As Long

StrFile = Dir("C:\Users\admi n\Downloads\*.csv")
While StrFile <> ""
    Workbooks.Open Filename:=StrFile
    Application.ScreenUpdating = False
    LR = ActiveSheet.Range("E" & Rows.Count).End(xlUp).Row
    For r = LR To 2 Step -1
        If (Range("E" & r).Value <> Date - 1) Then
            Rows(r).Delete
        End If
    Next r
    [COLOR=#ff0000]Application.ScreenUpdating = True[/COLOR]
    StrFile = Dir
Wend
End Sub

Cheers,

tonyyy
 
Upvote 0
Thanks for the reply. I got it working by adding ActiveSheet. infront of the delete line. Works now!

Cheers

Code:
Public Sub Setup1()
Dim StrFile As String
Dim LR As Long


StrFile = Dir("C:\Users\admi n\Downloads\*.csv")
While StrFile <> ""
    Workbooks.Open Filename:=StrFile
    Application.ScreenUpdating = False
    LR = ActiveSheet.Range("E" & Rows.Count).End(xlUp).Row
    For r = LR To 2 Step -1
        If (Range("E" & r).Value <> Date - 1) Then
[COLOR=#ff0000]            ActiveSheet.Rows(r).Delete[/COLOR]
        End If
    Next r
    Application.ScreenUpdating = True
    StrFile = Dir
Wend
End Sub
 
Upvote 0
You're welcome. Glad you got it working...
 
Upvote 0
Just as a side note to whomever might find this useful. I struggled with the code to open the csv files. Got Run-time error 1004 until i update the code to open the csv files. so use the below instead.

Code:
Dim wbCSV   As Workbook
Dim fPath   As String:      fPath = "C:\Users\[add your path here]\"
Dim strFile As String


Application.ScreenUpdating = False  'speed up macro


strFile = Dir(fPath & "*.csv")


While strFile <> ""
        
        Set wbCSV = Workbooks.Open(fPath & strFile)
 
Upvote 0
Good day,

I'm looking for some guidance as to why a piece of code I have does not function as expected. The code ultimately opens multiple .csv files, then looks for certain date values in Column E and decides whether to delete the row or not. The code to delete the row works fine if I run it as stand alone like below:

Sub test()
Dim LR As Long
Application.ScreenUpdating = False
LR = ActiveSheet.Range("E" & Rows.Count).End(xlUp).Row
For r = LR To 2 Step -1
If ((Weekday(Date) >= 3 And Weekday(Date) <= 6 And Range("E" & r).Value <> Date - 1) Or (Weekday(Date) = 2 And _
Range("E" & r).Value <> Date - 3)) Then
Rows(r).Delete
Application.ScreenUpdating = True
End If
Next r
End Sub

However, when I add it to the code that looks through a certain folder for multiple .csv it does not complete the operation. The code is below:

Public Sub Setup1()

Dim StrFile As String
Dim LR As Long

StrFile = Dir("C:\Users\admi n\Downloads\*.csv")
While StrFile <> ""

Workbooks.Open fileName:=StrFile
Application.ScreenUpdating = False
LR = ActiveSheet.Range("E" & Rows.Count).End(xlUp).Row
For r = LR To 2 Step -1
If (Range("E" & r).Value <> Date - 1) Then
Rows(r).Delete
Application.ScreenUpdating = True
End If
Next r
StrFile = Dir
Wend
End Sub

Can anyone perhaps explain to me why this does not work?

Thanks


here is video how to delete multiple rows from multiple csv or excel files
https://www.youtube.com/watch?v=2mx9bZ7xz5o
 
Upvote 0

Forum statistics

Threads
1,223,937
Messages
6,175,521
Members
452,650
Latest member
Tinfish

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