【问题标题】:Having trouble using designer and inputng VBA Code使用设计器和输入 VBA 代码时遇到问题
【发布时间】:2015-11-23 02:14:21
【问题描述】:

有人可以帮我让我的用户表单从 cal 工作表提交到此表中吗?

Private Sub cmdbutton_submitform_Click()
    Dim emptyRow As Long

    'Make Sheet2 active
    Sheet2.Activate
    'Determine emptyRow
    emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
    'Transfer information
    Cells(emptyRow, 1).Value = txtbox_number.Value
    Cells(emptyRow, 2).Value = txtbox_rank.Value
    Cells(emptyRow, 3).Value = txtbox_Name.Value
    Cells(emptyRow, 4).Value = txtbox_height.Value
    Cells(emptyRow, 5).Value = txtbox_weight.Value
    Cells(emptyRow, 6).Value = txtbox_right_rm.Value
    Cells(emptyRow, 7).Value = txtbox_left_rm.Value
End Sub

【问题讨论】:

  • 我可以截屏我正在使用的 vba 代码
  • Private Sub cmdbutton_submitform_Click() Dim emptyRow As Long '使 Sheet2 处于活动状态 Sheet2.Activate '确定 emptyRow emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1 '传输信息 Cells(emptyRow , 1).Value = txtbox_number.Value 单元格(emptyRow, 2).Value = txtbox_rank.Value 单元格(emptyRow, 3).Value = txtbox_Name.Value 单元格(emptyRow, 4).Value = txtbox_height.Value 单元格(emptyRow, 5 ).Value = txtbox_weight.Value Cells(emptyRow, 6).Value = txtbox_right_rm.Value Cells(emptyRow, 7).Value = txtbox_left_rm.Value End Sub
  • 似乎是什么问题?您收到错误或...?

标签: vba excel


【解决方案1】:

我认为您对工作表代号和工作表名称感到困惑(请参阅this)。试试

Private Sub cmdbutton_submitform_Click()
    Dim emptyRow As Long

    With Worksheets("Sheet2")
        'Determine emptyRow
        emptyRow = WorksheetFunction.CountA(.Range("A:A")) + 1
        'Transfer information
        .Cells(emptyRow, 1).Value = txtbox_number.Value
        .Cells(emptyRow, 2).Value = txtbox_rank.Value
        .Cells(emptyRow, 3).Value = txtbox_Name.Value
        .Cells(emptyRow, 4).Value = txtbox_height.Value
        .Cells(emptyRow, 5).Value = txtbox_weight.Value
        .Cells(emptyRow, 6).Value = txtbox_right_rm.Value
        .Cells(emptyRow, 7).Value = txtbox_left_rm.Value
    End With
End Sub

【讨论】:

  • 真棒洞察力谢谢...我每天都在学习。直到今天才接触过 vba
【解决方案2】:

使用Worksheet.Activate method 可能会丢失从用户表单正确获取文本框数据所需的父表单引用。在此Private Sub 中,您应该能够通过其工作表.CodeName property 引用Sheet2,并使用Me 将用户表单引用为文本框的父级。

Private Sub cmdbutton_submitform_Click()
    Dim emptyRow As Long

    'Reference Sheet2 by CodeName as the parent worksheet of the .Cells
    With Sheet2
        'Determine emptyRow
        emptyRow = .Cells(Rows.Count, 1).End(xlUp).Row + 1
        'Transfer information
        .Cells(emptyRow, 1).Value = Me.txtbox_number.Value
        .Cells(emptyRow, 2).Value = Me.txtbox_rank.Value
        .Cells(emptyRow, 3).Value = Me.txtbox_Name.Value
        .Cells(emptyRow, 4).Value = Me.txtbox_height.Value
        .Cells(emptyRow, 5).Value = Me.txtbox_weight.Value
        .Cells(emptyRow, 6).Value = Me.txtbox_right_rm.Value
        .Cells(emptyRow, 7).Value = Me.txtbox_left_rm.Value
    End With
End Sub

我发现您使用 Worksheet .CodeName property 而不是 Worksheet .Name property 来识别工作表有点奇怪。我提供了几个链接以确保您正确使用命名约定。无论如何,我使用With ... End With statement 来避免重复重新识别父工作表。

【讨论】:

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