Not looping

Ramballah

Active Member
Joined
Sep 25, 2018
Messages
334
Office Version
  1. 365
Platform
  1. Windows
Hello all,
Can someone please tell me why my code is not looping?
It should be looping normally but it's not.

Code:
Sub AddCorrectRanges()    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Dim rng1 As Range
    Dim rng2 As Range
    Dim rng3 As Range
    Dim rng4 As Range
    Dim rng5 As Range
    Dim cel As Range
    Dim i As Integer
    Dim d As Integer
    Set rng1 = Range("G2:J27") 'Entire chart
    Set rng2 = Range("I3:J14") 'Values for statistics
    Set rng3 = Range("I19:J27") 'Values for profits / losses
    Set rng4 = Range("G2:J2") 'Current Session Statistics
    Set rng5 = Range("G18:J18") 'Current Session Profits / Losses
    Set cel = Range("I3")
    For d = 0 To 5000
        For i = 0 To 4
            If d = 0 And i = 0 Then i = 1
            If cel.Offset(30 * d, 5 * i) = "" Then
                rng4.ClearContents
                rng5.ClearContents
                rng4.Value = "Session Statistics " & i + d * 5
                rng5.Value = "Session Profits / Losses " & i + d * 5
                rng1.Copy rng1.Offset(30 * d, 5 * i)
                rng2.Offset(30 * d, 5 * i).ClearContents
                rng3.Offset(30 * d, 5 * i).ClearContents
                rng2.Copy
                rng2.Offset(30 * d, 5 * i).PasteSpecial xlPasteValues
                rng3.Copy
                rng3.Offset(30 * d, 5 * i).PasteSpecial xlPasteValues
                rng4.ClearContents
                rng5.ClearContents
                rng4.Value = "Current Session Statistics"
                rng5.Value = "Current Session Profits / Losses"
                GoTo TheEnd
            End If
        Next i
    Next d
TheEnd:
Application.Calculation = xlCalculationAutomatic
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Have you tried stepping through the code with F8?

Perhaps even set up a few breakpoints with F9.
 
Upvote 0
Try this.

1 Put a breakpoint on the first line of code by placing the cursor on that line and pressing F9.

2 Press F8 to start executing the code.

3 Step through the code line by line using F8.
 
Upvote 0
Try this.

1 Put a breakpoint on the first line of code by placing the cursor on that line and pressing F9.

2 Press F8 to start executing the code.

3 Step through the code line by line using F8.
Thanks, however im not sure what F9 really does but i used the F8 button now and i figured that the GoTo TheEnd did that. but im wondering why because in my other macro i have the same stuff and there it works normally. i have 2 loops and a goto the end. but in this macro it just didnt work but in my other one it does work?
 
Upvote 0
Alright i still didn't fix it. i removed goto theend but that just makes me crash all the time. and doesnt do it 1 for 1 anymore... but if i leave it there it wont loop. wtf? Why does it work on my other macro but not this one?
 
Upvote 0
If you select a line of code and press F9 it creates a breakpoint in the code.

When executing the code execution will stop when a breakpoint is encountered.

So when debugging you can use breakpoints to pause the code and check out what's going on.

I would suggest you put a breakpoint on the first For statement, execute the code and then step through with F8 to see if the code is actually looping.
 
Upvote 0
If you select a line of code and press F9 it creates a breakpoint in the code.

When executing the code execution will stop when a breakpoint is encountered.

So when debugging you can use breakpoints to pause the code and check out what's going on.

I would suggest you put a breakpoint on the first For statement, execute the code and then step through with F8 to see if the code is actually looping.
Like i said, its not looping and i dont know why, the f8 didn't help me far. if i remove my TheEnd stuff excel crashes and it does all the loops without stopping. not what i want. i want it to go 1 for 1 not all at the same time and make my excel crash. But if i keep that theend there it wont loop. but in my other code i have almost the same code only difference being in less clearranges etc and there it works fine.
 
Upvote 0
What exactly do you mean the code isn't 'looping'?

As far as I can see there is nothing in that code that would prevent the 2 loops, well, looping.

What, in words, is the code meant to do?
 
Upvote 0
What exactly do you mean the code isn't 'looping'?

As far as I can see there is nothing in that code that would prevent the 2 loops, well, looping.

What, in words, is the code meant to do?
It's supposed to copy the main chart and put it on the offset coordinates. and rename it to chart 1. It should do this only 1 time every time i click the button. copying the mainshart to the offset position. so it starts with chart 1, then chart 2. going to the offset positions. but since my main chart uses formulas and i only need the values i have that big clearing and copy stuff going on. but its supposed to do 1 at a time. Now in my other worksheet i have a different chart but exact same code. only different ranges for my dim's. And it works perfect. So now i copied that script into this work sheet. changed the ranges and added a bit more .clearcontent line etc and copies. but nothing different than my own code which works fine
Code:
Sub CopyToCorrectRanges()    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Dim rng1 As Range
    Dim rng2 As Range
    Dim rng3 As Range
    Dim rng4 As Range
    Dim cel As Range
    Dim d As Integer
    Dim i As Integer
    Set rng1 = Range("A1:H102")
    Set rng2 = Range("B3:D102")
    Set rng3 = Range("F3:H102")
    Set rng4 = Range("A1:H1")
    Set cel = Range("B3")
    For d = 0 To 5000
        For i = 0 To 4
            If d = 0 And i = 0 Then i = 1
            If cel.Offset(103 * d, 9 * i) = "" Then
                rng4.ClearContents
                rng4.Value = "Chart " & i + d * 5
                rng1.Copy rng1.Offset(103 * d, 9 * i)
                rng2.ClearContents
                rng3.ClearContents
                rng4.ClearContents
                rng4.Value = "Main Chart"
                GoTo TheEnd
            End If
        Next i
    Next d
TheEnd:
Application.Calculation = xlCalculationAutomatic
MsgBox "Chart " & i + d * 5 & " has been succesfully made"


End Sub
Is the code that works fine ^
Is the code that is the same but doesn't work v
Code:
Sub AddCorrectRanges()    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Dim rng1 As Range
    Dim rng2 As Range
    Dim rng3 As Range
    Dim rng4 As Range
    Dim rng5 As Range
    Dim cel As Range
    Dim i As Integer
    Dim d As Integer
    Set rng1 = Range("G2:J27") 'Entire chart
    Set rng2 = Range("I3:J14") 'Values for statistics
    Set rng3 = Range("I19:J27") 'Values for profits / losses
    Set rng4 = Range("G2:J2") 'Current Session Statistics
    Set rng5 = Range("G18:J18") 'Current Session Profits / Losses
    Set cel = Range("I3")
    For d = 0 To 5000
        For i = 0 To 4
            If d = 0 And i = 0 Then i = 1
            If cel.Offset(30 * d, 5 * i) = "" Then
                rng4.ClearContents
                rng5.ClearContents
                rng4.Value = "Session Statistics " & i + d * 5
                rng5.Value = "Session Profits / Losses " & i + d * 5
                rng1.Copy rng1.Offset(30 * d, 5 * i)
                rng2.Offset(30 * d, 5 * i).ClearContents
                rng3.Offset(30 * d, 5 * i).ClearContents
                rng2.Copy
                rng2.Offset(30 * d, 5 * i).PasteSpecial xlPasteValues
                rng3.Copy
                rng3.Offset(30 * d, 5 * i).PasteSpecial xlPasteValues
                rng4.ClearContents
                rng5.ClearContents
                rng4.Value = "Current Session Statistics"
                rng5.Value = "Current Session Profits / Losses"
                GoTo TheEnd
            End If
        Next i
    Next d
TheEnd:
Application.Calculation = xlCalculationAutomatic
End Sub
And with F8 and F9 i dont get any further.
This code does only Chart 1 but then keeps redoing chart 1. basically its not looping. its not going to loop 2
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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