Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: VBA Macro To Open And Modify Files By Looping Through Files In Folder

  1. #11
    Senior Member
    Join Date
    Apr 2012
    Posts
    193
    Rep Power
    13
    Hi Excel Fox

    I had a rethink. Where there are Col A is blank, then the entire row must be deleted for Eg if A7 , A11 is blank then these rows must be deleted, not hidden

    I have tried to write the code, but cannot get it to work.

    I have attached 3 CSV files-one containing raw data, one after running the macro and one showing what the data should look like after running the macro



    Your assistance in this regard is most appreciated

    HTML Code:
    Sub OpenAndModifySameFileTypes()
         
        Application.ScreenUpdating = False
        Dim strFile As String
        Dim strFileType As String
        Dim strPath As String
        Dim lngLoop As Long
        
    
        strPath = "C:\PINNACLE Journal TEMPLATES"
        strFileType = "*.csv" 'Split with semi-colon if you want to specify the file types. Example ->> "*.xls;*.jpeg;*.doc;*.gif"
         
         For lngLoop = LBound(Split(strFileType, ";")) To UBound(Split(strFileType, ";"))
            strFile = Dir(strPath & "\" & Split(strFileType, ";")(lngLoop))
            Do While strFile <> ""
                With Workbooks.Open(strPath & "\" & strFile)
                    With .Sheets(1)
                        .Range("B1:D" & .UsedRange.Rows.Count).NumberFormat = "0.00"
                     Sheets(1).Select
                   Dim j As Integer, k As Integer
    j = Range("a1").End(xlDown).Row
    For k = j To 1 Step -1
    If Cells(k, "B") = "" And Cells(k, "c") = "" Then
    Cells(k, "A").EntireRow.Delete
    End If
    Range(Cells(j + 1, "A"), Cells(j + 50, "A")).EntireRow.Delete
    Next k
                    
                    End With
                    .Close 1
                End With
                strFile = Dir
            Loop
        Next lngLoop
        strFile = vbNullString
        strFileType = vbNullString
        strPath = vbNullString
        lngLoop = Empty
         
    End Sub
    Attached Files Attached Files
    Last edited by Howardc; 08-23-2013 at 07:09 AM.

  2. #12
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Try this
    Code:
    Sub OpenAndModifySameFileTypes()     
        Application.ScreenUpdating = False
        Dim strFile As String
        Dim strFileType As String
        Dim strPath As String
        Dim lngLoop As Long
        
    
    
        strPath = "C:\PINNACLE Journal TEMPLATES"
        strFileType = "*.csv" 'Split with semi-colon if you want to specify the file types. Example ->> "*.xls;*.jpeg;*.doc;*.gif"
         
         For lngLoop = LBound(Split(strFileType, ";")) To UBound(Split(strFileType, ";"))
            strFile = Dir(strPath & "\" & Split(strFileType, ";")(lngLoop))
            Do While strFile <> ""
                With Workbooks.Open(strPath & "\" & strFile)
                    With .Sheets(1)
                        .Range("B2:D" & .UsedRange.Rows.Count).NumberFormat = "0.00"
                        .UsedRange.Sort Key1:=.Columns("A"), Order1:=1, Header:=1   
                    End With
                    .Close 1
                End With
                strFile = Dir
            Loop
        Next lngLoop
        strFile = vbNullString
        strFileType = vbNullString
        strPath = vbNullString
        lngLoop = Empty
         
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  3. #13
    Senior Member
    Join Date
    Apr 2012
    Posts
    193
    Rep Power
    13
    Hi Excelfox

    Thanks for the help, much appreciated. I need one more favour. I would like to delete 50 rows after the last value in Col A

  4. #14
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Add this line after the sorting

    Code:
    .Cells(1).Offset(.UsedRange.Rows.Count).Resize(50).EntireRow.Delete
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  5. #15
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Or

    Code:
    .Cells(.Rows.Count).End(xlup)(2).Resize(50).EntireRow.Delete
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  6. #16
    Senior Member
    Join Date
    Apr 2012
    Posts
    193
    Rep Power
    13
    Hi Excelfox

    Thanks for all the assistance, which is highly appreciated

Similar Threads

  1. Replies: 0
    Last Post: 07-07-2013, 01:52 AM
  2. Replies: 2
    Last Post: 03-12-2013, 02:57 PM
  3. Looping through Each Files and Folders
    By Rajan_Verma in forum Rajan Verma's Corner
    Replies: 0
    Last Post: 04-18-2012, 12:12 AM
  4. Get Name List of All Open Workbook Files
    By princ_wns in forum Excel Help
    Replies: 5
    Last Post: 04-07-2012, 12:18 PM
  5. Count Files In A Folder VBA
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 05-07-2011, 10:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •