如何:Excel VBA命令按钮将UserForm值输入当前行?

时间:2017-01-06 16:38:31

标签: excel-vba vba excel

我目前有一个UserForm" UserForm1"从ComboBoxes获取值(课程,教师,开始时间等)并将它们保存到指定的工作表" InstructorHours"。 CommandButton" Save"将选定的值保存在" InstructorHours"的下一个可用行中。片。我遇到的问题是让CommandButton将相同的信息保存到另一个工作表" Calendar"中当前选定的行中。理想情况下,我想点击我的"日历"选择并将用户的值输入到我选择的行中。



Private Sub CommandButton1_Click()
 'Copy input values to sheet.
    Dim lRow As Long
    'next available row
    Dim ws As Worksheet
    Set ws = Worksheets("InstructorHours")
    lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    With ws
        .Cells(lRow, 1).Value = Me.ComboBox1.Value
        .Cells(lRow, 2).Value = Me.ComboBox2.Value
        .Cells(lRow, 3).Value = Me.ComboBox3.Value
        .Cells(lRow, 4).Value = Me.ComboBox4.Value
        .Cells(lRow, 5).Value = Me.ComboBox5.Value
        .Cells(lRow, 6).Value = Me.ComboBox6.Value
        .Cells(lRow, 7).Value = Me.TextBox1.Value
        End With
    'Clear input controls.
    Me.ComboBox1.Value = ""
    Me.ComboBox2.Value = ""
    Me.ComboBox3.Value = ""
    Me.ComboBox4.Value = ""
    Me.ComboBox5.Value = ""
    Me.ComboBox6.Value = ""
End Sub




非常感谢任何和所有帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

通常,如果您想处理某些活动的内容,您可能需要一些“活动”(例如ActiveSheet或ActiveCell)来进行主动选择活动。

我会为以下内容推荐另一个命令按钮(从头顶写下来,还没有验证代码):

Private Sub CommandButton2_Click()

'Input based on the selection
    ActiveCell.Value = Me.ComboBox1.Value
    ActiveCell.Offset(1,0).Value = Me.ComboBox2.Value
    ActiveCell.Offset(2,0).Value = Me.ComboBox3.Value
    ActiveCell.Offset(3,0).Value = Me.ComboBox4.Value
    ActiveCell.Offset(4,0).Value = Me.ComboBox5.Value
    ActiveCell.Offset(5,0).Value = Me.ComboBox6.Value
    ActiveCell.Offset(6,0).Value = Me.TextBox1.Value

'Clear input controls.
Me.ComboBox1.Value = ""
Me.ComboBox2.Value = ""
Me.ComboBox3.Value = ""
Me.ComboBox4.Value = ""
Me.ComboBox5.Value = ""
Me.ComboBox6.Value = ""

End Sub

至少应该提供一个起点。