I'm a VBA NewBe (this is my first post asking a question on any VBA help site) working on a Property/Expense Management application in excel. the ComboBoxes in question are used to select worksheets based on a Property ID, propID
, in cmbPropID
based on a range in a "Control" worksheet and the worksheet year, wsYr
, in cmbYear determined by the individual propID's beginning year, wsStartYr
, and the current calendar year, wdCurYear
. I am able to select/activate and view all worksheets of the first property I select. However, when I select another property to work on the application crashes with Debug error: 9 - Script out of range
! I have been going blind for over a week searching for a solution without success.
This issue is important in the overall functionality of the application. I hope someone is able to help me with this. Thanks in advance.
Below are the code sections for the ComboBoxes
The first configures cmbPropID
'this is an excert form MultiPage1
Case 2
'configure cmbPropID DDL
wkstControl.Activate
selectedRow = cmbPropID.ListIndex + 3 'I presume this is 3 instead of 2 because when using 2
'this throws an error - not sure why
For Each cPart In wsCntrl.Range(Range("propIDs"), Range("A" & Rows.Count).End(xlUp))
pAct = wsCntrl.Cells(selectedRow, 11).Value 'Value used to test the "isActive" status of a RealEstate property location
With Me.cmbPropID
If pAct = True And cPart.Value <> "" Then cmbPropID.AddItem cPart.Value 'Never Shows pAct as False ???
'this presents an issue that if is inActive of does'nt have worksheets it causes
' Debug error "Error: 13, Type Mismatch"
End With
Next cPart
This sub configures the contents of the cmbYears
DDL and should reset the DDL's contents when selection a different propID
... see notes in next sub-routine
Private Sub cmbPropID_Change()
Dim i
Dim strValue As String
wsCntrl.Activate
pID = ""
wsA = ""
wsYr = ""
selectedRow = cmbPropID.ListIndex + 2
wsStartYr = wsCntrl.Cells(selectedRow, 13).Text
With cmbYears
.Clear
For i = wsStartYr To wbCurYear
pAct = wsCntrl.Cells(selectedRow, 11).Value
If wbCurYear <> wsStartYr And pAct = True Then
.AddItem i
ElseIf wbCurYear = wsStartYr Then
.AddItem wbCurYear
End If
Next i
End With
lstDsplyUtil1.RowSource = ""
pID = cmbPropID.Text
End Sub
and finally, cmbYears. The code crashes at Set wsUtil = Worksheets(wsA)
when a when cmbPropID
changes.
Private Sub cmbYears_Change()
wsYr = cmbYears.Text
wsA = pID & "_" & wsYr
Debug.Print pID, wsYr, wsA
Set wsUtil = Worksheets(wsA)
lstDsplyUtil1.RowSource = wsA & "!$A$5:$Y$16"
Debug.Print pID, wsYr, wsA
'Remove after testing
Label120.Caption = wsA
Label136.Caption = pID
Label138.Caption = wsYr
Label134.Caption = lstDsplyUtil1.RowSource
wsUtil.Activate
End Sub