2014-07-24 21 views
0

我有創建散點圖(時間與一列數據)的代碼。有第三欄說明數據是「通過」還是「失敗」(數據來自一組樣本經過測試)。基於合格/不合格標準的VBA Creat圖表

我想知道,有沒有一種算法,可以通過第三列進行排序,並基於如果它說「通過」或「失敗」,它將相應的時間和數據單元格偏移在同一行一系列?將會有兩個系列:一個用於通過,另一個用於失敗。 x值是時間,y值是數據。

我已經寫好的代碼是創建與該系列中的圖如下,以及它如何(動態變化)發現的數據範圍:

Sub AddCharts() 

Dim TSht As Worksheet 
Dim Cht as Chart 

With TSht 

'Find last row in data 
LRow = .Cells(.Rows.Count, 1).End(xlUp).Row 

'Create Chart 
Set Cht = .Shapes.AddChart.Chart 
    With Cht 
    .ChartType = xlXYScatter 
    .SeriesCollection.NewSeries 
     With .SeriesCollection(1) 
     .XValues = TSht.Range(Cells(2, 6), Cells(LRow, 6)) 
     .Values = TSht.Range(Cells(2, 7), Cells(LRow, 7)) 
     End With 
    End With 
End With 

End Sub 
+0

如果你問一個新問題,基本上和你剛剛問過的一個小時前的問題一樣*,也許你應該考慮刪除第一個問題,或者至少把指針放到最新的問題上...... –

回答

1

測試:

Sub AddCharts() 

Dim TSht As Worksheet 
Dim Cht As Chart, LRow As Long 
Dim xP As Range, yP As Range, xF As Range, yF As Range 
Dim rng As Range, c As Range 

    Set TSht = ActiveSheet 

    With TSht 

    'Find last row in data 
    LRow = .Cells(.Rows.Count, 1).End(xlUp).Row 

    'e.g. Pass/Fail is in Col 5 
    Set rng = .Range(.Cells(2, 5), .Cells(LRow, 5)) 
    For Each c In rng.Cells 
     If c.Value = "Pass" Then 
      AddTo xP, c.Offset(0, 1) 
      AddTo yP, c.Offset(0, 2) 
     End If 
     If c.Value = "Fail" Then 
      AddTo xF, c.Offset(0, 1) 
      AddTo yF, c.Offset(0, 2) 
     End If 
    Next c 

    'Create Chart 
    Set Cht = .Shapes.AddChart.Chart 
     With Cht 
      .ChartType = xlXYScatter 

      With .SeriesCollection.NewSeries 
       .Name = "Pass" 
       .XValues = xP 
       .Values = yP 
      End With 

      With .SeriesCollection.NewSeries 
       .Name = "Fail" 
       .XValues = xF 
       .Values = yF 
      End With 

     End With 
    End With 

End Sub 

'utility sub for building up X and Y ranges 
Sub AddTo(rng As Range, rngAdd As Range) 
    If rng Is Nothing Then 
     Set rng = rngAdd 
    Else 
     Set rng = Application.Union(rng, rngAdd) 
    End If 
End Sub 
+0

這正是我期待的因爲,謝謝蒂姆。此外,我知道我發佈了相同的問題,我應該只是編輯它或刪除它,我是相當新的stackoverflow,謝謝你的頭。只是認爲這種格式很容易理解我想要做什麼 – art123456

+0

沒問題 - 通常最好是編輯你的原始問題:這種方式沒有人花時間試圖幫助解決已經在其他地方已經有答案的問題。 –