我想在用户选择的一组行的一组行中循环遍历所有单元格。但是,我不确定如何完成此操作。
在选择行时命名列
我已经使用了打击代码来获取用户选择的范围/行,但是我不知道该如何从那里转到此范围内的命名列中的单元格。
Set rng = Application.InputBox("Select a range", "Obtain Range Object", Type:=8)
您可以使用Intersect
:
Sub Test()
Dim rng1 As Range, rng2 As Range, cl As Range
With ThisWorkbook.Sheets("Sheet1") 'Whichever sheet your working on
Set rng1 = Application.InputBox("Select a range", "Obtain Range Object", Type:=8)
Set rng2 = Intersect(rng1, .Range("COLE")) 'Whichever name your named range is.
For Each cl In rng2
'Do something
Next cl
End With
End Sub
请注意,在取消输入框后,这将丢弃一个错误,您可以通过某些错误处理来解决。
这是循环遍历rng
中每个单元格的两种方法,并证明我们访问了每个单元格。
Dim r as Range
For Each r in rng
r = "visited"
Next r
和
Dim i As Long
For i = 1 To rng.Cells.Count
rng.Cells(i) = "visited"
Next i