Combobox ElseIF声明

时间:2018-01-22 10:05:45

标签: vba excel-vba excel

我一直在研究这个问题,我有一个带有3个组合框的Userform,每个组合都分配给一个值列表。我目前只能使第三个组合框工作“Storagebin”,一旦我选择了一个值并按下搜索,它就会填充其余的框。 我正在寻找代码,无论我放入文本的组合框,返回值。使用代码提供的“Oldmaterialnumber”和“Materialnumber”没有任何回报 谢谢!

Private Sub Search_Click()

If Len(Oldmaterialnumber.Text) <> 0 Then

row_number = row_number + 1
item_in_review = Sheets("Database").Range("B" & row_number)
If item_in_review = Oldmaterialnumber.Text Then
    Materialnumber.Text = Sheets("Database").Range("A" & row_number)
    Storagebin.Text = Sheets("Database").Range("C" & row_number)
    Olddescription.Text = Sheets("Database").Range("D" & row_number)
 End If


ElseIf Len(Materialnumber.Text) <> 0 Then

row_number = row_number + 1
item_in_review = Sheets("Database").Range("A" & row_number)
If item_in_review = Materialnumber.Text Then
    Oldmaterialnumber.Text = Sheets("Database").Range("B" & row_number)
    Storagebin.Text = Sheets("Database").Range("C" & row_number)
    Olddescription.Text = Sheets("Database").Range("D" & row_number)
End If



ElseIf Len(Storagebin.Text) <> 0 Then

row_number = 0
Do
DoEvents
row_number = row_number + 1
item_in_review = Sheets("Database").Range("C" & row_number)
If item_in_review = Storagebin.Text Then
    Materialnumber.Text = Sheets("Database").Range("A" & row_number)
    Oldmaterialnumber.Text = Sheets("Database").Range("B" & row_number)
    Olddescription.Text = Sheets("Database").Range("D" & row_number)
End If

Loop Until item_in_review = ""

End If
End Sub

2 个答案:

答案 0 :(得分:0)

尝试以下方法:

<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script defer src="https://use.fontawesome.com/releases/v5.0.1/js/all.js"></script>
  <link href="https://fonts.googleapis.com/css?family=Nunito:400,600,700,800" rel="stylesheet">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-zoom/1.7.20/jquery.zoom.js"></script>
  <script>
    $(document).ready(function() {
      $('#ex1').zoom();
      $('#ex2').zoom({
        on: 'grab'
      });
      $('#ex3').zoom({
        on: 'click'
      });
      $('#ex4').zoom({
        on: 'toggle'
      });
    });
  </script>
</head>
<body>
  <span class='zoom' id='ex1'>
    <img src='IMAGE_URL' width='555' height='320' alt='Daisy on the Ohoopee'/>
    <p>Hover</p>
  </span>
</body>
</html>

答案 1 :(得分:0)

我采用了不同的方法。花了一点时间,但你可能还是想看一看。下面的代码将“数据库”工作表中的列A:D加载到表单上的3个组合框中,包括列D,其中一列进入其中一个隐藏列(实际上是CbxBin)。当您在其中一个组合框中选择其中一个项目时,其他项目会自动对齐,而不是相互指向工作表。不需要搜索按钮(或实际上是搜索)。在表单上进行选择时进行更新。因此,您在组合框中选择一个项目,然后单击其他地方(任何地方)进行更新。

这可能不是最好的解决方案。我使用组合框'Update事件,以便您可以输入您的CBX。如果您打算严格仅从下拉列表中进行选择,请更改代码以使用Change事件,该事件为您提供即时更新,而无需单击其他位置。

要测试此代码,请将第一部分安装在标准代码模块(我假设的Module1)中,它将替换现有的Button1过程。确保Enum位于代码表的顶部。

Option Explicit
    Enum Ndb                                        ' Database columns
        ' 22 Jan 2018
        NdbMatNum = 1                               ' 1 = column A
        NdbOldMatNum
        NdbBin
        NdbOldDesc
    End Enum

    Sub Button1()
        ' 22 Jan 2018

        Dim MyForm As SetCbxs

        Set MyForm = New SetCbxs
        MyForm.Show
        Unload MyForm
    End Sub

将Userform重命名为“SetCbxs”或更改上述代码以提及表单的名称。在Button1 proc中提到了两次,这就是所需要的。

在用户表单的代码表中安装代码的第二部分。表单必须有3个名为“CbxMatNum”,“CbxOldMatNum”和“CbxBin”的组合框。它还需要一个名为“TbxOldDesc”的文本框,以及访问“DataBase”工作表以从中读取数据。该代码还提供了名为“CmdOK”的命令按钮。

Option Explicit

Private Sub UserForm_Initialize()
    ' 22 Jan 2018

    Dim Rng As Range
    Dim Arr As Variant
    Dim IsBin As Boolean
    Dim Rl As Long                              ' last row
    Dim C As Long                               ' column

    With Worksheets("Database")                 ' change as appropriate
        ' presume that all columns are of the same length
        Rl = .Cells(.Rows.Count, NdbMatNum).End(xlUp).Row
        For C = NdbMatNum To NdbBin             ' columns A:C
            IsBin = C = NdbBin                  ' include column D in CbxBin
            ' start in row 2, presuming that row 1 holds captions
            Set Rng = Range(.Cells(2, C), .Cells(Rl, C - CInt(IsBin)))
            Arr = Rng.Value
            With Me.Controls(CtlName(C))
                .List = Arr
                .ListIndex = 0
                .ColumnCount = 1 - CInt(IsBin)  ' 2 columns for CbxBin
                If IsBin Then
                    ' hide 2nd column in dropdown
                    .ColumnWidths = .Width - 8 & "pt, 0 pt"
                End If
            End With
        Next C
    End With
End Sub

Private Sub CmdOK_Click()
    ' 22 Jan 2018
    Me.Hide
End Sub

Private Sub CbxBin_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    ' 22 Jan 2018
    UpdateForm NdbBin
End Sub

Private Sub CbxMatNum_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    ' 22 Jan 2018
    UpdateForm NdbMatNum
End Sub

Private Sub CbxOldMatNum_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    ' 22 Jan 2018
    UpdateForm NdbOldMatNum
End Sub

Private Sub UpdateForm(ByVal CbxId As Ndb)
    ' 22 Jan 2018

    Dim i As Ndb
    Dim R As Integer

    Application.EnableEvents = False
    R = Me.Controls(CtlName(CbxId)).ListIndex
    For i = NdbMatNum To NdbBin             ' each of the 3 Cbx's
        If i <> CbxId Then
            Me.Controls(CtlName(i)).ListIndex = R
        End If
        TbxOldDesc.Text = CbxBin.Column(1)  ' Column is zero-based
    Next i
    Application.EnableEvents = True
End Sub

Private Function CtlName(ByVal Id As Long) As String
    ' 22 Jan 2018
    ' better not to repeat the names everwhere in the code:
    ' first item's index = 0
    CtlName = Split("CbxMatNum CbxOldMatNum CbxBin")(Id - 1)
End Function