【问题标题】:how to code userform to only enter data in a specific row如何对用户表单进行编码以仅在特定行中输入数据
【发布时间】:2022-01-11 14:38:13
【问题描述】:

我正在尝试编写一个用户表单来填充一个有 13 行的表。每行代表机器中用于工具的插槽,我希望用户选择一个插槽,表单将用信息填充该行或停止用户,因为该行中已经有数据。到目前为止我编码的是它只会为每个条目生成一个新行。有谁能帮帮我吗?

谢谢, 肖恩。

Private Sub CommandButton1_Click()
Dim wks As Worksheet
Dim addnew As Range
Set wksDMG = Sheet4
Set wksHURCO = Sheet3
Set wksREC1 = Sheet2
Set wksREC2 = Sheet5



'if DMG is selected input data into DMG files  NEED HELP HERE
 If OptionButton1.Value = True Then

 Set addnew = wksDMG.Range("A65356").End(xlUp).Offset(1, 0)
 addnew.Offset(0, 0).Value = ComboBox7.Text
 addnew.Offset(0, 1).Value = ComboBox2.Text
 addnew.Offset(0, 2).Value = ComboBox3.Text
 addnew.Offset(0, 3).Value = ComboBox4.Text
 addnew.Offset(0, 4).Value = ComboBox5.Text
 addnew.Offset(0, 5).Value = ComboBox6.Text


'Data to go to  DMG record log

Set addnew = wksREC1.Range("A65356").End(xlUp).Offset(1, 0)
addnew.Offset(0, 0).Value = ComboBox1.Text
addnew.Offset(0, 1).Value = TextBox1.Text
addnew.Offset(0, 2).Value = ComboBox7.Text
addnew.Offset(0, 3).Value = ComboBox2.Text
addnew.Offset(0, 4).Value = ComboBox3.Text
addnew.Offset(0, 5).Value = ComboBox4.Text
addnew.Offset(0, 6).Value = ComboBox5.Text
addnew.Offset(0, 7).Value = ComboBox6.Text
End If

'if Hurco is selected input data into Hurco files  NEED HELP HERE
 If OptionButton2.Value = True Then

 Set addnew = wksHURCO.Range("A65356").End(xlUp).Offset(1, 0)
 addnew.Offset(0, 0).Value = ComboBox7.Text
 addnew.Offset(0, 1).Value = ComboBox2.Text
 addnew.Offset(0, 2).Value = ComboBox3.Text
 addnew.Offset(0, 3).Value = ComboBox4.Text
 addnew.Offset(0, 4).Value = ComboBox5.Text
 addnew.Offset(0, 5).Value = ComboBox6.Text

'Data to go to  HURCO record log

 Set addnew = wksREC2.Range("A65356").End(xlUp).Offset(1, 0)
 addnew.Offset(0, 0).Value = ComboBox1.Text
 addnew.Offset(0, 1).Value = TextBox1.Text
 addnew.Offset(0, 2).Value = ComboBox7.Text
 addnew.Offset(0, 3).Value = ComboBox2.Text
 addnew.Offset(0, 4).Value = ComboBox3.Text
 addnew.Offset(0, 5).Value = ComboBox4.Text
 addnew.Offset(0, 6).Value = ComboBox5.Text
 addnew.Offset(0, 7).Value = ComboBox6.Text

 End If


 End Sub

【问题讨论】:

  • 当单元格被选中时,这会打开要完成的用户表单,还是您打算让用户表单已经完成,然后选择单元格(而不是cmd按钮)来传输数据到工作表?
  • 嗨塞缪尔,我在上面附上了用户表单的图片以供参考。用户表单“工具槽”中的最后一行是指条目所在的行,即 1-13。

标签: excel vba userform


【解决方案1】:

您的解决方案应捕获所选工具槽的值,然后根据选择计算行号。您没有提供足够的信息来提供更多详细信息,但这里有一个示例:

Option Explicit

Private toolSlotRow As Long

Private Sub AddButton_Click()
    If toolSlotRow = -99 Then
        MsgBox "You must select a tool slot before adding your stuff!"
        Exit Sub
    End If
    
    Dim wksDMG As Worksheet
    Set wksDMG = sheet4
    
    Dim tools As Range
    Set tools = wksDMG.Range("A1:Z100")

    '--- check the tool slot row for data
    If IsEmpty(tools.Offset(toolSlotRow, 0)) Then
        '--- empty row, add the tool info
        With tools
            .Offset(0, 0).Value = ComboBox7.Text
            .Offset(0, 1).Value = ComboBox2.Text
            .Offset(0, 2).Value = ComboBox3.Text
            .Offset(0, 3).Value = ComboBox4.Text
            .Offset(0, 4).Value = ComboBox5.Text
            .Offset(0, 5).Value = ComboBox6.Text
        End With
    Else
        MsgBox "The tool slot selected already has data, do something else!"
    End If
End Sub

Private Sub ToolSlotComboBox_Change()
    '--- also check here for "no selection" and set an appropriate value
    If ToolSlotComboBox.ListIndex = 0 Then
        '--- this might be the "default - unselected" value
        toolSlotRow = -99
    Else
        toolSlotRow = ToolSlotComboBox.ListIndex
    End If
End Sub

【讨论】:

    猜你喜欢
    • 2019-03-19
    • 2021-11-17
    • 2019-03-24
    • 2017-11-11
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    相关资源
    最近更新 更多