+ Reply to Thread
Results 1 to 13 of 13

VBA to create a chart when data can vary in column position and rows

  1. #1
    Registered User
    Join Date
    01-04-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    17

    VBA to create a chart when data can vary in column position and rows

    Hi everyone.

    I'm attempting to create a macro/VBA to draw some scatterplots, and I need your help!

    What I want to do is create a code to automatically look for a column by name, since column order can vary depending what data is exported, and select that column and another column, and create a scatterplot from that data on a new worksheet. My problem is that I don't know how to select column by name as opposed to just column letter. Also, the data can vary in length, so I need to figure out how to select the last row.

    I tried Record Macro to get the basic code for creating a scatterplot, but when I tried to re-run it, it came back with an error. Also, it doesn't include selecting columns by name, nor selecting data down to the last row which will vary.

    I've attached a worksheet with sample data on sheet1 and desired scatterplots on sheet2.

    Any and all help is greatly appreciated!

    Attachment 256572

    Please Login or Register  to view this content.
    chart macro.xlsm

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: VBA to create a chart when data can vary in column position and rows

    Here is one way. Just change the x and y names to use

    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    01-04-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: VBA to create a chart when data can vary in column position and rows

    Thanks Andy!

    I apologize for being some new to VBA, but when you say "change the x and y names to use", what x and y are you referring to? NameX, NameY? rngDataX, rngDataY?

    Thanks again for your help!

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: VBA to create a chart when data can vary in column position and rows

    I mean the names in the Test macro

    Please Login or Register  to view this content.
    These are the field names to use for x and y values

  5. #5
    Registered User
    Join Date
    01-04-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: VBA to create a chart when data can vary in column position and rows

    Excellent! Thank you very much, this is perfect!!

    One more question, I've been trying to figure this out but no luck so far: How can I add something right at the beginning that looks for certain header names, and if not found, displays a message box saying "xxx not found"

    I'd like to add something like this because currently if a field is not found a runtime error comes up, which would be confusing to the person using this VBA.

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: VBA to create a chart when data can vary in column position and rows

    you can use the worksheet match function.
    Assume you fieldnames are in range A1:M1 then something like

    Please Login or Register  to view this content.
    * not tested

  7. #7
    Registered User
    Join Date
    01-04-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: VBA to create a chart when data can vary in column position and rows

    Andy - thanks again for all your help. You have been a tremendous asset, and I thank you for that. I have one more question. I was wondering if you could help me understand what the first part of the Function CreatePlot is doing, specifically the vntCol part. I've included the code below for reference. Thank you.

    Please Login or Register  to view this content.

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: VBA to create a chart when data can vary in column position and rows

    that is simply locating the field name by using the MATCH function. If the match is successful the a reference to the range using that column can be set.
    If not successful then the function is exited.

  9. #9
    Registered User
    Join Date
    01-04-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: VBA to create a chart when data can vary in column position and rows

    Okay, that makes sense, thank you!

    Is there anything in that part that I would need to adapt to work with column headers it's looking for? I ask this because when I run this function, and a match is not found, I get a Run-time error '1004' Unable to get the Match property of the WorksheetFunction class.

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: VBA to create a chart when data can vary in column position and rows

    yes sorry with application.worksheetfunction.Match you would need error trapping code.

    Use just application.Match which will return error code to vntCol. The rest of the code is actually checking for non error being returned.

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    01-04-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: VBA to create a chart when data can vary in column position and rows

    Awesome, thanks again Andy! I feel silly asking another questions, especially since you've already been so patient with me, but I really want to understand what's going on so I can figure this out.

    Making the change you posted above, I get "Unable to locate data..." if the column headers do not match what is needed. However, after I press OK, it does not exit the function. Instead, it goes and tries to create the first scatterplot with the missing data. Now I get "Run-time error '91': Object variable or With block variable not set". When I debug, the code is highlighted in the second part, where it's creating the first scatterplot, at "With chtTemp.Chart"

    Please Login or Register  to view this content.

  12. #12
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: VBA to create a chart when data can vary in column position and rows

    Please Login or Register  to view this content.
    check to see returned reference is valid

  13. #13
    Registered User
    Join Date
    01-04-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: VBA to create a chart when data can vary in column position and rows

    Awesome, works perfectly!!! Thanks again for all your help Andy!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Using VBA to create a chart for 7 rows of data that have column lengths of 10 to 500
    By churchie27 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-25-2012, 10:25 AM
  2. Replies: 2
    Last Post: 04-25-2012, 04:53 AM
  3. Need help to set Chart to select data that doesnt have a fixed column position.
    By rocksan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-25-2012, 03:11 AM
  4. vba to create chart in certain position after data is entered
    By buckfran in forum Excel Charting & Pivots
    Replies: 14
    Last Post: 08-15-2009, 07:00 AM
  5. Sum of columns that vary in position
    By gibsol in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 08-17-2006, 10:55 AM

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