【问题标题】:Excel Visual Basic ComboBox-TextBox Linked With 2 Different CellsExcel Visual Basic ComboBox-TextBox 与 2 个不同的单元格链接
【发布时间】:2019-01-21 23:46:20
【问题描述】:

我得到了 1 个 ComboBox-Linked with Cell (A195:1230)[Purchasing_Group_List_CO] & 1 TextBox-Linked with Cell(B195:B230) [PIC_Name]。我想要的只是更改 ComboBox 上的值,而 TextBox 将跟随其余部分。但我不知道我需要哪种语法来缩短这段代码。

我是这个领域的新手,试图了解 VBA

这是我的代码。

Private Sub Purchasing_Group_List_CO_Change()

    If Purchasing_Group_List_CO.Value = "I10" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B195").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I20" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B196").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I21" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B197").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I22" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B198").Value)
    End If
     If Purchasing_Group_List_CO.Value = "I30" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B199").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I31" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B200").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I32" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B201").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I40" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B202").Value)
    End If
     If Purchasing_Group_List_CO.Value = "I41" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B203").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I42" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B204").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I50" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B205").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I51" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B206").Value)
    End If
     If Purchasing_Group_List_CO.Value = "I52" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B207").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I60" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B208").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I61" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B209").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I62" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B210").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I63" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B211").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I71" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B212").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I72" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B213").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I73" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B1214").Value)
    End If
     If Purchasing_Group_List_CO.Value = "I74" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B215").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I75" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B216").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I76" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B217").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I77" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B218").Value)
    End If
     If Purchasing_Group_List_CO.Value = "I78" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B219").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I79" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B220").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I80" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B221").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I81" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B222").Value)
    End If

     If Purchasing_Group_List_CO.Value = "I82" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B223").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I83" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B224").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I84" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B225").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I85" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B226").Value)
    End If

         If Purchasing_Group_List_CO.Value = "I86" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B227").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I87" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B228").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I88" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B229").Value)
    End If

    If Purchasing_Group_List_CO.Value = "I99" Then
        PIC_Name.Value = (ThisWorkbook.Sheets("Purchasing Group Database").Range("B230").Value)
    End If




End Sub

所有这些“I10,I20,I21....”都是单元格的值 (A195:1230)。

有没有办法缩短我的线路?我希望它很简单,因为会有另一个基于数千个不同 ID 的 UserForm。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    你可以这样做:

    Private Sub Purchasing_Group_List_CO_Change()
        Dim v, m
    
        v = Purchasing_Group_List_CO.Value
        m = Application.VLookup(v, _
            ThisWorkbook.Sheets("Purchasing Group Database").Range("A195:B230"), 2, False)
    
        PIC_Name.Value = IIf(IsError(m),"???",m)
    End Sub
    

    【讨论】:

    • 这就像一个魅力,@TimWilliams ! :) 我真的很感激,非常感谢。祝你有美好的一天。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    相关资源
    最近更新 更多