Run-time error 9: Subscript out of range

Glitch5618

Board Regular
Joined
Nov 6, 2015
Messages
105
Using excel 2007.

I'm trying to load an array into a worksheet, the array in question is declared as public within one module and is later called in a second module where the error occurs. The line throwing the error is highlighted in red. Array in blue.

First module:
Rich (BB code):
Option Explicit
Public d As DataBox
Public f As UserForm
Public AgentQuestionArray() As Variant
Public frmloaded As Boolean


Public Sub GetAgentQuestion()
'Prevents screen flickering
Application.ScreenUpdating = False
'PURPOSE: Searches for all all instances of agent name in question data and displays array in listbox
Set d = DataBox
Set f = UserForm
Dim fnd As String, FirstFound As String
Dim FoundCell As Range, Rng As Range
Dim myRange As Range, LastCell As Range


Worksheets("Question Data").Activate
'Sorts the Agent column to allow re-sizing of rng data range
With ActiveWorkbook.Worksheets("Question Data")
    .Range("A1:A" & .Range("A" & Rows.Count).End(xlUp).Row).Sort Key1:=.Range("A2"), Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
End With


'Search value
fnd = f.cboOption.Value
 Set myRange = ActiveSheet.Range("A:A")
  Set LastCell = myRange.Cells(myRange.Cells.Count)
   Set FoundCell = myRange.Find(what:=fnd, after:=LastCell)
 
'Test to see if anything was found
If Not FoundCell Is Nothing Then
    FirstFound = FoundCell.Address
Else
    GoTo NothingFound
End If


Set Rng = FoundCell
'Loop until cycled through all unique finds
    Do Until FoundCell Is Nothing
        'Find next cell with fnd valuem
        Set FoundCell = myRange.FindNext(after:=FoundCell)
            'Add found cell to rng range variable
            Set Rng = Union(Rng, FoundCell)
                'Test to see if cycled through to first found cell
                If FoundCell.Address = FirstFound Then Exit Do
  Loop


'Resize rng to include respective data and assign rng to array
Set Rng = Rng.Resize(, 14)
AgentQuestionArray = Rng


    d.listQuestion.List = AgentQuestionArray
    d.listQuestion.ColumnCount = 14
    d.listQuestion.ColumnWidths = ";;;;;;;;;;;;;"
    d.MultiPage1.Value = 0
          
If DataBox.Visible = True Then frmloaded = True
If DataBox.Visible = False Then frmloaded = False
    If frmloaded = True Then
    ElseIf frmloaded = False Then
        DataBox.Show
    End If


'Prevents cascading of userform
Application.ScreenUpdating = True


Exit Sub
'Error Handler
NothingFound:
  MsgBox "No question data found."
End Sub



Second Module calling AgentQuestionArray:
Rich (BB code):
Private Sub buttDate_Click()
Dim WS As Worksheet
Dim flg As Boolean
Dim Destination As Range
    
For Each WS In Worksheets
    If WS.Name Like "DataTemp" Then flg = True: Exit For
    Next
        If flg = True Then
            WS.Visible = xlSheetVisible
            Set Destination = Sheets("DataTemp").Range("A1")
            Sheets("DataTemp").UsedRange.ClearContents
        Else
            Set WS = Sheets.Add: WS.Name = "DataTemp"
            Set Destination = Sheets("DataTemp").Range("A1")
        End If


If Me.listQuestion.ListCount = 0 Then
    MsgBox "You must get data first"
ElseIf Me.listQuestion.ListCount = 1 Then
    MsgBox "You need more data then that!"
ElseIf Me.listQuestion.ListCount > 1 Then
    Destination.Resize(UBound(AgentQuestionArray, 1), UBound(AgentQuestionArray, 2)).Value = AgentQuestionArray
    Call FilterByDate
End If
End Sub

Can anyone explain why this is giving me an error? I'm completely confused because the Destination range works in all my other code, however it is contained within the same module that the arrays are declared as public. Although my understanding is that if its public it should be accessible to all modules or userforms in a project. I could really use help with this and an explanation of how to fix this, since I need to be able to load the array to a worksheet to filter the dates contained in the data.
 
Last edited:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Please disregard this threat. I'm a moron. I had the array declared public and then declared within the sub as well. Removing the dim in the sub fixed the problem.
 
Upvote 0

Forum statistics

Threads
1,216,225
Messages
6,129,597
Members
449,520
Latest member
TBFrieds

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