How to fix excel script that freezes in lloop?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tyler Hakes
    New Member
    • Feb 2011
    • 1

    How to fix excel script that freezes in lloop?

    Writing this code to help analyze and re-organize our e-mail promotions and every time I try to run the script, it just freezes up Excel (infinite loop somewhere?). It's pulling data from another sheet ("Promos") in the same workbook.

    Code:
    Sub Update_Campaign_Breakdown()
    Dim rownum As Integer
    Dim campaign As String
    Dim newrows As Integer
    Dim i As Integer
    rownum = 2
    Do Until IsEmpty(Sheets("Promos").Cells(rownum, 1))
        campaign = Sheets("Promos").Cells(rownum, 6).Value
        newrows = Application.CountA(Range("A:A")) + Application.CountA(Range("B:B"))
        For i = 1 To newrows
            If Cells(i, 1).Value = campaign Then
            Cells(i).Offset(1).EntireRow.Insert
            Cells(i + 1, 2).Value = Sheets("Promos").Cells(rownum, 1)
            Cells(i + 1, 3).Value = Sheets("Promos").Cells(rownum, 7)
            Cells(i + 1, 4).Value = Sheets("Promos").Cells(rownum, 8)
            Cells(i + 1, 5).Value = Sheets("Promos").Cells(rownum, 9)
            Cells(i + 1, 6).Value = Sheets("Promos").Cells(rownum, 10)
            Cells(i + 1, 7).Value = Sheets("Promos").Cells(rownum, 11)
            rownum = rownum + 1
            Exit For
            Else
            If i = newrows Then
            Cells(i).Offset(1).EntireRow.Insert
            Cells(i).Offset(1).EntireRow.Insert
            Cells(i + 1, 1).Value = campaign
            Cells(i + 2, 2).Value = Sheets("Promos").Cells(rownum, 1)
            Cells(i + 2, 3).Value = Sheets("Promos").Cells(rownum, 7)
            Cells(i + 2, 4).Value = Sheets("Promos").Cells(rownum, 8)
            Cells(i + 2, 5).Value = Sheets("Promos").Cells(rownum, 9)
            Cells(i + 2, 6).Value = Sheets("Promos").Cells(rownum, 10)
            Cells(i + 2, 7).Value = Sheets("Promos").Cells(rownum, 11)
            rownum = rownum + 1
            Exit For
            End If
            End If
            Next i
    Loop
    End Sub
    I'll try to summarize how it's SUPPOSED to work.

    Basically it's going down the line and looking at each entry in the Promo's table. Then, it's looking at one of the columns (Campaign) and seeing if that Campaign already exists inside of the new sheet (I'm executing the script from a button on this sheet).

    I want to have it organize the campaigns like so:

    Campaign 1
    > Email 1
    > Email 2
    Campaign 2
    > Email 1
    > Email 2

    ....and so on. So, the script is trying to see if there's already a slot where that campaign exists. If it finds the campaign, it should insert a row after the campaign, and then copy the data from the Promos sheet into the new sheet.

    If it DOESN'T find that campaign in the break-down sheet, then it should add two rows: One for the new campaign label and one for the e-mail that falls under that Campaign.

    Lastly, it should go back to the Promos table and move onto the next entry and repeat.

    Any insight into why this just locks up Excel?
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Without looking any further than Line #7, it appears that you may be using the wrong Function (IsEmpty()) at the start of your Do Until...Loop. The IsEmpty() Function is used to determine if individual Variables are Initialized, as in:
    Code:
    Dim MyVar, MyCheck
    MyCheck = IsEmpty(MyVar)    ' Returns True, MyVar NOT Initialized.
    
    MyVar = Null    ' Assign Null.
    MyCheck = IsEmpty(MyVar)    ' Returns False, is Initialized.

    Comment

    • beacon
      Contributor
      • Aug 2007
      • 579

      #3
      I think ADezii is right. You should probably be using IsNull if you want to check for a value.

      If ADezii's suggestion doesn't work out, can you post your spreadsheet?

      Comment

      Working...