在宏的开头保存选择并在宏的末尾将其设置为相同?

时间:2013-09-30 12:22:51

标签: vba excel-vba excel

我有一个宏来选择工作表中的内容。

在运行宏的主要部分之前,我想保存活动选择,以便我可以在宏的末尾设置相同的选择。

我已尝试过以下解决方案,但它不起作用。我很感激你的建议。

Dim rng As Range

'Beginning of macro
rng = Range(ActiveSheet.Selection) 'Object doesn't support this property or method

'Main section

'End of macro
rng.Select

2 个答案:

答案 0 :(得分:3)

而不是rng = Range(ActiveSheet.Selection),它应该是Set rng = Selection

答案 1 :(得分:2)

tmoore82提供的解决方案是最好的方法(+1)

为了完整性,您还可以将Address保存为字符串:

Dim selectionAddress as String
selectionAddress = Selection.Address //e.g. A1 is "$A$1"

//Your macro

Range(selectionAddress).Select //At end of macro select cell A1
相关问题