Consulting

Results 1 to 4 of 4

Thread: I need VBA to delete lines related to date

  1. #1

    I need VBA to delete lines related to date

    I receive a daily excel table which has a variable number of lines.n be anything from 5 to 10000 lines.
    Lines 1 to 11 are statis (always contain the same headers etc).
    Line 12 onwards contain the data.
    There are a list of dates in column U
    I would like VBA to delete any lines from line 5 onwards whose date in col U is not tomorrow.

  2. #2
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    Bing search "Excel VBA delete rows".
    Review https://analysistabs.com/vba/delete-...-example-code/
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,061
    Location
    maybe try this version
    Sub sbDelete_Rows _Date()
    Dim lRow As Long
    Dim iCntr As Long
    lRow = 10000
    For iCntr = lRow To 12 Step -1
        If Cells(iCntr, 21) = Today() Then
            Rows(iCntr).Delete
        End If
    Next
    End Sub
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    If you have a lot of rows, a more complicated approach would offer performance improvements


    Option Explicit
    
    
    Sub DeleteSomeRows()
        Dim r1 As Range, r2 As Range, r As Range, r12 As Range
        Dim tomm As Date
        
        Application.ScreenUpdating = False
        
        With ActiveSheet
            Set r1 = .Cells(5, 21)
            Set r2 = .Cells(.Rows.Count, 21).End(xlUp)
            Set r12 = Range(r1, r2)
        End With
        
        tomm = Date + 1
        
        For Each r In r12.Cells
            With r
                If IsDate(.Value) Then
                    If Int(.Value) <> tomm Then .Value = True
                End If
            End With
        Next
        
        On Error Resume Next
        r12.SpecialCells(xlCellTypeConstants, xlLogical).EntireRow.Delete
        On Error GoTo 0
    
    
        Application.ScreenUpdating = True
    
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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