"Semi-dynamic" array

Jaymond Flurrie

Well-known Member
Joined
Sep 22, 2008
Messages
891
Office Version
  1. 2016
How do I make the following code to work? I need an array that has nothing but strings in it and has two dimensions (X rows, 5 columns). The amount of rows changes a lot, it can be one, it can be one thousand.

Code:
Sub arraytest()
    Dim sHaettavat() As String
    Dim lRow As Long

    lRow = 1 'This can be any positive integer
    sHaettavat(lRow, 1) = "0.0.1.0."
    sHaettavat(lRow, 2) = "002F_002A_"
    sHaettavat(lRow, 3) = "002F_"
    sHaettavat(lRow, 4) = "No"
    sHaettavat(lRow, 5) = "Comment"
End Sub
It gives now "Run-time error '9': Subscript out of range error"

The problem is most likely on that "Dim sHaettavat() As String" row, but how do I fix it? "Dim sHaettavat(,5) As String" doesn't work either.

Am I forced to use some kind of variant even if there's nothing but strings?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Colin Legg

MrExcel MVP
Joined
Feb 28, 2008
Messages
3,497
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi,

Perhaps this?

Code:
Sub arraytest()
    Dim sHaettavat() As String
    
    Dim lRow As Long
    
    lRow = 1 'This can be any positive integer
    
    ReDim sHaettavat(1 To lRow, 1 To 5) As String
    
    sHaettavat(lRow, 1) = "0.0.1.0."
    sHaettavat(lRow, 2) = "002F_002A_"
    sHaettavat(lRow, 3) = "002F_"
    sHaettavat(lRow, 4) = "No"
    sHaettavat(lRow, 5) = "Comment"
End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,285
Messages
5,836,424
Members
430,428
Latest member
Deepak Tanwar

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
Top