I created a combo box and button in Excel using VBA. I want my button to run an IF statement and based on the selection in the combo box to go to a different worksheet. But my current code is giving me a "object required" error when debugging.
Please help?
Here's my current code:
Sub Button9_Click()
If DropDown13.Value = "Access Management" Then
ThisWorkbook.Sheets("AccMA").Activate
ElseIf DropDown13.Value = "Audit Management" Then
ThisWorkbook.Sheets("AudMA").Activate
ElseIf DropDown13.Value = "Asset Management" Then
ThisWorkbook.Sheets("AssMA").Activate
ElseIf DropDown13.Value = "Benefits Realisation" Then
ThisWorkbook.Sheets("BenMA").Activate
ElseIf DropDown13.Value = "Business Continuity Management" Then
ThisWorkbook.Sheets("BCMA").Activate
ElseIf DropDown13.Value = "Business Process Management" Then
ThisWorkbook.Sheets("BPMA").Activate
ElseIf DropDown13.Value = "Capacity Management" Then
ThisWorkbook.Sheets("CAPA").Activate
ElseIf DropDown13.Value = "Catalogue Management" Then
ThisWorkbook.Sheets("CATA").Activate
ElseIf DropDown13.Value = "Change Management" Then
ThisWorkbook.Sheets("CNGA").Activate
ElseIf DropDown13.Value = "Communications Management" Then
ThisWorkbook.Sheets("COMA").Activate
ElseIf DropDown13.Value = "Compliance Management" Then
ThisWorkbook.Sheets("COPA").Activate
End If
End Sub
You have a known 1:1 relationship between dropdown13 values and the sheets you wish to activate. Therefore you can totally avoid a long and complicated if/elseif by using a Scripting.DIctionary.
Option Explicit
Dim mySheets As Scripting.Dictionary
Sub Button9_click()
Dim myValue As String
myValue = dropdown13.Value
If mySheets Is Nothing Then SetupMySheets
If mySheets.Exists(myValue) Then
ThisWorkbook.Sheets(mySheets.Item(myValue)).Activate
Else
'raise an error because the requested sheet doesn't exist
End If
End Sub
Public Sub SetupMySheets()
Set mySheets = New Scripting.Dictionary
With mySheets
.Add "Access Management", "AccMA"
.Add "Audit Management", "AudMA"
.Add "Asset Management", "AssMA"
.Add "Benefits Realisation", "BenMA"
.Add "Business Continuity Management", "BCMA"
.Add "Business Process Management", "BPMA"
.Add "Capacity Management", "CAPA"
.Add "Catalogue Management", "CATA"
.Add "Change Management", "CNGA"
.Add "Communications Management", "COMA"
.Add "Compliance Management", "COPA"
End With
End Sub
I think your core problem is you don't have any control named "DropDown13" on the worksheet.
Once you resolve that , then maybe consider using Select Case
instead of the more-verbose If ElseIf
approach.
Sub Button9_Click()
Select Case Me.DropDown13.Value
Case "Access Management": sht = "AccMA"
Case "Audit Management": sht = "AudMA"
Case "Asset Management": sht = "AssMA"
'etc etc
Case Else: sht = ""
End Select
If Len(sht) > 0 Then ThisWorkbook.Sheets(sht).Activate
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.