Assigning values to an array using Do while loop

tkg99

New Member
Joined
Jun 29, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hello,
I would like to know what am I doing wrong here, I'm trying to assign values from 2000 to 2030 to my array ( the task demands of me to complete it using do while loop), but I'm constantly getting errors. Can anybody tell me what's wrong with my logic here?

VBA Code:
Sub loop1()
Dim tab2(29) As Integer
Dim intc As Long
Dim intr, intm As Integer


intm = 0
intc = 2000
intr = 1

    Do While intc <= 2030
        tab2(intm) = intc
        intm = intm + 1
        intc = intc + 1
    Loop
    
    Do While intr <= 30
        Cells(intr, 1) = tab2
        intr = intr + 1
    Loop
        
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
In what way doesn't it work?
 
Upvote 0
In what way doesn't it work?
The second loop could be omitted here, but the first one is supposed to assign values to my 1D array (0 = 2000, 1 = 2001 and so on) and whenever I try to run the code, it's giving me "Subscript out of range" error.
 
Upvote 0
That's because you declared Dim tab2(29) As Integer so it will be 0 to 29 (ie 30 values) but you are trying to put 31 values into it.
 
Upvote 0
Solution
That's because you declared Dim tab2(29) As Integer so it will be 0 to 29 (ie 30 values) but you are trying to put 31 values into it.
Oh you are right, my bad 😅 Thank you very much for the help!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,821
Messages
6,121,759
Members
449,048
Latest member
excelknuckles

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