【问题标题】:Excel VBA Userform print to dynamic listExcel VBA用户表单打印到动态列表
【发布时间】:2015-06-09 03:40:14
【问题描述】:

我正在做一个项目,我有一个 userform 和多个 comboboxescomboboxes 将输出到我指定的单元格。我的问题是我想要一个button,它根据combobox 输出对项目进行排名。我希望用户能够更改combobox 输入,单击排名button 并按顺序更改相应的列表。正如您在下面看到的,我使用了“Cells(3, 2).Value = C02Combo.Value”。我知道这只会输出到第 3 行,但我需要用户表单输出到我选择打开用户表单的任何行(列可以保持不变)。我在下面的 cmets 中尝试了一些“emptyrow”变量,但没有运气。我是 VBA 初学者,如果问题解释不正确,请见谅。

私有子 enterData_Click()

'Dim emptyRow As Long
'Make Sheet1 active
'Model.Activate
'Determine emptyRow
'emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

'Code to write form data to spreadsheet here
Cells(3, 2).Value = C02Combo.Value
Cells(3, 3).Value = AdminCombo.Value
Cells(3, 4).Value = FacultyCombo.Value
Cells(3, 5).Value = ResearchCombo.Value
Cells(3, 6).Value = EducationCombo.Value
Cells(3, 7).Value = CommunityCombo.Value
Cells(3, 8).Value = InnovationCombo.Value
Cells(3, 9).Value = CostCombo.Value
Cells(3, 10).Value = PaybackCombo.Value
Cells(3, 11).Value = CostPerCutCombo.Value

Unload Me

结束子

【问题讨论】:

    标签: vba excel dynamic combobox userform


    【解决方案1】:

    你没有明确说明哪里出了问题,所以我试图涵盖所有的基础。

    WorksheetFunction.CountA(Range("A:A")) 计算A1:An 范围内非空单元格的数量,其中An 是具有值的最低单元格。如果此范围内有任何空单元格,您将不会得到您要的结果。

    Excel 提供了多种查找最后一行或最后一列的方法;没有一个在所有情况下都有效。

    Find是最普遍适用的:

      With Sheets("xxxx")
        rowFinal = .Cells.Find("*", .Range("A1"), xlFormulas, , xlByRows, xlPrevious).Row
        colFinal = .Cells.Find("*", .Range("A1"), xlFormulas, , xlByColumns, xlPrevious).Column
      End With 
    

    如果工作表“xxxx”中的数据是矩形的,Cells(rowFinal, colFinal) 将是底部右侧的单元格,其中包含一个值。如果数据不规则,Cells(rowFinal, colFinal) 可能为空,但它下面没有值,右边也没有值。

    从这个网站来看,最流行的方法是:

      With Sheets("xxxx")
        rowAFinal = .Cells(Rows.Count, “A”).End(xlUp).Row
      End With 
    

    这相当于将光标放在 A 列的底部单元格并单击 Ctrl + UpArrow 的 VBA。如果 A 列底部有值,则会出现复杂情况,但通常这会给出 A 列中最后使用的行。

    我不确定“我需要将用户表单输出到我选择打开用户表单的任何行”是什么意思。您可以使用ActiveCell.Row 标识包含光标的行。

    以上都假设用户表单中的代码标识了所需的行。如果您希望调用模块执行识别,您需要以下内容:

      Public emptyRow As Long   ‘ This must be outside all subs and functions
    
      emptyRow = 27
    

    Dim emptyRow As Long 声明了一个模块私有的变量。如果您希望其他模块或用户表单中的子用户具有访问权限,则需要 Public

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多