VBA script out of range (dynamic arrays)

robertrobert905

Board Regular
Joined
Jun 27, 2008
Messages
139
Hi everyoneI'm getting the runtime error '9', script out of range errors

1.The line of the error is Accountlist(AccountlistCount) = TempText
2.GetLastRowWithData is a function that returns an int
3. temptext is a string in a cell, e.g. Account:Company1


So am I not allowed to assign text to a dynamic array?
Code:
Sub RawFileFix()

Dim Accountlist() As String
Dim AccountlistCount As Integer
Dim TempText As String
AccountlistCount = 1

Sheets("Raw files").Select

For i = 1 To GetLastRowWithData
    If InStr(1, Range("A" & CStr(i)).Value, "Account") > 0 Then  'cell says Account:...
         TempText = Range("A" & CStr(i)).Value
         Accountlist(AccountlistCount) = TempText
         AccountlistCount = AccountlistCount + 1
    End If
Next i

For j = 1 To UBound(Accountlist)
Range("B" & CStr(j)).Value = Accountlist(j)

Next j
End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try...

Code:
Sub RawFileFix()

    Dim AccountlistCount As Integer
    Dim TempText As String
    Dim Accountlist() As String
    
    Sheets("Raw files").Select
    
    ReDim Accountlist(1 To GetLastRowWithData)
    
    AccountlistCount = 1

    For i = 1 To UBound(Accountlist)
        If InStr(1, Range("A" & CStr(i)).Value, "Account") > 0 Then  'cell says Account:...
            TempText = Range("A" & CStr(i)).Value
            Accountlist(AccountlistCount) = TempText
            AccountlistCount = AccountlistCount + 1
        End If
    Next i
    
    For j = 1 To UBound(Accountlist)
        Range("B" & CStr(j)).Value = Accountlist(j)
    Next j
    
End Sub
 
Upvote 0
Try putting:
ReDim Accountlist(1 To AccountlistCount)
after
AccountlistCount = 1

and putting:
ReDim Preserve Accountlist(1 To AccountlistCount)
after:
TempText = Range("A" & CStr(i)).Value
 
Upvote 0
Try...

Code:
Sub RawFileFix()

    Dim AccountlistCount As Integer
    Dim TempText As String
    Dim Accountlist() As String
    
    Sheets("Raw files").Select
    
    ReDim Accountlist(1 To GetLastRowWithData)
    
    AccountlistCount = 1

    For i = 1 To UBound(Accountlist)
        If InStr(1, Range("A" & CStr(i)).Value, "Account") > 0 Then  'cell says Account:...
            TempText = Range("A" & CStr(i)).Value
            Accountlist(AccountlistCount) = TempText
            AccountlistCount = AccountlistCount + 1
        End If
    Next i
    
    For j = 1 To UBound(Accountlist)
        Range("B" & CStr(j)).Value = Accountlist(j)
    Next j
    
End Sub


There seems to be a lot of unnecessary activity

Code:
Sub RawFileFix()

    Dim AccountlistCount As Integer
    dim x as long

    Sheets("Raw files").Select
    
    x=GetLastRowWithData
 
   AccountlistCount = 1

    For i = 1 To x
        If InStr(1, Range("A" & CStr(i)).Value, "Account") > 0 Then  'cell says Account:...
            Range("B" & CStr(AccountlistCount )).Value= Range("A" & CStr(i)).Value
            AccountlistCount = AccountlistCount + 1
        End If
    Next i
    
    
End Sub
 
Upvote 0
thanks everyone, I did not know that you had to redim the dynamic array to static before adding any information in there.

Mr.redundant
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,299
Members
452,904
Latest member
CodeMasterX

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