+ Reply to Thread
Results 1 to 6 of 6

Code Produces Error When Trying to Operate a Particular Userform after Another

Hybrid View

JP777 Code Produces Error When... 04-29-2012, 12:15 PM
Charles Re: Code Produces Error When... 04-29-2012, 07:29 PM
Charles Re: Code Produces Error When... 04-29-2012, 07:44 PM
JP777 Re: Code Produces Error When... 04-29-2012, 07:49 PM
Charles Re: Code Produces Error When... 04-29-2012, 07:51 PM
JP777 Re: Code Produces Error When... 04-29-2012, 08:04 PM
  1. #1
    Registered User
    Join Date
    04-08-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    76

    Code Produces Error When Trying to Operate a Particular Userform after Another

    I have a program that plots voltage and current waveforms and digital inputs and outputs of power system relays. I am running into a problem that if I have plotted a digital plot, when I try to plot the waveforms I get an error. The line of code that highlights is the line of code that sets the X axis values for the plot. The only time this happens is when a waveform is attempted while a digital exists. You can plot waveforms, then go back and run the waveforms macro again, and everything will work fine. You can also do multiple digital plots in a row. Also, you can plot a waveform first and then plot a digital without flaw. The only issue is trying to plot a waveform after a digital. Please see the attached sample spreadsheet so that you can reproduce what I'm seeing. Just run the macros in whatever order so that you can get a better feel for what I am talking about. Please let me know if further clarification is needed. Does anyone know what the issue may be? My troubleshooting techniques are very novice with VBA...Thanks in advance for the help!
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor Charles's Avatar
    Join Date
    02-10-2004
    Location
    Biloxi
    MS-Off Ver
    Windows 7, Excel 2003,2007 & Mac2011
    Posts
    845

    Re: Code Produces Error When Trying to Operate a Particular Userform after Another

    Hi,

    How do you plot to a Digital?
    Give an example of what you first do the what it is that you do when you rec the error.
    Charles

    There are other ways to do this, this is but 1 !
    Be Sure you thank those who helped.
    IF YOU'RE SATISFIED BY ANY MEMBERS RESPONSE TO YOUR ISSUE PLEASE USE THE STAR ICON AT THE BOTTOM LEFT OF THE POST UNDER THEIR NAME.

  3. #3
    Valued Forum Contributor Charles's Avatar
    Join Date
    02-10-2004
    Location
    Biloxi
    MS-Off Ver
    Windows 7, Excel 2003,2007 & Mac2011
    Posts
    845

    Re: Code Produces Error When Trying to Operate a Particular Userform after Another

    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

  4. #4
    Registered User
    Join Date
    04-08-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    76

    Re: Code Produces Error When Trying to Operate a Particular Userform after Another

    Charles,
    That fixed it! Thanks! I have been running into a lot of situations where I have to activate a sheet. Don't know how it got past me...thanks again!

  5. #5
    Valued Forum Contributor Charles's Avatar
    Join Date
    02-10-2004
    Location
    Biloxi
    MS-Off Ver
    Windows 7, Excel 2003,2007 & Mac2011
    Posts
    845

    Re: Code Produces Error When Trying to Operate a Particular Userform after Another

    You are welcome and thanks for the "Rep".

  6. #6
    Registered User
    Join Date
    04-08-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    76

    Re: Code Produces Error When Trying to Operate a Particular Userform after Another

    no prob

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1