Results 1 to 1 of 1

60 USD Need to insert rows based on conditions up to a certain date. Re-run For Loop? Help

Threaded View

  1. #1
    Registered User
    Join Date
    02-14-2015
    Location
    Chicago, USA
    MS-Off Ver
    2013
    Posts
    21

    60 USD Need to insert rows based on conditions up to a certain date. Re-run For Loop? Help

    Hello All,

    I have the code below that uses a For Next loop to insert rows given specific conditions working from the last row up. However, I really need to insert as many rows every 30 days as necessary in the same manner up to a certain date or condition - that is, I do need to skip up through the rows but in order to make all the insertions that are necessary, I would need to have the For Next loop run more than once or something of that nature.

    Essentially, rather than inserting one row per save given the conditions, as the code does now using a for loop, I need to insert all perfect point rows withing valid date conditions (as if the For loop were repeating its row scan until conditions stop it from repeating).

    I have looked high and low to this point for a solution and keep either confusing or crashing the macro. Please help me see the error of my ways? Will reply to any questions. Thank you...

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    On Error GoTo errHnd
    Application.EnableEvents = False
    Dim lastrow, chkRw As Integer
    'Determine last row with data in Column B
     lastrow = Range("B" & Rows.Count).End(xlUp).Row
    'Loop from bottom of list to Row 2 in reverse order
       For chkRw = lastrow To 2 Step -1
    'Compare the current cell to the one below it
    'If conditions met, insert a perfect point row below the current Row
         If Range("B" & chkRw) <= Date - 30 _
         And Range("M" & chkRw) < 4 _
         And Range("N" & chkRw) = 0 _
         And Range("B" & chkRw) <> Range("B" & chkRw + 2) - 60 _
         Then
            Range("B" & chkRw).EntireRow.Copy
            Range("B" & chkRw + 1).EntireRow.Insert Shift:=xlDown
         'Put PerfectPoint value in column C
        Range("C" & chkRw + 1) = "00"
            'Put REC value in column N
        Range("N" & chkRw) = 1
        'Put date in Column B of inserted Row
          Cells(chkRw + 1, 2) = Cells(chkRw, 2) + 30
         ElseIf Range("B" & chkRw) <= Date - 30 _
         And Range("M" & chkRw) < 4 _
         And Range("N" & chkRw) = 0 _
         And Range("B" & chkRw + 1) = "" _
         And Range("B" & chkRw) <> Range("B" & chkRw + 2) - 60 _
         Then
            Range("B" & chkRw).EntireRow.Copy
            Range("B" & chkRw + 1).EntireRow.Insert Shift:=xlDown
            'Put PerfectPoint value in column C
        Range("C" & chkRw + 1) = "00"
            'Put REC value in column N
        Range("N" & chkRw) = 1
        'Put date in Column B of inserted Row
          Cells(chkRw + 1, 2) = Cells(chkRw, 2) + 30
        If Cells(chkRw, 3) = "16" Then
        Range("B" & chkRw + 1).EntireRow.Delete
            End If
                End If
             Next
            'Sort by Date
       Range("A1").CurrentRegion.Sort _
          Key1:=Range("B1"), Order1:=xlAscending, _
          Header:=xlYes
    'Decrement the counter and do it again
    Range("j3:M3").AutoFill Destination:=Range("j3:M" & Range("A" & Rows.Count).End(xlUp).Row)
    errHnd:
    'Re-enable event
    Application.EnableEvents = True
    End Sub


    Paypal to Ozgrid Unique Transaction ID # 49P9309681906281V
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Need to insert rows based on conditions up to a certain date. Re-run For Loop? Help?
    By treschouette in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-03-2015, 03:04 PM
  2. Insert rows in conditions
    By chris4204 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-02-2015, 04:13 AM
  3. How to insert rows+add based on the conditions
    By RookieOfTheYear in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-03-2012, 09:45 AM
  4. loop through rows for match insert blank rows with title
    By reeyu in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-27-2012, 07:34 AM
  5. Loop based on a couple of conditions
    By h_aesa1 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-31-2010, 01:22 PM

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.6.0 RC 1