PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Update Chart in a loop-VBForums
Results 1 to 2 of 2

Thread: Update Chart in a loop

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2019
    Posts
    17

    Update Chart in a loop

    Hello All,

    I am trying to update my chart inside a loop but have been unsuccessful.

    when I add Doevents in the code, I can get the chart to update but intermittently. also the update does not start until 4th or 5th data. I am trying to read the data off a multimeter. Any help would be appreciated.

    Code start Code:
    1. Sub CurrentGPIB()
    2.    
    3.     Dim ioMgr As VisaComLib.ResourceManager
    4.     Dim instrument As VisaComLib.FormattedIO488
    5.     Dim Current As Double
    6.     Dim Instradress As String
    7.        
    8.     Set ioMgr = New VisaComLib.ResourceManager
    9.     Set instrument = New VisaComLib.FormattedIO488
    10.     Instradress = ActiveWorkbook.Sheets("Information").Range("A1").Value
    11.     ' use instrument specific address for Open()
    12.     Set instrument.IO = ioMgr.Open("GPIB0::2")
    13.     instrument.WriteString "sense:CURR:DC:range:auto OFF"
    14.     'instrument.WriteString "sense:CURR:DC:range:100"
    15.    
    16.     Range("B3").Select
    17.        
    18.        Dim rng As Long
    19.        Dim rngf As Range
    20.        Dim cht As Object
    21.      
    22.        Set cht = ActiveSheet.Shapes.AddChart2
    23. cht.Name = "mychart"
    24. cht.Chart.ChartType = xlXYScatterLines
    25.  cht.Chart.HasTitle = True
    26. cht.Chart.ChartTitle.Text = "Current Monitoring"
    27. ActiveSheet.ChartObjects("mychart").Chart.Refresh
    28.  
    29.  
    30. Do
    31.  
    32. DoEvents
    33.  
    34.    ' If GetAsyncKeyState(VK_ENTER) = 0 Then
    35.    
    36.     instrument.WriteString "MEAS:CURR?"
    37.     Current = instrument.ReadString()
    38.     ActiveCell() = Now
    39.     ActiveCell.NumberFormat = "mm/dd/yyy h:mm:ss"
    40.     ActiveCell.Offset(0, 1).Select
    41.     ActiveCell.NumberFormat = "##0.000E+0"
    42.     ActiveCell() = Current
    43.     ActiveCell.Offset(1, -1).Select
    44.     rng = Range("C" & Rows.Count).End(xlUp).Row
    45.     Set rngf = ActiveSheet.Range("C3:C" & rng)
    46.     cht.Chart.SetSourceData Source:=rngf
    47.        
    48.      
    49.     ActiveSheet.ChartObjects("mychart").Chart.Refresh
    50.    
    51.       ' Else:
    52.        'MsgBox ("Monitoring Terminated")
    53.        'End
    54.    
    55.    'End If
    56.     Application.Wait (Now + TimeValue("0:00:02"))
    57.     Application.ScreenUpdating = True
    58.  
    59.      Loop
    60.    
    61. End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,248

    Re: Update Chart in a loop

    while i doubt it will solve your problem, you should avoid using the active cell and select, better like

    Code:
    Current = instrument.ReadString()
    set cel = Range("b" & Rows.Count).End(xlUp).Row
    with cel
        .value = Now
        .NumberFormat = "mm/dd/yyy h:mm:ss"
        .offset(1).NumberFormat = "##0.000E+0"
        .offset(1) = Current
        Set rngf = ActiveSheet.Range("C3:" & .address)
    end with
    cht.Chart.SetSourceData Source:=rngf
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width