Variable assignment inside do until loop

tkg99

New Member
Joined
Jun 29, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hello,
I cannot understand why does assigning a value to a variable outside a do while loop, specifically the intCol variable in the following example makes such a huge difference:

VBA Code:
Sub LoopExample11()
 
Dim intRow As Integer
Dim intCol As Integer
intRow = 1
 
Do Until intRow > 10
intCol = 1
 
Do
Cells(intRow, intCol) = intCol * intRow
intCol = intCol + 1
Loop Until intCol > 10
 
intRow = intRow + 1
Loop
 
End Sub

A simple periodic table as a result
col inside.png



When a value of 1 is assigned to intCol outside of both loops the outcome of my program looks like this
intCol outside of loop.png

Not to mention when I assign values to the both variables inside the do untill loop, my excel crashes, same thing happens when I put intRow = 1
in the place of intCol (intCol=1 outside and intRow=1 inside the loop).

I'm new to VBA programming and until stumbling upon this particular example I would never have thought that assigning variables in or outside of a loop could have such an impact on the final result. That being said I would very much appreciate anyone explaining me, why does it happen the way it does :) Thank you in advance!
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
You have a double loop so you need to reset the column variable every time you start a new row
try this modification of your code that shows what is happening during your code. ( just keep hitting carriage return)
VBA Code:
Sub LoopExample11()
 
Dim intRow As Integer
Dim intCol As Integer
intRow = 1
' the outer loop
Do Until intRow > 10
intCol = 1
        ' the inner loop
        Do
        Cells(intRow, intCol) = intCol * intRow
        intCol = intCol + 1
        MsgBox ("introw=" & intRow & " intcol =" & intCol)
        Loop Until intCol > 10
        ' the inner loop end
intRow = intRow + 1
Loop
' the outer looop end
End Sub
 
Upvote 0
Solution
You have a double loop so you need to reset the column variable every time you start a new row
try this modification of your code that shows what is happening during your code. ( just keep hitting carriage return)
VBA Code:
Sub LoopExample11()
 
Dim intRow As Integer
Dim intCol As Integer
intRow = 1
' the outer loop
Do Until intRow > 10
intCol = 1
        ' the inner loop
        Do
        Cells(intRow, intCol) = intCol * intRow
        intCol = intCol + 1
        MsgBox ("introw=" & intRow & " intcol =" & intCol)
        Loop Until intCol > 10
        ' the inner loop end
intRow = intRow + 1
Loop
' the outer looop end
End Sub
Thank you very much! I will to do as you say first thing tomorrow! Have a good night/day :)
 
Upvote 0

Forum statistics

Threads
1,215,568
Messages
6,125,599
Members
449,238
Latest member
wcbyers

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