【问题标题】:Excel VBA Userform with combo box and Vlookup带有组合框和 Vlookup 的 Excel VBA 用户表单
【发布时间】:2018-03-21 01:42:19
【问题描述】:

我尝试用组合框制作我的第一个用户窗体,我已经做到了:

   Private Sub cmdClose_Click()
   Unload Me
   End Sub

  Private Sub Reg1_AfterUpdate()
  If WorksheetFunction.CountIf(Stock.Range("A:A"), Me.Range.Value) = 0 Then
  NsgBox "This is an incorrect item"
  Me.Reg1.Value = ""
  Exit Sub
  End If

  With Me
  .Reg2 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), 
  Stock.Range("Lookup"), 2, 0)
  End Sub 

  Private Sub UserForm_Click()
  End Sub

问题是我不知道如何使用带有 Vlookup 功能的组合框。

我需要一个组合框,因为我想更改 Sheet1 上的数字。

例如:

       Harry      10
       David      20

  A1 Harry   B1  10
  A2 David   B2  20

所以我想从组合框中选择一个名称。选择名称后,我想在文本框中输入一个数字,该数字将属于所选名称并与现有数字相加。

所以 Harry 有 10 个。在我从组合框中选择 Harry 并在 TextBox 中设置 90 后,Harry 的数字将变为 100。这就是为什么我认为我必须在 VBA 中以某种方式使用 Vlookup。

谢谢

【问题讨论】:

  • 看起来是这样的:imgur.com/a/BoZZ9
  • Harry 和 10 是否在不同的列中?
  • 是的,哈利是A1,10是B1
  • 那么完美,检查我的答案应该可以!

标签: excel vba combobox textbox userform


【解决方案1】:

下面的代码应该按照你的要求工作

Sub ChangeValue()
    Dim sheetName As String

    sheetName = "Name of your sheet"

    With ThisWorkbook.Sheets(sheetName)
       'For each name in your range
        For Each cell In .Range("Your range where names are") 
           'If the name is the one selected in your combobox
            If (cell = YourComboBox.Text) Then
                'Increment value 
                .Range(cell.Address).Offset(0, 1).Value = _
                .Range(cell.Address).Offset(0, 1).Value + YourTextBoxValue.Text
            End If
        Next cell
    End With
End Sub

用法

工作表名称替换为名称所在的工作表名称。

姓名所在的范围替换为我们可以在您的工作表中找到所有姓名的范围。

YourComboBoxYourTextBoxValue 替换为 userForm 中组件的名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-07
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 1970-01-01
    相关资源
    最近更新 更多