我尝试设置的是,用户可以从表中选择一行文本,然后单击按钮让Excel将选择复制到另一个工作表并插入到非固定的地址。目前,我尝试让它搜索特定文本并在该文本之后插入复制的行,但我遇到了错误91"对象变量或未设置块变量。"如果有更好的方法可以做到这一点,我全神贯顺。
Private Sub CommandButton1_Click()
Selection.Copy
Sheets("Form").Select
Dim FoundRange As Range
Dim RangeAddress As Range
Set FoundRange = Sheet3.Cells.Find("SIGN-ON")
RangeAddress = FoundRange.Address
RangeAddress.Selection
Selection.Insert shift:=xlDown
Sheets("MasterList").Select
End Sub
编辑:修正了代码中的拼写错误。
答案 0 :(得分:0)
您尝试将Range.Address property(字符串)分配到Range object(例如RangeAddress = FoundRange.Address
),Selection
是一个范围,不是像.Select
那样的动作。
Private Sub CommandButton1_Click()
Dim rngToCopy As Range
Dim FoundRange As Range
Set rngToCopy = Selection 'save the current selection so it won't be lost
With Sheets("Form")
On Error Resume Next
Set FoundRange = .Cells.Find("SIGN-ON")
On Error GoTo 0
If Not FoundRange Is Nothing Then
With FoundRange.Resize(rngToCopy.Rows.Count, rngToCopy.Columns.Count).Offset(1, 0)
rngToCopy.Copy
.Insert shift:=xlDown
Application.CutCopyMode = False
End With
End If
End With
End Sub
我通过摆脱依赖.Select
来减少操作。有关远离依赖选择和激活以实现目标的更多方法,请参阅How to avoid using Select in Excel VBA macros。