Runtime Error 91 when dropdown changes are made the second time

royb5000

New Member
Joined
Jul 23, 2015
Messages
3
Good afternoon all. I hope you are doing well. This is my first post here on Mrexcel so please excuse any lapses in formality that I may cause. Thank you all for the valuable information that is provided to this VBA newbie, it has been a great help.

I have been working on a complicated userform for my HR department that will complete many different functions based on dropdown choices that they make.

On the userform, I have a dropdown called "cboPrevTitle" that loads a list of positions from another excel spreadsheet when the userform initializes. This works very well.

Based on the position title that I choose, a dropdown called cboPrevStep is populated from a different spreadsheet which provides me with the pay steps that are available to that title. Here is the code that completes that function:

Code:
Private Sub cboPrevTitle_Change()

    Dim ChosenTitle As String
    Dim FindRowNumber As Long
    Dim SalaryClass
    Dim TitleComments As String
    ChosenTitle = cboPrevTitle.Value
    
    

        Set SourceWB = Workbooks.Open("S:\Hr Share\Par Main\ParResource.xlsx", _
            False, True)
'
            With SourceWB.Sheets(1)
                Set FindRow = .Range("A2:A175").Find(What:=ChosenTitle, LookIn:=xlValues)
                FindRowNumber = FindRow.Row
                SalaryClass = .Range("P" & FindRowNumber)
                TitleComments = .Range("Q" & FindRowNumber)
                lblTitleComments.Caption = TitleComments
                
        SourceWB.Close False ' close the source workbook without saving changes
        Set SourceWB = Nothing
        
        
        
            
        End With
'====================================================================================================
        'Populate cboPrevStep
        
        If SalaryClass = "Admin-11 Month" Then
        
        With Me.cboPrevStep
        .Clear ' remove existing entries from the listbox
        ' turn screen updating off,
        ' prevent the user from seeing the source workbook being opened
        Application.ScreenUpdating = False
        ' open the source workbook as ReadOnly
        Set SourceWB = Workbooks.Open("S:\Hr Share\Par Main\SalaryScales.xlsx", _
            False, True)
        
        ListItems = SourceWB.Worksheets(4).Range("A3:A23").Value
        ' get the values you want
        
        ListItems = Application.WorksheetFunction.Transpose(ListItems)
        ' convert values to a vertical array
        For i = 1 To UBound(ListItems)
            .AddItem ListItems(i) ' populate the listbox
        Next i
        .ListIndex = -1 ' no items selected, set to 0 to select the first item
        SourceWB.Close False ' close the source workbook without saving changes
        Set SourceWB = Nothing
        Application.ScreenUpdating = True
       End With
    ElseIf cboPrevTitle.ListIndex = -1 Then
    
        With Me.cboPrevStep
        .Clear
        End With
    End If
  End Sub

The above code does exactly what I want in that it loads the cboPrevStep dropdown with the available codes for the chosen title. The SalaryClass is the class that is used to point to the correct list of salary scales and give me the right steps for that job title. I have about 12 different Salary Classes but only gave you one example because I didn't want to make this post too long.

Now once I have chosen a title, and the steps are loaded, I now need to choose a step. Once I pick the step it pulls the information with no problem until I decide to choose another title from the cboPrevTitle list and then it give me a runtime error 91 and errors out on the "Set Annual Salary =" line of code.

This is supposed to combine a column address with the row address so that I can pull the data from that cell but it will not work after the first time through.

I have figured out that my problem lies in that when the code runs the second time through, the stepvalue variable is empty but I cannot figure out why. It should contain the step that I have chosen in the dropdown box.

Any help at would be greatly appreciated. If you have any questions or are confused by my code, please let me know and I will be glad to try to clarify.

Code:
Private Sub cboPrevStep_Change()


    Dim ChosenTitle As String
    Dim Grade As String
    ChosenTitle = cboPrevTitle.Value
    
    Dim FindColumn As Long
    Dim FindColumnLetter
    Dim GradeColumnLetter
    Dim TitleRowNumber
    Dim SalaryClass
    
    Dim AnnualSalary
    Dim TotalSalary
    
    Dim HoursPerYear
    Dim ContractDays
    Dim HoursPerDay
    
    Dim PPBAChoice
    
    lblPrevAuxPayAmt1.Caption = 0
    lblPrevAuxPayAmt2.Caption = 0
    txtPrevAuxPayAmt1.Value = 0
    txtPrevAuxPayAmt2.Value = 0
'-----------------------------------------------------------------------------------------
    'Find Salary Class and Grade for the chosen position

        Set SourceWB = Workbooks.Open("S:\Hr Share\Par Main\ParResource.xlsx", _
            False, True)
            
            With SourceWB.Sheets(1)
                Set FindRow = .Range("A2:A175").Find(What:=ChosenTitle, LookIn:=xlValues)
            
            TitleRowNumber = FindRow.Row
            
            SalaryClass = .Range("P" & TitleRowNumber)
            Grade = .Range("O" & TitleRowNumber)
            ContractDays = .Range("L" & TitleRowNumber)
            HoursPerDay = .Range("M" & TitleRowNumber)
            HoursPerYear = ContractDays * HoursPerDay
            
            
            
        SourceWB.Close False ' close the source workbook without saving changes
        Set SourceWB = Nothing
        
        End With
'-------------------------------------------------------------------------------------------

        If SalaryClass = "Admin-11 Month" Then

        ' turn screen updating off,
        ' prevent the user from seeing the source workbook being opened
        Application.ScreenUpdating = False
        ' open the source workbook as ReadOnly
        Set SourceWB = Workbooks.Open("S:\Hr Share\Par Main\SalaryScales.xlsx", _
            False, True)
            

        With SourceWB.Sheets(4)
                
                
                
                Set FindColumnLetter = .Range("B2:R2").Find(What:=Grade, LookIn:=xlValues)

                StepValue = cboPrevStep.Value
                MsgBox StepValue
                Set Stepvaluerow = .Range("A3:A23").Find(What:=StepValue, LookIn:=xlValues)
                FindColumn = FindColumnLetter.Column
                GradeColumnLetter = Col_Letter(FindColumn)
                
                Set AnnualSalary = .Range(GradeColumnLetter & Stepvaluerow.Row)  'Runtime error 91 starts here.
                
                lblAnnualSalary.Caption = "$" & AnnualSalary
                
        End With


        SourceWB.Close False ' close the source workbook without saving changes
        Set SourceWB = Nothing

End If
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi,

I guess the "StepValue" was not found.
>Set Stepvaluerow = .Range("A3:A23").Find(What:=StepValue, LookIn:=xlValues)
If this find method could not find anything, Stepvaluerow could not be stored Range-Object.
".Range(GradeColumnLetter & Stepvaluerow.Row)" will be ...nothing?
 
Upvote 0
Hi Takae, thank you for your response.

You are right, StepValue is nothing but I have no idea why. Since Stepvalue is pulled from the cboStepValue dropdown value then it should have something in it but on the second time through it is empty and it is frustrating me so much that I can't figure out why.

Here is the line where I pull that info from the combobox: StepValue = cboPrevStep.Value

Thanks for your input. Hope you are having a great day.
 
Upvote 0
Takae, thank you again for your help. I was finally able to find a solution to this issue. Instead of worrying about why the stepvalue was coming back as blank I figured out how to use an if nothing statement to get around it. Below is the new code.

Code:
If SalaryClass = "Admin-11 Month" Then

        ' turn screen updating off,
        ' prevent the user from seeing the source workbook being opened
        Application.ScreenUpdating = False
        ' open the source workbook as ReadOnly
        Set SourceWB = Workbooks.Open("S:\Hr Share\Par Main\SalaryScales.xlsx", _
            False, True)

        StepValue = cboPrevStep.Value
        
        If StepValue = "" Then
        Exit Sub
        
        ElseIf StepValue <> "" Then
        
        
        With SourceWB.Sheets(4)

                Set FindColumnLetter = .Range("B2:R2").Find(What:=Grade, LookIn:=xlValues)

                Set StepValueRow = .Range("A3:A23").Find(What:=StepValue, LookIn:=xlValues)
                FindColumn = FindColumnLetter.Column
                GradeColumnLetter = Col_Letter(FindColumn)
                
                Set AnnualSalary = .Range(GradeColumnLetter & StepValueRow.Row)  'Runtime error 91 starts here.
                
                AnnualSalaryValue = AnnualSalary

                lblAnnualSalary.Caption = "$" & AnnualSalary

        End With

            
        SourceWB.Close False ' close the source workbook without saving changes
        Set SourceWB = Nothing
       
        End If
 
Upvote 0

Forum statistics

Threads
1,222,786
Messages
6,168,220
Members
452,171
Latest member
saeid025

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