【问题标题】:Double click event to fill textbox in form双击事件以填充表单中的文本框
【发布时间】:2016-09-23 00:10:25
【问题描述】:

我正在尝试使用工作表的双击事件来填充表单中的文本框,其中包含双击行中特定列的值。

例如

我有四列

ID Name Age Gender
1  A     24  M
2  B     26  F
3  C     22  F
4  D     30  M

当我双击Name 列中的任何单元格时,将弹出一个带有文本框的表单,该文本框填充了双击行中Gender 列中的值。所以当我双击名称列中的“B”时,应该会弹出一个带有文本框值“F”的表单。

这是我目前的代码

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, CANCEL As Boolean)
    CANCEL = True
    If Target.Column = 2 Then
        Update.Show
        With Update.TextBox1.value = ?????
        End With
    End If
End Sub

【问题讨论】:

  • 您正在寻找 Target.Value 的“??? .另请参阅Best Practices in the User Forms documentation
  • 它没有用,也不会 target.value 将用双击的单元格值填充文本框。我希望它填充双击单元格行上特定列的值
  • 抱歉,应该是Target.Offset(0, x).Value,其中x 是右侧的列数。

标签: excel vba


【解决方案1】:

使用名为 frm_Update 的表单和三个适当命名的文本框控件。
这将从Target 行、第 2、3 和 4 列获取值,并将值放入文本框中:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    Dim oForm As frm_Update

    If Target.Column = 2 Then
        Set oForm = New frm_Update

        Cancel = True
        With oForm
            .txtName = Cells(Target.Row, 2)
            .txtAge = Cells(Target.Row, 3)
            .txtGender = Cells(Target.Row, 4)
            .Show
        End With
    End If

End Sub

@Comintern - 我会好好阅读那个链接。我真正应该付诸实践的所有东西。

【讨论】:

    【解决方案2】:

    我使用双击事件打开表单&表单激活事件从活动单元格行中的列中获取值

    基本上是这样

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, CANCEL As Boolean)
        CANCEL = True
        If Target.Column = 2 Then Exit Sub
        Update.Show
    End Sub
    
    Private Sub UserForm_Activate()
    Dim r As Long
    
        r = ActiveCell.Row
    
        Me.TextBox1.value = Cells(r, 4).value
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-26
      • 1970-01-01
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多