Simple VBA code stalling on END IF?

30136353

Board Regular
Joined
Aug 14, 2019
Messages
105
I have a fairly simple code to copy some cells down after a countif = 0, but some reason this just stalls and when i hit escape it throws an error to the end if?

VBA Code:
    Sub NewSpineTable()
    
    Dim I As Long, LR As Long
    
    I = 2
    
    Do While Worksheets("Network Data").Cells(I, 14) <> ""
    
    
    LR = Worksheets("Spine Summary").Range("E" & Rows.Count).End(xlUp).Row
    
    If Application.WorksheetFunction.CountIf(Worksheets("Spine Summary").Range("D2:D" & LR), Worksheets("Network Data").Cells(I, 14) = 0) Then

    MsgBox "TRUE"

    LR = Worksheets("Spine Summary").Range("E" & Rows.Count).End(xlUp).Row + 1
    Worksheets("Spine Summary").Range("A2:T3").Copy Worksheets("Spine Summary").Range("A" & LR)
    Worksheets("Spine Summary").Range("A:" & LR & ":C" & LR + 1).ClearContents
    I = I + 1
    End If
    
    Loop

    End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
If your countif is not 0, then I never gets incremented and you will be stuck in an endless loop.
 
Upvote 0
If your countif is not 0, then I never gets incremented and you will be stuck in an endless loop.
ahhhh yeh that makes sense, how do i position the "Else" and "End if", so if the countif = 0 that it just loops onto the next I? Then after it continues the overall loop? Thanks for the help

VBA Code:
    Sub NewSpineTable()
    
    Dim I As Long, LR As Long
    
    I = 2
    
    Do While Worksheets("Network Data").Cells(I, 14) <> ""

    If Application.WorksheetFunction.CountIf(Worksheets("Spine Summary").Range("D:D"), Worksheets("Network Data").Cells(I, 14) = 0) Then
    
    LR = Worksheets("Spine Summary").Range("E" & Rows.Count).End(xlUp).Row + 1
    Worksheets("Spine Summary").Range("A2:T3").Copy Worksheets("Spine Summary").Range("A" & LR)
    Worksheets("Spine Summary").Range("A:" & LR & ":C" & LR + 1).ClearContents
    Else
    I = I + 1

    End If

    Loop

    End Sub
 
Upvote 0
The I = I + 1 should be after the End If since you always want to move on to the next cell. Personally, I'd prefer to get the last row (using the same End(xlup) technique you used for LR and then just loop from 2 to that row without the Do While.
 
Upvote 0
The I = I + 1 should be after the End If since you always want to move on to the next cell. Personally, I'd prefer to get the last row (using the same End(xlup) technique you used for LR and then just loop from 2 to that row without the Do While.
Thanks, I still can't seem to it to work. If i remove the IF statement it works how it should, but Column D:D on spine summary has duplicates hence the additional IF statement. Any Idea? When I run the below code it just completes without doing anything, because the first countif does not = 0.

VBA Code:
    Sub NewSpineTable()
    
    Dim I As Long, LR As Long
    
    I = 2
    
    
    Do While Worksheets("Network Data").Cells(I, 14) <> ""

    If Application.WorksheetFunction.CountIf(Worksheets("Spine Summary").Range("D:D"), Worksheets("Network Data").Cells(I, 14) = 0) Then

    LR = Worksheets("Spine Summary").Range("E" & Rows.Count).End(xlUp).Row + 1
    Worksheets("Spine Summary").Range("A2:U3").Copy Worksheets("Spine Summary").Range("A" & LR)
    Worksheets("Spine Summary").Range("A" & LR & ":C" & LR + 1).ClearContents
    Worksheets("Spine Summary").Range("A" & LR & ":A" & LR + 1).Value = Worksheets("Network Data").Cells(I, 1)
    Worksheets("Spine Summary").Range("B" & LR & ":B" & LR + 1).Value = Worksheets("Network Data").Cells(I, 6)
    Worksheets("Spine Summary").Range("C" & LR & ":D" & LR + 1).Value = Worksheets("Network Data").Cells(I, 9)
    Worksheets("Spine Summary").Range("D" & LR & ":D" & LR + 1).Value = Worksheets("Network Data").Cells(I, 14)
    
    Else
       
    I = I + 1
    
    End If

    Loop

    MsgBox "All Spines Added!"
    
    End Sub
 
Upvote 0
As I said, the I = I + 1 needs to be after the End If, not inside the If clause.
 
Upvote 0
Please post what you tried.
 
Upvote 0
Please post what you tried.
VBA Code:
    Sub NewSpineTable()
    
    Dim I As Long, LR As Long
    
    I = 2
    
    
    Do While Worksheets("Network Data").Cells(I, 14) <> ""

    If Application.WorksheetFunction.CountIf(Worksheets("Spine Summary").Range("D:D"), Worksheets("Network Data").Cells(I, 14) = 0) Then

    LR = Worksheets("Spine Summary").Range("E" & Rows.Count).End(xlUp).Row + 1
    Worksheets("Spine Summary").Range("A2:U3").Copy Worksheets("Spine Summary").Range("A" & LR)
    Worksheets("Spine Summary").Range("A" & LR & ":C" & LR + 1).ClearContents
    Worksheets("Spine Summary").Range("A" & LR & ":A" & LR + 1).Value = Worksheets("Network Data").Cells(I, 1)
    Worksheets("Spine Summary").Range("B" & LR & ":B" & LR + 1).Value = Worksheets("Network Data").Cells(I, 6)
    Worksheets("Spine Summary").Range("C" & LR & ":D" & LR + 1).Value = Worksheets("Network Data").Cells(I, 9)
    Worksheets("Spine Summary").Range("D" & LR & ":D" & LR + 1).Value = Worksheets("Network Data").Cells(I, 14)
    
    Else
    End If
    
       
    I = I + 1

    Loop

    MsgBox "All Spines Added!"
    
    End Sub
 
Upvote 0
Just noticed you have a bracket in the wrong place in the Countif line - it should be:

Code:
If Application.WorksheetFunction.CountIf(Worksheets("Spine Summary").Range("D:D"), Worksheets("Network Data").Cells(I, 14)) = 0 Then
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,303
Members
449,078
Latest member
nonnakkong

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top