我想使用行和列信息选择单元格范围。我们的想法是选择整列和整行,然后选择重叠区域。是否可以在Excel VBA中执行此操作? The idea visualized here
答案 0 :(得分:2)
最简单的方法是使用Cells()
:
Sub jlkj()
Dim ws As Worksheet
Dim StartRow As Long
Dim EndRow As Long
Dim StartClm As Long
Dim EndClm As Long
Dim rng As Range
StartRow = 6
EndRow = 10
StartClm = 5
EndClm = 5
Set ws = Sheets("Sheet1")
With ws
Set rng = .Range(.Cells(StartRow, StartClm), .Cells(EndRow, EndClm))
End With
Debug.Print rng.Address
End Sub
如果您想使用列字母,即“E”而不是数字,那么:
Sub jlkj()
Dim ws As Worksheet
Dim StartRow As Long
Dim EndRow As Long
Dim StartClm As String
Dim EndClm As String
Dim rng As Range
StartRow = 6
EndRow = 10
StartClm = "E"
EndClm = "E"
Set ws = Sheets("Sheet1")
With ws
Set rng = .Range(.Cells(StartRow, StartClm), .Cells(EndRow, EndClm))
End With
Debug.Print rng.Address
End Sub
答案 1 :(得分:2)
首先选择横截面,然后运行以下代码
Sub SelectionCrossSection()
Dim wRi As Range: Set wRi = Selection
Dim wR1 As Range: Set wR1 = wRi.Areas(1)
Dim wR2 As Range: Set wR2 = wRi.Areas(2)
Dim wRo As Range: Set wRo = Intersect(wR1, wR2)
wRo.Select
End Sub