2013-04-03 398 views
2

我有一個引用excel表中的表的範圍。 我需要創建一個圖形,因此使用我的表格範圍設置源數據。 我希望源數據是我的表的第一行和最後兩行(標題和總共2行)。 考慮到範圍是使用變量形成的,有沒有一種方法可以選擇任何給定範圍的那些部分(假設範圍由三行或更多行組成)。 這裏是我到目前爲止的代碼:如何使用vba(excel)給定範圍的一部分,即範圍內的某些行

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 

回答

6

的邏輯是尋找第一行第一列總排數,的列數,然後推斷的範圍內,你想。讓我用一個例子來解釋它。我已經評論了代碼,以便您不應該有任何理解它的問題。

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$3:$B$3 
     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$3:$B$3,$A$9:$B$10 
     Set FinalRange = Union(rng1, rng2, rng3) 
     Debug.Print "The Final Range address is " & FinalRange.Address 
    End With 
End Sub 

當你運行上面的代碼你在即時窗口

Our Sample Range is $A$3:$B$10 
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$3:$B$3 
The Last Row address is $A$10:$B$10 
The Second Last Row address is $A$9:$B$9 
The Final Range address is $A$3:$B$3,$A$9:$B$10 

編輯

所以上面的代碼減去調試語句和正確的變量聲明可以寫成這樣輸出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 
+1

Hey Sid,+1 from – teylyn 2013-04-04 00:06:19

+0

Siddharth,謝謝。這段代碼對我來說效果很好,能夠做出如此快速,深思熟慮的響應真是太好了。 – Fletch 2013-04-04 18:38:45

+0

@Fletch:很高興有幫助:) – 2013-04-04 18:41:44