简体   繁体   English

Excel Visual Basic ComboBox-TextBox与2个不同的单元格链接

[英]Excel Visual Basic ComboBox-TextBox Linked With 2 Different Cells

I got 1 ComboBox-Linked with Cell (A195:1230) [Purchasing_Group_List_CO] & 1 TextBox-Linked with Cell(B195:B230) [PIC_Name] . 我得到了1个与单元格链接的ComboBox(A195:1230) [Purchasing_Group_List_CO]和1个与单元格链接的TextBox(B195:B230) [PIC_Name] All I want is to change the value on ComboBox only and the TextBox will follow the rest. 我只想更改ComboBox上的值,然后TextBox将跟随其余的内容。 But I don't know which syntax do I need to shorten this code. 但是我不知道我需要哪种语法来缩短这段代码。

I'm totally new in this field, trying to understand VBA 我是这个领域的新手,试图了解VBA

Here's my code. 这是我的代码。

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

All of those "I10,I20,I21...." is the value of cells (A195:1230). 所有这些“ I10,I20,I21 ....”都是单元格的值(A195:1230)。

Is there a way to shorten my line? 有办法缩短我的电话吗? I want it to be simple cause there will be another UserForm which based on thousands of different ID. 我希望它很简单,因为将会有另一个基于数千种不同ID的UserForm。

You can do something like this: 您可以执行以下操作:

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

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

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