[英]Assign an incoherent range to a variable
我試圖將一個不連貫的范圍分配給一個變量,但我似乎無法做到正確。 誰能幫幫我嗎?
提前致謝!
我的代碼如下所示:
Dim tstRange As Range
Dim strRange As String
Select Case strSite
Case "Lerum/Aspedalen"
strRange = "B" & iLastRow & ", H" & iLastRow & ":L" & iLastRow
With Kurtans_favorit
Set tstRange = .Range(strRange)
Set tstRange = .Range("B" & iLastRow, "H" & iLastRow & ":L" & iLastRow)
End With
將范圍分配給“tstRange”的第一種和第二種方法都不起作用。 好吧,實際上第一個確實如此,但它只是給了我范圍內第一個單元格的內容,這不是我的意圖。
我打算使用這個范圍來檢查它們是否包含任何值。 如果他們這樣做,用戶將收到一個問題,是否覆蓋或取消他/她正在執行的操作。
看看這個例子:
Option Explicit
Private Sub OlaJohnson()
PrintAllRangeMembers RangeString:="A1,C1:E1"
End Sub
Private Sub PrintAllRangeMembers(RangeString As String)
' Purpose:
' Iterate through all members of a range and print them to the Immediate window
Dim IncoRange As Range
Set IncoRange = ThisWorkbook.Sheets("Sheet1").Range(RangeString)
Dim Cell As Variant
For Each Cell In IncoRange
Debug.Print Cell
Next Cell
End Sub
Sub PrintAllRangeMembers
將Range
(是否一致)作為參數,通過For Each
循環遍歷所有成員,並將值打印到即時 window。
您可以在For Each
循環中執行您需要進行的任何檢查。 打印單元格的值只是為了證明預期的行為結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.