繁体   English   中英

将refedit合并到Vlookup用户表单中

[英]Incorporating refedit into Vlookup userform

我有一个vlookup用户表单,可根据座位编号自动填写表单中的详细信息。

在此处输入图片说明

现在,我想对裁判编辑进行非比例运算,以将这些数据从文本框中粘贴到用户使用裁判编辑选择的单元格中 因此,在进行这些操作时,我需要一些帮助。
这是我使用的代码。

我可能要插入 3条RefEdit控件框,让用户选择他们要粘贴的每个从文本数据( 姓名部门分机号 )的细胞

请参阅下面的代码:

Option Explicit

Private Sub Frame1_Click()

End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

   Dim answer As Integer
   answer = TextBox1.Value
   TextBox2.Value = WorksheetFunction.VLookup(answer, Sheets("L12 - Data Sheet").Range("B:E"), 2, False)
   TextBox3.Value = WorksheetFunction.VLookup(answer, Sheets("L12 - Data Sheet").Range("B:E"), 3, False)
   TextBox4.Value = WorksheetFunction.VLookup(answer, Sheets("L12 - Data Sheet").Range("B:E"), 4, False)

End Sub

Private Sub TextBox2_Change()

End Sub

Private Sub TextBox3_Change()

End Sub

Private Sub TextBox4_Change()

End Sub
Private Sub CancelButton_Click()

    Unload Me
    End
End Sub

我试图弄清楚代码来解决此问题,但出现了对象必需错误。 我的rngcopy将是textbox2.value(名称),而rngpaste位置将是参考编辑1。

这是代码

Private Sub PasteButton_Click()

Dim rngCopy As Range, rngPaste As Range
Dim wsPaste As Range
Dim answer As Integer
answer = TextBox1.Value
If RefEdit1.Value <> "" Then

        TextBox2.Value = WorksheetFunction.VLookup(answer, Sheets("L12 - Data Sheet").Range("B:E"), 2, False)
        Set rngCopy = TextBox2.Value
        Set wsPaste = ThisWorkbook.Sheets(Replace(Split(TextBox2.Value, "!")(0), "'", ""))
        Set rngPaste = wsPaste.Range(Split(TextBox2.Value, "!")(1))

        rngCopy.Copy rngPaste
  Else
        MsgBox "Please select an Output range"
    End If
End Sub

您应该使用Match获取行索引,并将其公开给表单,以便复制功能可以使用它。 并设置Ref控件指向的目标,只需使用Range()评估.Value属性:

Range(RefEdit.Value).cells(1, 1) = Worksheet.Cells(row, column)

表格:

在此处输入图片说明

编码:

' constants to define the data
Const SHEET_DATA = "L12 - Data Sheet"
Const COLUMN_SEAT = "B"
Const COLUMNN_NAME = "C"
Const COLUMN_DEPT = "D"
Const COLUMN_EXTNO = "E"

Private Sheet As Worksheet
Private RowIndex As Long

Private Sub TxtSeatNo_Change()
  Dim seatno

  'clear the fields first
  Me.TxtName.value = Empty
  Me.TxtDept.value = Empty
  Me.TxtExtNo.value = Empty
  RowIndex = 0

  If Len(TxtSeatNo.value) Then
    Set Sheet = ThisWorkbook.Sheets(SHEET_DATA)

    On Error Resume Next

    ' get the seat number to either string or double
    seatno = TxtSeatNo.value
    seatno = CDbl(seatno)

    ' get the row index containing the SeatNo
    RowIndex = WorksheetFunction.match(seatno, _
                                       Sheet.Columns(COLUMN_SEAT), _
                                       0)
    On Error GoTo 0
  End If

  If RowIndex Then
    ' copy the values from the sheet to the text boxes
    Me.TxtName.value = Sheet.Cells(RowIndex, COLUMNN_NAME)
    Me.TxtDept.value = Sheet.Cells(RowIndex, COLUMN_DEPT)
    Me.TxtExtNo.value = Sheet.Cells(RowIndex, COLUMN_EXTNO)
  End If
End Sub

Private Sub BtCopy_Click()
  If RowIndex < 1 Then Exit Sub

  ' copy the current values to the cells pointed by the ref controls

  If Len(Me.RefName.value) Then _
    Range(Me.RefName.value) = Sheet.Cells(RowIndex, COLUMNN_NAME)

  If Len(Me.RefDept.value) Then _
    Range(Me.RefDept.value) = Sheet.Cells(RowIndex, COLUMN_DEPT)

  If Len(Me.RefExtNo.value) Then _
    Range(Me.RefExtNo.value) = Sheet.Cells(RowIndex, COLUMN_EXTNO)
End Sub

Private Sub BtlClose_Click()
  ' close the form
  Unload Me
End Sub

您将rngCopy声明为Range对象 ,然后将其绑定到Range对象.value 方法

Set rngCopy = TextBox2.Value

这可能是您遇到错误的地方。 尝试声明一个string并将其分配给您的副本值。

Dim string1 As String
string1 = TextBox2.Value

LOCALS窗口打开的情况下rngCopy代码编辑器,并观察为rngCopy对象分配字符串时发生的情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM