2017-11-17 14 views
0

Excelのグラフに新しいシリーズとして多数のデータポイントを追加するマクロを作成しようとしています。VBA - 2つの文字列の間に範囲をグラフとして追加しようとしています

私は現在、ボルタモグラムデータをCSVファイルからスプレッドシートに2つの列(PotentialとCurrent)にインポートします。このデータは、それぞれ500〜1500個の値を含む43サイクルで構成されています(ソフトウェアを吐き出すポイントの数が制御できないため、複雑になります)。

サイクル「Potential *」の各サイクルの開始時および各サイクルの最後に表示される文字列「PN」をスキャンしたい場合は、これらの値をグラフに追加してから次のサイクル。

さまざまな検索のいくつかの異なるソリューションを組み合わせて、ときどき動作するばかげた解決策を得ました。私は、各サイクルに同じ数のポイントが含まれていてもそれがうまくいくと確信していますが、残念ながらそうではありません。

私の厄介なソリューション:

Sub InsertChart() 

Dim first As Long, last As Long 
first = 4 
last = Range(Range("A:A").Find("*Potential*").Offset(1), Range("A:A").Find("*PN*", Range("A:A").Find("*Potential*")).Offset(-1, 1)).Cells.Count 


Dim wsChart As Worksheet 
Set wsChart = Sheets(1) 

wsChart.Activate 
wsChart.Shapes.AddChart.Select 

Dim chart As chart 
Set chart = ActiveChart 
chart.ChartType = xlXYScatter 
    ActiveChart.ClearToMatchStyle 
    ActiveChart.ChartStyle = 240 
    ActiveChart.Axes(xlCategory).MinimumScale = 0 
    ActiveChart.Axes(xlCategory).MaximumScale = 1.5 
    ActiveChart.Axes(xlCategory).MajorUnit = 0.1 

' adding series 
chart.SeriesCollection.NewSeries 
chart.SeriesCollection(1).Name = "Cycle 01" 
chart.SeriesCollection(1).XValues = "=" & ActiveSheet.Name & "!$A$" & first & ":$A$" & last 
chart.SeriesCollection(1).Values = "=" & ActiveSheet.Name & "!$B$" & first & ":$B$" & last 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(2).Name = "Cycle 02" 
chart.SeriesCollection(2).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 2) + 4 & ":$A$" & last * 3 
chart.SeriesCollection(2).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 2) + 4 & ":$B$" & last * 3 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(3).Name = "Cycle 03" 
chart.SeriesCollection(3).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 3) + 4 & ":$A$" & last * 4 
chart.SeriesCollection(3).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 3) + 4 & ":$B$" & last * 4 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(4).Name = "Cycle 04" 
chart.SeriesCollection(4).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 4) + 4 & ":$A$" & last * 5 
chart.SeriesCollection(4).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 4) + 4 & ":$B$" & last * 5 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(5).Name = "Cycle 05" 
chart.SeriesCollection(5).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 5) + 4 & ":$A$" & last * 6 
chart.SeriesCollection(5).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 5) + 4 & ":$B$" & last * 6 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(6).Name = "Cycle 06" 
chart.SeriesCollection(6).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 6) + 4 & ":$A$" & last * 7 
chart.SeriesCollection(6).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 6) + 4 & ":$B$" & last * 7 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(7).Name = "Cycle 07" 
chart.SeriesCollection(7).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 7) + 4 & ":$A$" & last * 8 
chart.SeriesCollection(7).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 7) + 4 & ":$B$" & last * 8 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(8).Name = "Cycle 08" 
chart.SeriesCollection(8).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 8) + 4 & ":$A$" & last * 9 
chart.SeriesCollection(8).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 8) + 4 & ":$B$" & last * 9 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(9).Name = "Cycle 09" 
chart.SeriesCollection(9).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 9) + 4 & ":$A$" & last * 10 
chart.SeriesCollection(9).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 9) + 4 & ":$B$" & last * 10 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(10).Name = "Cycle 10" 
chart.SeriesCollection(10).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 10) + 4 & ":$A$" & last * 11 
chart.SeriesCollection(10).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 10) + 4 & ":$B$" & last * 11 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(11).Name = "Cycle 11" 
chart.SeriesCollection(11).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 11) + 4 & ":$A$" & last * 12 
chart.SeriesCollection(11).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 11) + 4 & ":$B$" & last * 12 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(12).Name = "Cycle 12" 
chart.SeriesCollection(12).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 12) + 4 & ":$A$" & last * 13 
chart.SeriesCollection(12).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 12) + 4 & ":$B$" & last * 13 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(13).Name = "Cycle 13" 
chart.SeriesCollection(13).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 13) + 4 & ":$A$" & last * 14 
chart.SeriesCollection(13).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 13) + 4 & ":$B$" & last * 14 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(14).Name = "Cycle 14" 
chart.SeriesCollection(14).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 14) + 4 & ":$A$" & last * 15 
chart.SeriesCollection(14).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 14) + 4 & ":$B$" & last * 15 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(15).Name = "Cycle 15" 
chart.SeriesCollection(15).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 15) + 4 & ":$A$" & last * 16 
chart.SeriesCollection(15).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 15) + 4 & ":$B$" & last * 16 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(16).Name = "Cycle 16" 
chart.SeriesCollection(16).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 16) + 4 & ":$A$" & last * 17 
chart.SeriesCollection(16).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 16) + 4 & ":$B$" & last * 17 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(17).Name = "Cycle 17" 
chart.SeriesCollection(17).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 17) + 4 & ":$A$" & last * 18 
chart.SeriesCollection(17).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 17) + 4 & ":$B$" & last * 18 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(18).Name = "Cycle 18" 
chart.SeriesCollection(18).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 18) + 4 & ":$A$" & last * 19 
chart.SeriesCollection(18).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 18) + 4 & ":$B$" & last * 19 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(19).Name = "Cycle 19" 
chart.SeriesCollection(19).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 19) + 4 & ":$A$" & last * 20 
chart.SeriesCollection(19).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 19) + 4 & ":$B$" & last * 20 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(20).Name = "Cycle 20" 
chart.SeriesCollection(20).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 20) + 4 & ":$A$" & last * 21 
chart.SeriesCollection(20).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 20) + 4 & ":$B$" & last * 21 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(21).Name = "Cycle 21" 
chart.SeriesCollection(21).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 21) + 4 & ":$A$" & last * 22 
chart.SeriesCollection(21).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 21) + 4 & ":$B$" & last * 22 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(22).Name = "Cycle 22" 
chart.SeriesCollection(22).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 22) + 4 & ":$A$" & last * 23 
chart.SeriesCollection(22).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 22) + 4 & ":$B$" & last * 23 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(23).Name = "Cycle 23" 
chart.SeriesCollection(23).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 23) + 4 & ":$A$" & last * 24 
chart.SeriesCollection(23).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 23) + 4 & ":$B$" & last * 24 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(21).Name = "Cycle 24" 
chart.SeriesCollection(21).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 24) + 4 & ":$A$" & last * 25 
chart.SeriesCollection(21).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 24) + 4 & ":$B$" & last * 25 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(25).Name = "Cycle 25" 
chart.SeriesCollection(25).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 25) + 4 & ":$A$" & last * 26 
chart.SeriesCollection(25).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 25) + 4 & ":$B$" & last * 26 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(26).Name = "Cycle 26" 
chart.SeriesCollection(26).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 26) + 4 & ":$A$" & last * 27 
chart.SeriesCollection(26).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 26) + 4 & ":$B$" & last * 27 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(27).Name = "Cycle 27" 
chart.SeriesCollection(27).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 27) + 4 & ":$A$" & last * 28 
chart.SeriesCollection(27).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 27) + 4 & ":$B$" & last * 28 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(28).Name = "Cycle 28" 
chart.SeriesCollection(28).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 28) + 4 & ":$A$" & last * 29 
chart.SeriesCollection(28).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 28) + 4 & ":$B$" & last * 29 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(29).Name = "Cycle 29" 
chart.SeriesCollection(29).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 29) + 4 & ":$A$" & last * 30 
chart.SeriesCollection(29).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 29) + 4 & ":$B$" & last * 30 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(30).Name = "Cycle 30" 
chart.SeriesCollection(30).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 30) + 4 & ":$A$" & last * 31 
chart.SeriesCollection(30).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 30) + 4 & ":$B$" & last * 31 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(31).Name = "Cycle 31" 
chart.SeriesCollection(31).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 31) + 4 & ":$A$" & last * 32 
chart.SeriesCollection(31).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 31) + 4 & ":$B$" & last * 32 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(32).Name = "Cycle 32" 
chart.SeriesCollection(32).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 32) + 4 & ":$A$" & last * 33 
chart.SeriesCollection(32).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 32) + 4 & ":$B$" & last * 33 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(33).Name = "Cycle 33" 
chart.SeriesCollection(33).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 33) + 4 & ":$A$" & last * 34 
chart.SeriesCollection(33).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 33) + 4 & ":$B$" & last * 34 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(34).Name = "Cycle 34" 
chart.SeriesCollection(34).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 34) + 4 & ":$A$" & last * 35 
chart.SeriesCollection(34).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 34) + 4 & ":$B$" & last * 35 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(35).Name = "Cycle 35" 
chart.SeriesCollection(35).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 35) + 4 & ":$A$" & last * 36 
chart.SeriesCollection(35).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 35) + 4 & ":$B$" & last * 36 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(36).Name = "Cycle 36" 
chart.SeriesCollection(36).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 36) + 4 & ":$A$" & last * 37 
chart.SeriesCollection(36).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 36) + 4 & ":$B$" & last * 37 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(37).Name = "Cycle 37" 
chart.SeriesCollection(37).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 37) + 4 & ":$A$" & last * 38 
chart.SeriesCollection(37).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 37) + 4 & ":$B$" & last * 38 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(38).Name = "Cycle 38" 
chart.SeriesCollection(38).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 38) + 4 & ":$A$" & last * 39 
chart.SeriesCollection(38).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 38) + 4 & ":$B$" & last * 39 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(39).Name = "Cycle 39" 
chart.SeriesCollection(39).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 39) + 4 & ":$A$" & last * 40 
chart.SeriesCollection(39).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 39) + 4 & ":$B$" & last * 40 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(40).Name = "Cycle 40" 
chart.SeriesCollection(40).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 40) + 4 & ":$A$" & last * 41 
chart.SeriesCollection(40).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 40) + 4 & ":$B$" & last * 41 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(41).Name = "Cycle 41" 
chart.SeriesCollection(41).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 41) + 4 & ":$A$" & last * 42 
chart.SeriesCollection(41).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 41) + 4 & ":$B$" & last * 42 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(42).Name = "Cycle 42" 
chart.SeriesCollection(42).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 42) + 4 & ":$A$" & last * 43 
chart.SeriesCollection(42).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 42) + 4 & ":$B$" & last * 43 

chart.SeriesCollection.NewSeries 
chart.SeriesCollection(43).Name = "Cycle 43" 
chart.SeriesCollection(43).XValues = "=" & ActiveSheet.Name & "!$A$" & (last * 43) + 4 & ":$A$" & last * 44 
chart.SeriesCollection(43).Values = "=" & ActiveSheet.Name & "!$B$" & (last * 43) + 4 & ":$B$" & last * 44 
End Sub 

右方向に任意のヘルプやポインタは素晴らしいことです。

あなたの助けを借りて、コードを使用するようにマクロを更新しました。 Y値(現在)のみが0として出力されますが、コードを調整してデータで動作させるのは自分の責任である可能性があります。

'Plots the Excel Data 
Sub Voltammogram() 

'Add Chart 
Dim wsChart As Worksheet 
Set wsChart = Sheets(1) 

wsChart.Activate 
wsChart.Shapes.AddChart.Select 

Dim chart As chart 
    Set chart = ActiveChart 
Dim chartsize As Range 
    Set chartsize = ActiveSheet.Range("D1:o20") 
Dim Chart_obj As ChartObject 
    Set Chart_obj = ActiveSheet.ChartObjects(1) 

chart.ChartType = xlXYScatter 
chart.HasTitle = True 
chart.ChartTitle.Text = ActiveSheet.Name 
chart.Axes(xlCategory, xlPrimary).HasTitle = True 
ActiveChart.Axes(xlCategory).AxisTitle.Text = "Potential (V)" 
chart.Axes(xlValue, xlPrimary).HasTitle = True 
ActiveChart.Axes(xlValue).AxisTitle.Text = "Current (uA)" 
ActiveChart.Axes(xlCategory).MinimumScale = 0 
ActiveChart.Axes(xlCategory).MaximumScale = 1.5 
ActiveChart.Axes(xlCategory).MajorUnit = 0.1 
ActiveChart.ChartStyle = 240 
Chart_obj.Height = chartsize.Height 
Chart_obj.Width = chartsize.Width 
Chart_obj.Top = chartsize.Top 
Chart_obj.Left = chartsize.Left 
ActiveChart.ChartArea.ClearContents 

'Find Last Row 
    Dim LastRow As Long 
    With ActiveSheet 
     LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 
    End With 
    Cells(LastRow + 2, 1).Value = "PN" 
    Cells(LastRow + 4, 1).Value = "END" 

'End of Data Flag 
Dim End_of_data As Boolean 
End_of_data = False 

'Chart Object 
'Dim Chart_obj As ChartObject 
Set Chart_obj = ActiveSheet.ChartObjects(1) 

'Series Variable 
Dim MySeries As Series 
Dim Series_Col As SeriesCollection 
Set Series_Col = Chart_obj.chart.SeriesCollection 

'Range Offset Variable 
Dim i As Integer 
i = 3 

'Series Index Value 
Dim si As Integer 
si = 1 

'Value Variables 
Dim Voltage, Current As String 

'Value Collections 
Dim Vol_List As New Collection 
Dim Cur_List As New Collection 

'Looping through the Sheet collecting Cycle data 
While (End_of_data = False) 

    Voltage = ActiveSheet.Range("A1").Offset(i, 0).Value 
    Current = ActiveSheet.Range("A1").Offset(i, 1).Value 

    'Checking for a Cycle End 
    If (Voltage = "PN") Then 
     'Incrementing the series index at the end of cycle 
     si = si + 1 

     'Creating the Series with all of the cycle data 
     Set MySeries = Series_Col.NewSeries 
     MySeries.Values = Collection_To_Array(Cur_List) 
     MySeries.XValues = Collection_To_Array(Vol_List) 
     MySeries.Name = "Cycle " & (si - 1) 


     'Clearing out the List 
     Set Vol_List = New Collection 
     Set Cur_List = New Collection 

    'Checking for end of data. This assumes there are no blanks in your data collection. You may need to update this logic 
    ElseIf (Voltage = "END") Then 

     'Exiting the Loop 
     End_of_data = True 
    Else 

     'Adding the Data to the List 
     If (Voltage <> "Potential" And Voltage <> "PN") Then 
      Vol_List.Add (Voltage) 
      Cur_List.Add (Current) 
     End If 
    End If 


    'Incrementing the offset Counter 
    i = i + 1 
Wend 

End Sub 

Function Collection_To_Array(c As Collection) As Variant() 
    Length = c.Count - 1 
    Dim A() As Variant 
    ReDim A(Length) 
    Dim i As Integer 
    For i = 1 To Length + 1 
     A(i - 1) = c.Item(i)  'Arrays are zero based and collections are 1 based 
    Next 
    Collection_To_Array = A 
End Function 

編集:0と表示されるY値の問題は、暗電圧、暗電流、電圧の切り替えを忘れてしまったために発生しました。新しい問題は、値が記録されており、グラフ上の「ビューデータ」に表示されていてもプロットされていないことです。また、21サイクル(約30000データポイント)後にオーバーフローエラーが発生します。私は不可視のプロットは、私が修正することができなければならない空のセルによって引き起こされると思うが、オーバーフローの原因を見つけることができない。

'Plots the Excel Data 
Sub Voltammogram() 

'Add Chart 
Dim chart As chart 
Dim chartsize As Range 
Dim Chart_obj As ChartObject 
Dim VolMin As Double 
    VolMin = WorksheetFunction.Min(Range("A:A")) 
Dim VolMax As Double 
    VolMax = WorksheetFunction.Max(Range("A:A")) 

ActiveSheet.Shapes.AddChart.Select 
    Set chart = ActiveChart 
    Set chartsize = ActiveSheet.Range("D1:O20") 
    Set Chart_obj = ActiveSheet.ChartObjects(1) 
    chart.ChartType = xlXYScatter 
    chart.HasTitle = True 
    chart.ChartTitle.Text = ActiveSheet.Name 
    chart.Axes(xlCategory, xlPrimary).HasTitle = True 
    ActiveChart.Axes(xlCategory).AxisTitle.Text = "Potential (V)" 
    chart.Axes(xlValue, xlPrimary).HasTitle = True 
    ActiveChart.Axes(xlValue).AxisTitle.Text = "Current (uA)" 
    ActiveChart.Axes(xlCategory).MinimumScale = VolMin 
    ActiveChart.Axes(xlCategory).MaximumScale = VolMax 
    ActiveChart.Axes(xlCategory).MajorUnit = 0.1 
    ActiveChart.ChartStyle = 240 
    ActiveChart.Axes(xlCategory).TickLabelPosition = xlLow 
    Chart_obj.Height = chartsize.Height 
    Chart_obj.Width = chartsize.Width 
    Chart_obj.Top = chartsize.Top 
    Chart_obj.Left = chartsize.Left 
    ActiveChart.ChartArea.ClearContents 

'Find Last Row 
    Dim LastRow As Long 
    With ActiveSheet 
     LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 
    End With 
    If Not (Cells(LastRow, 1) = "END") Then 
     Cells(LastRow + 2, 1).Value = "PN" 
     Cells(LastRow + 4, 1).Value = "END" 
    End If 

'End of Data Flag 
Dim End_of_data As Boolean 
End_of_data = False 

'Chart Object 
'Dim Chart_obj As ChartObject 
'Set Chart_obj = ActiveSheet.ChartObjects(1) 

'Series Variable 
Dim MySeries As Series 
Dim Series_Col As SeriesCollection 
Set Series_Col = Chart_obj.chart.SeriesCollection 

'Range Offset Variable 
Dim i As Integer 
i = 2 

'Series Index Value 
Dim si As Integer 
si = 1 

'Value Variables 
Dim Current, Voltage As String 

'Value Collections 
Dim Vol_List As New Collection 
Dim Cur_List As New Collection 

'Looping through the Sheet collecting Cycle data 
While (End_of_data = False) 

    Voltage = ActiveSheet.Range("A1").Offset(i, 0).Value 
    Current = ActiveSheet.Range("A1").Offset(i, 1).Value 

    'Checking for a Cycle End 
    If (Voltage = "PN") Then 
     'Incrementing the series index at the end of cycle 
     si = si + 1 

     'Creating the Series with all of the cycle data 
     Set MySeries = Series_Col.NewSeries 
     MySeries.Values = Collection_To_Array(Cur_List) 
     MySeries.XValues = Collection_To_Array(Vol_List) 
     MySeries.Name = "Cycle " & (si - 1) 

     'Clearing out the List 
     Set Vol_List = New Collection 
     Set Cur_List = New Collection 

    'Checking for end of data. This assumes there are no blanks in your data collection. You may need to update this logic 
    ElseIf (Voltage = "END") Then 

     'Exiting the Loop 
     End_of_data = True 
    Else 

     'Adding the Data to the List 
     If (Voltage <> "Potential" And Voltage <> "PN") Then 
      Vol_List.Add (Voltage) 
      Cur_List.Add (Current) 
     End If 
    End If 


    'Incrementing the offset Counter 
    i = i + 1 
Wend 

End Sub 

Function Collection_To_Array(c As Collection) As Variant() 
    c.Remove (1) 
    c.Remove (c.Count) 
    Length = c.Count - 1 
    Dim a() As Variant 
    ReDim a(Length) 
    Dim i As Integer 
    For i = 1 To Length + 1 
     a(i - 1) = c.Item(i)  'Arrays are zero based and collections are 1 based 
    Next 
    Collection_To_Array = a 

End Function 
+0

(最初のセルを見つけた後)PNを見つけるために2つの発見し、電位と第2を見つけることが最初のを行います。ルックアップエラーを回避する前に値が見つかったかどうかを確認する必要があるため、ヘルプを検索してください。 – SJR

答えて

0
'Plots the Excel Data 
Sub Plot_data() 

'End of Data Flag 
Dim End_of_data As Boolean 
End_of_data = False 

'Chart Object 
Dim Chart_obj As ChartObject 
Set Chart_obj = Sheet6.ChartObjects(1) 

'Series Variable 
Dim MySeries As Series 
Dim Series_Col As SeriesCollection 
Set Series_Col = Chart_obj.Chart.SeriesCollection 

'Range Offset Variable 
Dim i As Integer 
i = 0 

'Series Index Value 
Dim si As Integer 
si = 1 

'Value Variables 
Dim Voltage, Current As String 

'Value Collections 
Dim Vol_List As New Collection 
Dim Cur_List As New Collection 

'Looping through the Sheet collecting Cycle data 
While (End_of_data = False) 

    Voltage = Sheet6.Range("A1").Offset(i, 0).value 
    Current = Sheet6.Range("A1").Offset(i, 1).value 

    'Checking for a Cycle End 
    If (Voltage = "PN") Then 
     'Incrementing the series index at the end of cycle 
     si = si + 1 

     'Creating the Series with all of the cycle data 
     Set MySeries = Series_Col.NewSeries 
     MySeries.Values = Sheet2.Collection_To_Array(Vol_List) 
     MySeries.XValues = Sheet2.Collection_To_Array(Cur_List) 

     'Clearing out the List 
     Set Vol_List = New Collection 
     Set Cur_List = New Collection 

    'Checking for end of data. This assumes there are no blanks in your data collection. You may need to update this logic 
    ElseIf (Voltage = "") Then 

     'Exiting the Loop 
     End_of_data = True 
    Else 

     'Adding the Data to the List 
     If (Voltage <> "Potential" And Voltage <> "PN") Then 
      Vol_List.Add (Voltage) 
      Cur_List.Add (Current) 
     End If 
    End If 


    'Incrementing the offset Counter 
    i = i + 1 
Wend 

End Sub 

Function Collection_To_Array(c As Collection) As Variant() 
    Length = c.Count - 1 
    Dim A() As Variant 
    ReDim A(Length) 
    Dim i As Integer 
    For i = 1 To Length + 1 
     A(i - 1) = c.Item(i)  'Arrays are zero based and collections are 1 based 
    Next 
    Collection_To_Array = A 
End Function 
+0

これは、あなたがPotentialをA列に、CurrentをB列に持っていると仮定して、データの書式に基づいて少し微調整する必要があるかもしれません。ループは空白の範囲を想定しているため、データが終了しているので、これを念頭に置いてください。また、Excelシートには空白の図が必要です。私は散布図をお勧めします – Dammer15

+0

ねえ、ありがとう。それは私のデータで動作するようにいくつかの調整が必要でしたが、それは本当に近いようです。 X値(電位)をプロットすることはできますが、Y値はすべて0になります。私はコレクションで何かを混乱させましたか?私は最近VBAで働き始めたばかりです – vossyan

関連する問題