Consulting

Results 1 to 5 of 5

Thread: Plot charts with data position unfixed

  1. #1

    Lightbulb Plot charts with data position unfixed

    Hello, all.

    I now want to plot, say, speed vs time using vb in excel.
    The problem I have is that, the "speed" data is sometimes located at column O, while it can also be at column P, Q or R depending on the program that generats the file.

    Would someone please tell me how to do that?

    Appreciate it.

  2. #2
    I tried to use a do...loop to track the column titled "speed" and then get a number "15".

    But then I couldn't substitute that number into the following code.

    ActiveChart.SetSourceData Source:=Sheets("data").Range("H1:H10928,O1:O10928") _
    ' , PlotBy:=xlColumns

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Untested

    [vba]

    colNum = Application.Match("speed",Rows(1),0)
    ActiveChart.SetSourceData Source:=Sheets("data").Cells(1,colNum).Resize(10928)
    [/vba]

    How did you come to 10928?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Bravo!
    When I recorded the macro, I selected the whole column. I guess that's why the number is so big.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I thought it would be something liek that.

    We can improve upon it

    [vba]

    colNum = Application.Match("speed",Rows(1),0)
    rowNum = Cells(Rows.Count,colNum).End(xlUp).Row
    ActiveChart.SetSourceData Source:=Sheets("data").Cells(1,colNum).Resize(rowNum)
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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