最初选择的单元格存储在rngStart
中,以便最后重新选择,因此宏不会将用户带走。但是,存储在rngStart
中的范围会更改。看似本身。最终是粘贴操作发生的范围。
Sub Macro2()
Application.ScreenUpdating = False
Dim rngStart 'The variable I'm struggling with
Dim ws As Worksheet
Set rngStart = Selection 'Store original selection
Set ws = ActiveSheet
Selection.Cut
'Find an empty cell in column B
For Each cell In ws.Columns(2).Cells
If IsEmpty(cell) = True Then cell.Select: Exit For
Next cell
ActiveSheet.Paste 'Upon executing this line, rngStart changes to the cell being pasted to
rngStart.Select 'Supposed to return to the originally selected range
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:1)
将其另存为字符串。
Sub Macro2()
Application.ScreenUpdating = False
Dim rngStart As String 'The variable I'm struggling with
Dim ws As Worksheet
rngStart = Selection.Address 'Store original selection
Set ws = ActiveSheet
Selection.Cut
'Find an empty cell in row B
For Each cell In ws.Columns(2).Cells
If IsEmpty(cell) = True Then cell.Select: Exit For
Next cell
ActiveSheet.Paste 'Upon executing this line, rngStart changes to the cell being pasted to
Range(rngStart).Select 'Supposed to return to the originally selected range
Application.ScreenUpdating = True
End Sub
答案 1 :(得分:0)
记住您的起始范围地址以返回该地址。还要注意,有一种更快的方法来查找列中的下一个空行:
Sub Macro2()
Application.ScreenUpdating = False
Dim ws As Worksheet
Set ws = ActiveSheet
Dim rngStart As Range
Set rngStart = Selection
Dim OriginalAddress As String
OriginalAddress = rngStart.Address
rngStart.Cut
ws.Cells(ws.Rows.Count, "B").End(xlUp).Offset(RowOffset:=1).Select
ws.Paste
ws.Range(OriginalAddress).Select
Application.ScreenUpdating = True
End Sub