Forum

VBA Code for Spiral
 
Notifications
Clear all

VBA Code for Spiral

9 Posts
4 Users
0 Reactions
208 Views
(@iem)
Posts: 7
Active Member
Topic starter
 
Hi Dear

I need a VBA code for filling excel cells Spiraly.
it can be filling by counter from 1 to n.
I searched a lot of codes and change some of them but I couldn't find it.
in the attachment, you can see that.
thanks a lot.
have a good time.
 
 
 
 
Posted : 24/07/2023 7:36 am
Alan Sidman
(@alansidman)
Posts: 223
Member Moderator
 

What does adding cells spiraly mean?  Attach a file showing us what you are trying to do.  Are you trying to

Create a Fibonacci Sequence

 
Posted : 24/07/2023 5:20 pm
(@iem)
Posts: 7
Active Member
Topic starter
 

Hi

I added an attachment.

 
Posted : 27/07/2023 1:59 am
Alan Sidman
(@alansidman)
Posts: 223
Member Moderator
 

Your attachment is unreadable for me.  It has appeared as all WingDings.

 
Posted : 27/07/2023 4:43 pm
Riny van Eekelen
(@riny)
Posts: 1209
Member Moderator
 

"Your attachment is unreadable for me. It has appeared as all WingDings"

Right-click on the file and select 'Save link as'. Now you can save the file where you want and then open it.

 
Posted : 28/07/2023 1:45 am
(@iem)
Posts: 7
Active Member
Topic starter
 

Hi Dear

H attached a simple file.Spiral-in-excel.jpg

start from a cell like I10 and move spiraling to cell J10 , J11 , I11 , ... anf fill cells by counter.

 
Posted : 30/07/2023 12:50 am
(@keebellah)
Posts: 373
Reputable Member
 

And what's the maximum value you want?

 
Posted : 31/07/2023 4:02 am
(@iem)
Posts: 7
Active Member
Topic starter
 

I find the way by myself!

I change it a little, it works. you can see that:

Sub Spiral_new()
Dim userinput As Long, n As Long, i As Long, j As Long, rngAddr As Range
userinput = InputBox("How many cells for Spiralling?")
n = 1: j = 0: i = 1
Set rngAddr = Cells(10, 10)

Do Until n > userinput

Do Until j >= i
If n <= userinput Then
Set rngAddr = rngAddr.Offset(0, 1)
rngAddr.Value = n
j = j + 1
n = n + 1
Else
Exit Sub
End If
Loop

j = 1
Do Until j >= i
If n <= userinput Then
Set rngAddr = rngAddr.Offset(-1, 0)
rngAddr.Value = n
j = j + 1
n = n + 1
Else
Exit Sub
End If
Loop
i = i + 1
j = 1
Do Until j >= i
If n <= userinput Then
Set rngAddr = rngAddr.Offset(0, -1)
rngAddr.Value = n
j = j + 1
n = n + 1
Else
Exit Sub
End If
Loop
j = 1
Do Until j >= i
If n <= userinput Then
Set rngAddr = rngAddr.Offset(1, 0)
rngAddr.Value = n
j = j + 1
n = n + 1
Else
Exit Sub
End If
Loop
i = i + 1
j = 1
Loop
Set rngAddr = Nothing
End Sub

 
Posted : 01/08/2023 6:57 am
(@keebellah)
Posts: 373
Reputable Member
 

You should also include something to clear the area before a new one

 
Posted : 02/08/2023 2:36 am
Share: