excel - 为什么选择范围变量时会发生变化?
问题描述
最初选择的单元格被存储在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
解决方案
将其保存为字符串。
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
推荐阅读
- go - 时间:在测试时,我如何将上下文传递到工作流和活动中?
- github - Github 操作将特定文件夹从一个分支复制到同一 repo 中的另一个分支
- python - 检查列表中的重叠
- google-chrome-extension - 未添加 Chrome 扩展的内容脚本
- java - 如何按嵌套多重集中的值排序?
- java - 使用 CompletableFuture 提供同步和异步方法作为 API 的一部分
- powerbi - PowerBI如何将堆栈条形图嵌入为条形的百分比
- javascript - 如何将 onclick 返回到 kendoGrid 列?
- java - 如何在java中的双向链表中快速排序
- c# - 当字符串从另一个类更改时,我可以触发事件吗?