繁体   English   中英

我想从组合框中获取 select 数据并使用 VBA Excel 中的 IF 语句将其拉到我的按钮

[英]I want to select data from a combo box and pull it through to my button using an IF statement in VBA Excel

我使用 VBA 在 Excel 中创建了一个组合框和按钮。 我希望我的按钮运行一个 IF 语句,并根据组合框中的选择 go 到另一个工作表。 但是我当前的代码在调试时给了我一个“需要对象”的错误。

请帮忙?

这是我当前的代码:

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

您在 dropdown13 值和您希望激活的工作表之间具有已知的 1:1 关系。 因此,您可以通过使用 Scripting.DIctionary 完全避免冗长而复杂的 if/elseif。

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

我认为您的核心问题是您在工作表上没有任何名为“DropDown13”的控件。

一旦你解决了这个问题,那么也许可以考虑使用Select Case而不是更详细的If ElseIf方法。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM