Piano Forum

Topic: Programming Visual Basic in Excel  (Read 2339 times)

Offline pianolearner

  • PS Silver Member
  • Sr. Member
  • ***
  • Posts: 573
Programming Visual Basic in Excel
on: November 19, 2006, 08:02:01 AM
I posted this in a VB forum but I have not had a reply yet. I thought I might try here as well.

I am not a programmer, I just write code on rare occasions for a specific purpose. I have written an application using VB in Excel 97 to change a range within a chart. The purpose is to simulate a real time price chart for a stock market index. It works, but I'd like to remove bulky, unnecessary code.

My code looks like this:

Private Sub CommandButton2_Click()

Dim PauseTime, Start


PauseTime = 0.1 ' Set duration.
Start = Timer ' Set start time.
Do While Timer < Start + PauseTime
DoEvents ' Yield to other processes.
Loop


ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.ChartArea.Select
ActiveChart.ChartType = xlStockOHLC
ActiveChart.SetSourceData Source:=Sheets("e-mini_test").range("A1:F3"), _
PlotBy:=xlColumns

PauseTime = 0.1 ' Set duration.
Start = Timer ' Set start time.
Do While Timer < Start + PauseTime
DoEvents ' Yield to other processes.
Loop


.............

End Sub


What I have done is copied this 391 times in order to increment the range, but I'd like to know if I can use a for/next loop.

The line I'd like to increment contains the range value:

ActiveChart.SetSourceData Source:=Sheets("e-mini_test").range("A1:F3"), _
PlotBy:=xlColumns


I'd like to change the range after each pause so that it is A1:F3, A1:F4 etc


Is this possible? If so, how can I do it?

Thanks

Offline maul

  • PS Silver Member
  • Sr. Member
  • ***
  • Posts: 592
Re: Programming Visual Basic in Excel
Reply #1 on: November 19, 2006, 08:44:40 AM
This is the last place you should be looking for the solution to your problem, although I do have it.

Offline pianolearner

  • PS Silver Member
  • Sr. Member
  • ***
  • Posts: 573
Re: Programming Visual Basic in Excel
Reply #2 on: November 19, 2006, 11:08:26 AM
This is the last place you should be looking for the solution to your problem, although I do have it.

You think you do, but it's wrong.

Offline anodibu

  • PS Silver Member
  • Jr. Member
  • ***
  • Posts: 98
Re: Programming Visual Basic in Excel
Reply #3 on: November 19, 2006, 11:23:40 AM
How about this:

Private Sub CommandButton2_Click()

Dim PauseTime, Start, Range


PauseTime = 0.1 ' Set duration.
Start = Timer ' Set start time.
Do While Timer < Start + PauseTime
DoEvents ' Yield to other processes.
Loop
   
For Counter = 3 To 394  
    Range = "A1:F" & Trim(Str$(Counter))
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveChart.ChartArea.Select
    ActiveChart.ChartType = xlStockOHLC
    ActiveChart.SetSourceData Source:=Sheets("e-mini_test").Range(Range), _
    PlotBy:=xlColumns
   
   
    PauseTime = 0.1 ' Set duration.
    Start = Timer ' Set start time.
    Do While Timer < Start + PauseTime
    DoEvents ' Yield to other processes.
    Loop
Next

End Sub

Offline pianolearner

  • PS Silver Member
  • Sr. Member
  • ***
  • Posts: 573
Re: Programming Visual Basic in Excel
Reply #4 on: November 19, 2006, 02:44:37 PM
How about this:

Private Sub CommandButton2_Click()

Dim PauseTime, Start, Range


PauseTime = 0.1 ' Set duration.
Start = Timer ' Set start time.
Do While Timer < Start + PauseTime
DoEvents ' Yield to other processes.
Loop
   
For Counter = 3 To 394  
    Range = "A1:F" & Trim(Str$(Counter))
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveChart.ChartArea.Select
    ActiveChart.ChartType = xlStockOHLC
    ActiveChart.SetSourceData Source:=Sheets("e-mini_test").Range(Range), _
    PlotBy:=xlColumns
   
   
    PauseTime = 0.1 ' Set duration.
    Start = Timer ' Set start time.
    Do While Timer < Start + PauseTime
    DoEvents ' Yield to other processes.
    Loop
Next

End Sub

anodibu, you are a GENIUS! Works beautifully. Thanks very much.

You should join: https://www.xtremevbtalk.com/

They could probably use your help!

Offline anodibu

  • PS Silver Member
  • Jr. Member
  • ***
  • Posts: 98
Re: Programming Visual Basic in Excel
Reply #5 on: November 19, 2006, 03:20:59 PM
Glad that it helped (maul should reconsider his opinion hehe).

But actually I don't know that much of VB, I mostly work with C++ and C#.
For more information about this topic, click search below!

Piano Street Magazine:
New Piano Piece by Chopin Discovered – Free Piano Score

A previously unknown manuscript by Frédéric Chopin has been discovered at New York’s Morgan Library and Museum. The handwritten score is titled “Valse” and consists of 24 bars of music in the key of A minor and is considered a major discovery in the wold of classical piano music. Read more
 

Logo light pianostreet.com - the website for classical pianists, piano teachers, students and piano music enthusiasts.

Subscribe for unlimited access

Sign up

Follow us

Piano Street Digicert