如何在 vba (excel) 中使用给定范围的一部分,即范围内的某些行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15799253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use part of a given range in vba (excel) i.e. certain rows in the range
提问by Fletch
I have a range that references a table in excel. I need to create a graph and therefore set the source data using my table range. I want the source data to be the first row and last 2 rows from my table (the headings and 2 total rows). Given that the range is formed using variables, is there a way of selecting those sections of any given range (assuming the range consists of three or more rows). Here's my code so far:
我有一个引用 excel 表格的范围。我需要创建一个图表,因此使用我的表格范围设置源数据。我希望源数据是我的表中的第一行和最后 2 行(标题和 2 总行)。鉴于范围是使用变量形成的,有没有办法选择任何给定范围的那些部分(假设范围由三行或更多行组成)。到目前为止,这是我的代码:
Sub addchart(ByVal TableRange As Range, SheetName As Worksheet, TblLabel As String, TableLabel As String)
Dim ChtPosition As Range
Dim ChtRow As Long
Dim ChtSourceData As Range
Set ChtSourceData = Union(Top row of TableRange here, Last 2 rows of TableRange here)
ChtRow = SheetName.Cells(SheetName.Rows.Count, "B").End(xlUp).Row + 2
ChtPosition = SheetName.Cells(ChtRow, 2)
SheetName.Shapes.addchart.Select
With ActiveChart
.SetSourceData Source:=SheetName.Range(ChtSourceData)
.ApplyChartTemplate ("\graphtemplatefilepath")
.Parent.Name = "Cht" & TblLabel
End With
With SheetName.ChartObjects("Cht" & TblLabel)
.Width = (16 * 29)
.Height = (7 * 29)
.Left = ChtPosition.Left
.Top = ChtPosition.Top
.Chart.ChartTitle.Characters.Text = TableLabel & " by Month"
End With
End Sub
回答by Siddharth Rout
The logic is to find the First Row, First Column, Total Number of rows, Total Number of columnsand then deduce the range that you want. Let me explain it with an example. I have commented the code so that you shouldn't have any problem understanding it.
逻辑是找到First Row、First Column、Total Number of rows、Total Number of columns,然后推断出你想要的范围。让我用一个例子来解释它。我已经对代码进行了注释,以便您理解它不会有任何问题。
Sub Sample()
Dim TestRange As Range
Dim rng1 As Range, rng2 As Range, rng3 As Range, FinalRange As Range
Dim ws As Worksheet
Dim r As Long, c As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
Set TestRange = .Range("A3:B10")
Debug.Print "Our Sample Range is " & TestRange.Address
'~~> This will give you 8
Debug.Print "Total Rows in the range is " & TestRange.Rows.Count
'~~> This will give you 3 From A3
Debug.Print "The First Row in the range is " & TestRange.Row
'~~> This will give you 1 From A3
Debug.Print "The First Column in the range is " & TestRange.Column
r = TestRange.Row
c = TestRange.Column
'~~> This will give you 2
Debug.Print "Total Columns in the range is " & TestRange.Columns.Count
ColCount = TestRange.Columns.Count
'~~> This is give you the 1st row in that range $A:$B
Debug.Print "The First Row address is " & Range(.Cells(r, c), _
.Cells(r, c + ColCount - 1)).Address
Set rng1 = Range(.Cells(r, c), .Cells(r, c + ColCount - 1))
'~~> This will give you the last row
Debug.Print "The Last Row address is " & _
Range(.Cells(r + TestRange.Rows.Count - 1, c), _
.Cells(r + TestRange.Rows.Count - 1, c + ColCount - 1)).Address
Set rng2 = Range(.Cells(r + TestRange.Rows.Count - 1, c), _
.Cells(r + TestRange.Rows.Count - 1, c + ColCount - 1))
'~~> This will give you the Second last row
Debug.Print "The Second Last Row address is " & _
Range(.Cells(r + TestRange.Rows.Count - 2, c), _
.Cells(r + TestRange.Rows.Count - 2, c + ColCount - 1)).Address
Set rng3 = Range(.Cells(r + TestRange.Rows.Count - 2, c), _
.Cells(r + TestRange.Rows.Count - 2, c + ColCount - 1))
'~~> This will give you the final range i.e $A:$B,$A:$B
Set FinalRange = Union(rng1, rng2, rng3)
Debug.Print "The Final Range address is " & FinalRange.Address
End With
End Sub
When you run the above code you get this output in the Immediate Window
当你运行上面的代码时,你会在立即窗口中得到这个输出
Our Sample Range is $A:$B
Total Rows in the range is 8
The First Row in the range is 3
The First Column in the range is 1
Total Columns in the range is 2
The First Row address is $A:$B
The Last Row address is $A:$B
The Second Last Row address is $A:$B
The Final Range address is $A:$B,$A:$B
EDIT
编辑
So the above code minus the debug statements and proper variable declaration can be written as
所以上面的代码减去调试语句和正确的变量声明可以写成
Sub Sample()
Dim TestRange As Range
Dim rng1 As Range, rng2 As Range, rng3 As Range, FinalRange As Range
Dim ws As Worksheet
Dim r1 As Long, c1 As Long, rCount As Long, cCount As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
Set TestRange = .Range("A3:B10")
rCount = TestRange.Rows.Count
r1 = TestRange.Row
cCount = TestRange.Columns.Count
c1 = TestRange.Column
Set rng1 = Range(.Cells(r1, c1), .Cells(r1, c1 + cCount - 1))
Set rng2 = Range(.Cells(r1 + rCount - 1, c1), _
.Cells(r1 + rCount - 1, c1 + cCount - 1))
Set rng3 = Range(.Cells(r1 + rCount - 2, c1), _
.Cells(r1 + rCount - 2, c1 + cCount - 1))
Set FinalRange = Union(rng1, rng2, rng3)
Debug.Print "The Final Range address is " & FinalRange.Address
End With
End Sub