我目前正試圖設置一個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