我试过找出一个代码来从文本框值中复制信息,并使用引用编辑来粘贴该信息。但是我在Set rngCopy = TextBox2.Value
上收到了一个对象必需的错误。因此,我需要一些帮助,以解释为什么我会收到此错误。我的rngcopy将是textbox2.value(Name),而rngpaste位置将是ref edit 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
答案 0 :(得分:1)
编辑以粘贴链接返回的单元格地址
已修改以允许粘贴指向不同的工作表
Option Explicit
Private Sub PasteButton_Click()
Dim refRng As Range, fRng As Range
With Me
Set refRng = getRange(.RefEdit1.Value)
If refRng Is Nothing Then
MsgBox "Please select an Output range"
.RefEdit1.SetFocus
Else
Set fRng = FindValue(Sheets("L12 - Data Sheet").Range("B:B"), CInt(.TextBox1.Value))
If fRng Is Nothing Then
MsgBox "No match!"
.TextBox1.SetFocus
Else
.TextBox2.Value = fRng.Offset(, 1).Value
refRng.Formula = "='" & fRng.Parent.Name & "'!" & fRng.Offset(, 1).address
End If
End If
End With
End Sub
Function FindValue(rngToSearchIn As Range, valueToFind As Variant) As Range
On Error Resume Next
Set FindValue = rngToSearchIn.Find(what:=valueToFind, LookIn:=xlValues, LookAt:=xlWhole)
On Error GoTo 0
End Function
Function getRange(address As String) As Range
On Error Resume Next
Set getRange = Range(address)
On Error GoTo 0
End Function
首先确保RefEdit1
控件中的用户输入符合有效范围,因为奇怪的是,RefEdit
控件允许任何输入..