"Semi-dynamic" array

Jaymond Flurrie

Well-known Member
Joined
Sep 22, 2008
Messages
870
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

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

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
 

Watch MrExcel Video

Forum statistics

Threads
1,130,117
Messages
5,640,207
Members
417,131
Latest member
Seanr19871

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