+ Reply to Thread
Results 1 to 6 of 6

Thread: Need to delete all rows containing 'Christmas'!

  1. #1
    Registered User
    Join Date
    09-20-2011
    Location
    Italy
    MS-Off Ver
    Excel 2007
    Posts
    3

    Need to delete all rows containing 'Christmas'!

    Hello everybody,

    I have about 200 excel files each with numerous worksheets within each, labeled 'stock 1' to 'stock 45' for example. Now within each worksheet, i would like to delete the rows which contain specific dates around christmas (ie. 24/12/##, 25/12/##, and 26/12/##). The dates start in column A row 3.

    Does anyone out there now now to do this quickly via a macro of some sort, as doing it manually would take about six months i reckon!

    Thanks very much.
    Last edited by fergmcg; 09-22-2011 at 02:51 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Need to delete all rows containing 'Christmas'!

    Hello fergmscg,

    Welcome to the Forum!

    This macro will open each workbook with an xlsx extension in the folder you specify. it will check the only the worksheets whose name start with "Stock". Any date that is from 12/24 to 12/26 will have its row deleted.

    Because this could take some time to complete, the macro turns on Excel's status bar and display the workbook and worksheet it is currently checking. Each workbook is saved once the changes, if any, are made. The macro will continue until there are no more workbooks to open.

    Change the folder (marked in bold) to the directory you will be using. Copy and paste this code into a new VBA module in your Workbook's VBA project. Remember macros must be enabled before the code will run.

    ' Thread:  http://www.excelforum.com/excel-programming/793072-need-to-delete-all-rows-containing-christmas.html
    ' Poster:  fergmcg
    ' Written: Septemeber 20, 2011
    ' Author:  Leith Ross
    
    Sub DeleteChristmasRows()
    
      Dim Dates As Variant
      Dim FileName As String
      Dim FolderPath As String
      Dim i As Long
      Dim iDates As Variant
      Dim Rng As Range
      Dim RngEnd As Range
      Dim Wks As Worksheet
      Dim Wkb As Workbook
      
        ' Directory with workbooks to checked
          FolderPath = "C:\Documents and Settings\Admin.ADMINS\My Documents"
          
          Application.DisplayStatusBar = True
          Application.ScreenUpdating = False
          Application.Calculation = xlCalculationManual
          
            ' Add a backslash to the folder path if it is missing.
              FolderPath = IIf(Right(FolderPath, 1) <> "\", FolderPath, "\", FolderPath)
              
            ' Find the first workbook in the folder.
              FileName = Dir(FolderPath, "*.xlsx")
              
              Do While FileName <> ""
              
                ' Open each workbook in the specified folder.
                  Set Wkb = Workbooks.Open(FileName:=FilePath & FileName)
                  
                      For Each Wks In Wkb
                      
                      ' Look only at worksheets whose names start with Stock.
                        If LCase(Wks.Name) Like "stock*" Then
                        
                         ' Update the StatusBar
                           Application.StatusBar = "Checking Workbook: " & Wkb.Name & "  Current Sheet: " & Wks.Name
                           
                           Set Rng = Wks.Range("A3")
                           Set RngEnd = Wks.Cells(Rows.Count, "A").End(xlUp)
                           If RngEnd.Row < Rng.Row Then Exit For Else Dates = Wks.Range(Rng, RngEnd).Value
                               
                             ' Check the dates.
                               For i = UBound(Dates) To 1 Step -1
                                 iDate = Dates(i, 1)
                                 
                               ' Delete the row if the month is December and the day is between from 24 to 26.
                                 If Month(iDate) = 12 Then
                                    If Day(iDate) >= 24 And Day(iDate) <= 26 Then
                                     ' Change the date index number to its worksheet row number.
                                       Wks.Rows(i + 3 - 1).EntireRow.Delete Shift:=xlShiftUp
                                    End If
                                 End If
                                 
                               Next i
                               
                        End If
                        
                      Next Wks
                      
                  Wkb.Close SaveChanges:=True
                  FileName = Dir()
              Loop
              
          Application.DisplayStatusBar = False
          Application.ScreenUpdating = True
          Application.Calculation = xlCalculationAutomatic
          
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    09-20-2011
    Location
    Italy
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Need to delete all rows containing 'Christmas'!

    Good afternoon/good morning Ross!

    First of all thanks for the prompt reply. Much appreciated indeed. Excuse my general ingorance as I'm new to this stuff! Anyway, I tired your code in a new module within my vba project including my specific changes but i recieved the following error message when I debugged "Compile Error: Argument not optional" at the line "FolderPath = IIf(Right(FolderPath, 1)".

    I've attached a notepad file of the code I tried to run for your viewing pleasure. I'm a bit clueless as to what this means really!

    The workbooks have been saved in the "*.xlsm" format in order to enable macros. Was i correct to do this or would the "*.xls" format be more appropriate?

    Cheers for all the help, and also is there a way in which i can dontate something to the site as it might prove to be a bit of a 'life saver'!

    fergmcg
    Attached Files Attached Files

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Need to delete all rows containing 'Christmas'!

    Hello fergmcg,

    Sorry about that. I should have checked my typing. The line should be...
        FolderPath = IIf(Right(FolderPath, 1) <> "\", FolderPath & "\", FolderPath)
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    09-20-2011
    Location
    Italy
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Need to delete all rows containing 'Christmas'!

    Ok i tried the code again but now get a "Run-time '13' error: Type mismatch" message.

    The next line at - FileName = Dir(FolderPath, "1999.xlsm") - shows up in yellow when i debug.

    Anyway thanks again for all your help so far and hopefully we can get this solved. On an unrelated note, lets hope the scots thrash the english next week in the rugby!

    Sub DeleteChristmasRows()
    
      Dim Dates As Variant
      Dim FileName As String
      Dim FolderPath As String
      Dim i As Long
      Dim iDates As Variant
      Dim Rng As Range
      Dim RngEnd As Range
      Dim Wks As Worksheet
      Dim Wkb As Workbook
      
        ' Directory with workbooks to checked
          FolderPath = "C:\Users\Fergus\Dropbox\latest work\IPO carve out and demerger\data\company data"
          
          Application.DisplayStatusBar = True
          Application.ScreenUpdating = False
          Application.Calculation = xlCalculationManual
          
              
            ' Add a backslash to the folder path if it is missing.
              FolderPath = IIf(Right(FolderPath, 1) <> "\", FolderPath & "\", FolderPath)
                      
            ' Find the first workbook in the folder.
              FileName = Dir(FolderPath, "*.xlsm")
              
              Do While FileName <> ""
              
                ' Open each workbook in the specified folder.
                  Set Wkb = Workbooks.Open(FileName:=FilePath & FileName)
                  
                      For Each Wks In Wkb
                      
                      ' Look only at worksheets whose names start with Stock.
                        If LCase(Wks.Name) Like "stock*" Then
                        
                         ' Update the StatusBar
                           Application.StatusBar = "Checking Workbook: " & Wkb.Name & "  Current Sheet: " & Wks.Name
                           
                           Set Rng = Wks.Range("A3")
                           Set RngEnd = Wks.Cells(Rows.Count, "A").End(xlUp)
                           If RngEnd.Row < Rng.Row Then Exit For Else Dates = Wks.Range(Rng, RngEnd).Value
                               
                             ' Check the dates.
                               For i = UBound(Dates) To 1 Step -1
                                 iDate = Dates(i, 1)
                                 
                               ' Delete the row if the month is December and the day is between from 24 to 26.
                                 If Month(iDate) = 12 Then
                                    If Day(iDate) >= 24 And Day(iDate) <= 26 Then
                                     ' Change the date index number to its worksheet row number.
                                       Wks.Rows(i + 3 - 1).EntireRow.Delete Shift:=xlShiftUp
                                    End If
                                 End If
                                 
                               Next i
                               
                        End If
                        
                      Next Wks
                      
                  Wkb.Close SaveChanges:=True
                  FileName = Dir()
              Loop
              
          Application.DisplayStatusBar = False
          Application.ScreenUpdating = True
          Application.Calculation = xlCalculationAutomatic
    
    End Sub

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Need to delete all rows containing 'Christmas'!

    Hello fergmcg,

    I have checked the macro for any other typos and it should be good to go. The last problem was the comma should have been an ampersand. You can't concatenate the file extension with a comma.

    Sub DeleteChristmasRows()
    
      Dim Dates As Variant
      Dim FileName As String
      Dim FolderPath As String
      Dim i As Long
      Dim iDates As Variant
      Dim Rng As Range
      Dim RngEnd As Range
      Dim Wks As Worksheet
      Dim Wkb As Workbook
      
        ' Directory with workbooks to checked
          FolderPath = "C:\Users\Fergus\Dropbox\latest work\IPO carve out and demerger\data\company data"
          
          Application.DisplayStatusBar = True
          Application.ScreenUpdating = False
          Application.Calculation = xlCalculationManual
          
              
            ' Add a backslash to the folder path if it is missing.
              FolderPath = IIf(Right(FolderPath, 1) <> "\", FolderPath & "\", FolderPath)
                      
            ' Find the first workbook in the folder.
              FileName = Dir(FolderPath & "*.xlsm")
              
              Do While FileName <> ""
              
                ' Open each workbook in the specified folder.
                  Set Wkb = Workbooks.Open(FileName:=FilePath & FileName)
                  
                      For Each Wks In Wkb
                      
                      ' Look only at worksheets whose names start with Stock.
                        If LCase(Wks.Name) Like "stock*" Then
                        
                         ' Update the StatusBar
                           Application.StatusBar = "Checking Workbook: " & Wkb.Name & "  Current Sheet: " & Wks.Name
                           
                           Set Rng = Wks.Range("A3")
                           Set RngEnd = Wks.Cells(Rows.Count, "A").End(xlUp)
                           If RngEnd.Row < Rng.Row Then Exit For Else Dates = Wks.Range(Rng, RngEnd).Value
                               
                             ' Check the dates.
                               For i = UBound(Dates) To 1 Step -1
                                 iDate = Dates(i, 1)
                                 
                               ' Delete the row if the month is December and the day is between from 24 to 26.
                                 If Month(iDate) = 12 Then
                                    If Day(iDate) >= 24 And Day(iDate) <= 26 Then
                                     ' Change the date index number to its worksheet row number.
                                       Wks.Rows(i + 3 - 1).EntireRow.Delete Shift:=xlShiftUp
                                    End If
                                 End If
                                 
                               Next i
                               
                        End If
                        
                      Next Wks
                      
                  Wkb.Close SaveChanges:=True
                  FileName = Dir()
              Loop
              
          Application.DisplayStatusBar = False
          Application.ScreenUpdating = True
          Application.Calculation = xlCalculationAutomatic
    
    End Sub

    As for the rugby match, my comment is "Alba gu brąth!"

    Also for the true rugger don't forget these essentials bum wash and willy wash
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0