Vba loop sequence

Frankroger

New Member
Joined
Nov 16, 2023
Messages
15
Office Version
  1. 2013
Platform
  1. Windows
Can anyone please guide me with this problem

I'm trying to create an number square, but want to use vba rather than formulas

I want to be able to set the size of the square, if the number 3 is entered, it would be 3x3,
3 rows and 3 columns
If 4 was entered, then it would be 4x4, 4 rows by 4 columns etc.

Then for it to begin from an selected number, in the example I've set it to start at 10, and to be able to increment by an selected value.

I've put two examples using formula, but I don't understand vba well enough

So far all I have is the first column figured with vba =(

Dim i As Integer

i = 5

For i = 1 To i

Activecell.value = 1
Activecell.offset(1, 0).activate

Next i

End sub



Book1
JKLMNOPQRSTUV
12
1350100150200250
144090140190240
153693080130180230
162582070120170220
171471060110160210
18
19Start1Start10
20increment1increment10
21Size3Size5
22
23
Sheet1
Cell Formulas
RangeFormula
Q13:U16Q13=Q14+$Q$20
Q17,K17Q17=Q19
R17:U17R17=Q13+$Q$20
K15:K16K15=K16+K19
L15L15=L16+K20
M15M15=M16+K20
L16L16=L17+K20
M16M16=M17+K20
L17L17=K15+K20
M17M17=L15+K20
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Modify code as needed for where your parameters are located and where you want the square to start:
Rich (BB code):
Public Sub NumberSquare()

   Dim Start As Long
   Dim Increment As Long
   Dim Size As Long
   Dim Row As Long, Column As Long
   Dim n As Long
 
   Start = [B1]
   Increment = [B2]
   Size = [B3]
 
   n = Start
   For Column = 3 To Size + 3 - 1 ' First column is 3=C
      For Row = 1 + Size - 1 To 1 Step -1 ' First row is 1
         Cells(Row, Column) = n
         n = n + Increment
      Next Row
   Next Column

End Sub
$scratch.xlsm
ABCDE
1Start151117
2Increment23915
3Size31713
Number Square
 
Upvote 1
Solution
Modify code as needed for where your parameters are located and where you want the square to start:
Rich (BB code):
Public Sub NumberSquare()

   Dim Start As Long
   Dim Increment As Long
   Dim Size As Long
   Dim Row As Long, Column As Long
   Dim n As Long
 
   Start = [B1]
   Increment = [B2]
   Size = [B3]
 
   n = Start
   For Column = 3 To Size + 3 - 1 ' First column is 3=C
      For Row = 1 + Size - 1 To 1 Step -1 ' First row is 1
         Cells(Row, Column) = n
         n = n + Increment
      Next Row
   Next Column

End Sub
$scratch.xlsm
ABCDE
1Start151117
2Increment23915
3Size31713
Number Square

Thank you so much, it works great!

Is it possible to add increments that are less than 1,

If I set the increment to 0.1, its returning 1s for all cells
 
Upvote 0
Change the declaration for Increment
VBA Code:
Dim Increment As Double

You will also need to do the same with Start if you want to start with 0.1 or some other real number
 
Upvote 1
Change the declaration for Increment
VBA Code:
Dim Increment As Double

You will also need to do the same with Start if you want to start with 0.1 or some other real number

Thank you for the help! =)

May I ask you a different variation of number square on a new thread?

It's the same number sequences again, only spiraling out from a centre, rather than stacked in columns.
I can explain it better with an example in a new thread
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,951
Members
449,095
Latest member
nmaske

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