"Semi-dynamic" array

Jaymond Flurrie

Well-known Member
Joined
Sep 22, 2008
Messages
919
Office Version
  1. 365
Platform
  1. Windows
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

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
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
 
Upvote 0

Forum statistics

Threads
1,217,755
Messages
6,138,422
Members
450,136
Latest member
Tabako1960

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