【发布时间】:2020-09-03 15:22:40
【问题描述】:
我有一个 Excel,它包含许多表格和图形数据,我想从中只选择两行并使用 VBA 将其转换为 XML 文件。我之前尝试的内容包含选择所有行和列,但我想选择从 2 到 3 的行和从 J 到 T 的列。 到目前为止我尝试了什么:
Sub FindUsedRange()
Dim LastRow As Long
Dim FirstRow As Long
Dim LastCol As Integer
Dim FirstCol As Integer
' Find the FIRST real row
FirstRow = ActiveSheet.Cells.Find(What:="*", _
SearchDirection:=xlNext, _
SearchOrder:=xlByRows).Row
' Find the FIRST real column
FirstCol = ActiveSheet.Cells.Find(What:="*", _
SearchDirection:=xlNext, _
SearchOrder:=xlByColumns).Column
' Find the LAST real row
LastRow = ActiveSheet.Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row
' Find the LAST real column
LastCol = ActiveSheet.Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns).Column
'Select the ACTUAL Used Range as identified by the
'variables identified above
'MsgBox (FirstRow & "," & LastRow & "," & FirstCol & "," & LastCol)
Dim topCel As Range
Dim bottomCel As Range
Set topCel = Cells(FirstRow, FirstCol)
Set bottomCel = Cells(LastRow, LastCol)
ActiveSheet.Range(topCel, bottomCel).Select
End Sub
但它会返回活动工作表中的所有内容。我想从(J2到T3)中选择,即J列和第2行到T列和第3行。它的固定格式所以我们可以直接提到行和列号。但我不知道如何更改此功能。
【问题讨论】:
-
如果您知道范围,那么只需使用
Dim rng as range: Set rng = Range("J2:T3")