【问题标题】:How do you pass a cell value from a table to a combobox?如何将表格中的单元格值传递给组合框?
【发布时间】:2020-12-28 23:36:31
【问题描述】:

我正在尝试在 Excel VBA 中构建一个表单。我已经构建了表单,但我希望用户能够编辑表单中的数据。我希望用户能够输入参数,然后表单将搜索表格以查看参数是否存在。如果是,则表单将填充该行中的数据。

我不知道为什么这段代码有效,但我似乎无法将单元格中的数据取出。我可以从手表上看到正在选择正确的单元格。但是单元格不会分配给我的变量或组合框。这是我到目前为止所得到的!

Public Function LoadFormEdit(Row As Integer)
Dim rng As Range
Dim RowNum As Integer
RowNum = Row
Dim lookupValue As String
'Set the range to be the table we want to work with
Set rng = ThisWorkbook.Worksheets("MyWorksheet").Range("MyDataTable")

Dim ColOffset As Integer
ColOffset = rng.Column - 1

'Add the selections to the ComboBox, and make visible
Me.MyComboBox.Visible = True
lookupValue = rng.Cells(RowNum, 6).Text
Me.MyComboBox.value = lookupValue

非常感谢任何帮助!

【问题讨论】:

    标签: excel vba combobox range


    【解决方案1】:

    以真正的 Stack Overflow 方式,我在发布后发现了这一点。

    问题是我没有正确考虑到我的表不在工作表中的 1,1 处。因此,以下代码修复了一些问题:

    Public Function LoadFormEdit(Row As Integer)
    Dim rng As Range
    Dim RowNum As Integer
    Dim RowOffset As Integer
    Dim lookupValue As String
    
    'Set the range to be the table we want to work with
    Set rng = ThisWorkbook.Worksheets("MyDataSheet").Range("MyDataTable")
    RowOffset = rng.Row - 1
    RowNum = Row - RowOffset
    
    
    'Add the selections to the ComboBox, and make visible
    Me.MyComboBox.Visible = True
    lookupValue = rng.Cells(RowNum, 5).value
    Me.MyComboBox.value = lookupValue
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-08
      • 2013-10-31
      • 2020-11-14
      • 1970-01-01
      • 2019-07-05
      相关资源
      最近更新 更多