我目前正试图设置一个Do While
循环,其中循环的一部分只执行一次。我正在有效地尝试为一个工作表定义一个单元格范围,然后让我的循环将相同的单元格范围应用于所有工作表/工作簿,而无需重新指定范围。循环后忘记变量值VBA
这是我到目前为止有:
isExecuted = False
Do While FileName <> ""
' Open a workbook in the folder
Set WorkBk = Workbooks.Open(Folder & "\" & FileName)
' Conditional to select range only once
If Not isExecuted Then
Dim rng As Range
Set rng = Application.InputBox("Select a Range", "Obtain Range Object", Type:=8)
Debug.Print (rng.Address)
MsgBox "The cells selected were " & rng.Address
isExecuted = True
End If
Set SourceRange = WorkBk.Worksheets(1).Range(rng.Address)
' more stuff goes here
在调试模式。这个循环第一次执行所有预期的工作,我可以看到我的rng.Address是指定的单元格范围。但是,在第二个循环rng.Address变为<ObjectRequired>
,所以脚本的其余部分失败。有关如何将rng.Address永久设置到指定单元格区域的任何想法?
你在关闭“更多的东西在这里”的wb? – exSnake