Hi,
I added a line of code to activate sheet3.
Private Sub OKButton_Click()
Dim intLbxIndex As Integer 'ListBox (Lbx) index number
Dim rngCell As Range, rngMyRange As Range
Dim rngChtData As Range
Dim rngChtXVal As Range
Dim iColumn As Long
Dim theChart As Chart
Dim FirstSeries As Boolean
Dim max_cyc As Integer
Dim activsht As Variant
' check if worksheet or chart sheet and delete if they do
Application.DisplayAlerts = False
For Each activsht In ActiveWorkbook.Sheets
If activsht.Name = "Current Waveforms" Then
activsht.Delete
ElseIf activsht.Name = "Voltage Waveforms" Then
activsht.Delete
End If
Next activsht
Sheets("Analog_Data").Activate''''Added this line
Set rngChtXVal = Range(Sheet3.Cells(2, 1), Sheet3.Cells(Sheet3.Cells(Rows.Count, 1).End(xlUp).Row, 1))
max_cyc = Sheet1.Range("D6").Value
' Make the line graph for current
FirstSeries = True
For intLbxIndex = 0 To ListBox2.ListCount - 1
For Each rngCell In Sheet3.Range("B1:J1")
If (CStr(rngCell.Value) = CStr(ListBox2.List(intLbxIndex)) _
And Mid(CStr(rngCell.Value), 1, 1) = "I") Then
' add the chart when the first series is encountered
If (FirstSeries) Then
FirstSeries = False
Set theChart = Charts.Add
With theChart
.ChartType = xlXYScatterSmoothNoMarkers
.Name = "Current Waveforms"
.Axes(xlCategory).MaximumScale = max_cyc
.Move After:=Sheets(Sheets.Count)
End With
With theChart.Axes(xlCategory, xlPrimary)
.HasTitle = True
With .AxisTitle
.Characters.Text = "Time (Cycles)"
End With
End With
With theChart.Axes(xlValue, xlPrimary)
.HasTitle = True
With .AxisTitle
.Characters.Text = "Current (A)"
End With
End With
' remove any series already place on the chart
Do Until theChart.SeriesCollection.Count = 0
theChart.SeriesCollection(1).Delete
Loop
End If
' add this series to the chart
iColumn = rngCell.Column
Set rngChtData = Range(Sheet3.Cells(2, iColumn), Sheet3.Cells(Sheet3.Cells(Sheet3.Rows.Count, iColumn).End(xlUp).Row, iColumn))
With theChart.SeriesCollection.NewSeries
.Values = rngChtData
.XValues = rngChtXVal
.Name = Sheet3.Cells(rngChtData.Row - 1, rngChtData.Column)
End With
End If
Next rngCell
Next intLbxIndex
' Make the line graph for voltage
FirstSeries = True
For intLbxIndex = 0 To ListBox2.ListCount - 1
For Each rngCell In Sheet3.Range("B1:J1")
If (CStr(rngCell.Value) = CStr(ListBox2.List(intLbxIndex)) _
And Mid(CStr(rngCell.Value), 1, 1) = "V") Then
' add the chart when the first series is encountered
If (FirstSeries) Then
FirstSeries = False
Set theChart = Charts.Add
With theChart
.ChartType = xlXYScatterSmoothNoMarkers
.Name = "Voltage Waveforms"
.Axes(xlCategory).MaximumScale = max_cyc
.Move After:=Sheets(Sheets.Count)
End With
With theChart.Axes(xlCategory, xlPrimary)
.HasTitle = True
With .AxisTitle
.Characters.Text = "Time (Cycles)"
End With
End With
With theChart.Axes(xlValue, xlPrimary)
.HasTitle = True
With .AxisTitle
.Characters.Text = "Voltage (kV)"
End With
End With
' remove any series already place on the chart
Do Until theChart.SeriesCollection.Count = 0
theChart.SeriesCollection(1).Delete
Loop
End If
' add this series to the chart
iColumn = rngCell.Column
Set rngChtData = Range(Sheet3.Cells(2, iColumn), Sheet3.Cells(Sheet3.Cells(Sheet3.Rows.Count, 1).End(xlUp).Row, iColumn))
With theChart.SeriesCollection.NewSeries
.Values = rngChtData
.XValues = rngChtXVal
.Name = Sheet3.Cells(rngChtData.Row - 1, rngChtData.Column)
End With
End If
Next rngCell
Next intLbxIndex
Unload Me
End Sub
Bookmarks