+ Reply to Thread
Results 1 to 11 of 11

Selecting a range of columns based on the last column

  1. #1
    Registered User
    Join Date
    11-29-2018
    Location
    Sunny Cairns, QLD AUST
    MS-Off Ver
    2010
    Posts
    97

    Selecting a range of columns based on the last column

    Hi All,

    I have some code that finds the last column that contains a date. If this column is AD or somthing like that the range will be B to AD and this is too much. How can I select just the last 10 columns?

    Also if there are less than 10 columns I need it to be from B to what ever the last coloumn is. eg. B to G

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,259

    Re: Selecting a range of columns based on the last column

    Post your current code to find the last column.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Registered User
    Join Date
    11-29-2018
    Location
    Sunny Cairns, QLD AUST
    MS-Off Ver
    2010
    Posts
    97

    Re: Selecting a range of columns based on the last column

    This first bit finds the last date cell.


    Please Login or Register  to view this content.
    This bit calls the first one and then substitutes "cell" with "lttr" which is the last date cell.

    Please Login or Register  to view this content.

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,259

    Re: Selecting a range of columns based on the last column

    Changed your function to this.
    Please Login or Register  to view this content.
    In case lastcolumis AD it gives you
    Please Login or Register  to view this content.
    In your Port code I changed

    Please Login or Register  to view this content.
    to this

    Please Login or Register  to view this content.
    Which gives you firstlttr = "T" and lttr = "AD"
    Now you can play around with both variables to suit your need.

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,345

    Re: Selecting a range of columns based on the last column

    What I see is -- rather than pulling the data the chart(s) need into a nice, contiguous range that the chart can easily reference -- you are setting each chart's source data to a complex, multi-area range. As one who never uses VBA for charting, I would probably have done something that pulled the necessary data into a contiguous range, then used dynamic named ranges to build the chart data series. But, if you are happy with this, and it is working for you, then we should be able to work with this.

    It appears that the charts exist before the procedure is run, which also suggests that, if we get it working right, each chart can have multiple data series in it (before the code is run). It should be possible to do this in a way that will not completely redo each data series.

    In my experience, when you use the .SetSourceData method to change a chart's data series, this can cause Excel to completely re-evaluate all of the data series in the chart. In most charts (that I see) where you have a second very different data series for the horizontal line (often using a combination chart), using the .SetSourceData method can undo anything you have done to create these effects. Is there an external requirement to use the ,SetSourceData method?

    I would expect to edit each data series individually. Maybe something like:
    Please Login or Register  to view this content.
    If A) the chart exists before the code runs and B) the necessary data series for the horizontal line also exists and C) the ranges where the max/min/horizontal line data does not change, then this small change to this part of the code should allow you to change the main data series without changing the horizontal lines. You would then be able to add the horizontal lines to the chart (however you did that before this part of the project) and they will stay in the chart.

    If you are required to use .SetSourceData or there is something else that I am missing, then I could see modifying this code to add/edit the horizontal line data series after setting the source data to get the horizontal line in the chart. But it ultimately still boils down to the same thing -- retaining or adding or editing a second horizontal line series in each chart. As noted, I don't use VBA to do my charting, so this is not the kind of VBA coding I am good at (I would tend to find ways to do this that don't use VBA). If you will help us understand exactly what the charts look like before this code is run and exactly how you want to change the chart, those here who are good with VBA will be able to help come up with something that will edit the data series and still retain or add back in the horizontal lines.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  6. #6
    Registered User
    Join Date
    11-29-2018
    Location
    Sunny Cairns, QLD AUST
    MS-Off Ver
    2010
    Posts
    97

    Re: Selecting a range of columns based on the last column

    Thank you. I will give this a try a bit later.

  7. #7
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Selecting a range of columns based on the last column

    This is strictly to answer your request in your first Post.
    After I had this done, I seen all the other Posts.
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    11-29-2018
    Location
    Sunny Cairns, QLD AUST
    MS-Off Ver
    2010
    Posts
    97

    Re: Selecting a range of columns based on the last column

    Bakerman2, it sort of works with a bit of tuning but only on the X axis dates. The actual data is still showing the full 20 odd points. Here is what I did to get it to do something.

    Please Login or Register  to view this content.

  9. #9
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,259

    Re: Selecting a range of columns based on the last column

    I never, ever use charts so this is a shot in the dark but what if you change this too.

    Please Login or Register  to view this content.
    just like you did with rfrnce.

  10. #10
    Registered User
    Join Date
    11-29-2018
    Location
    Sunny Cairns, QLD AUST
    MS-Off Ver
    2010
    Posts
    97

    Re: Selecting a range of columns based on the last column

    Sorry, I don't understand what you mean.

    Also this has been done this way because the range I am using is not contiguous. Unfortunately the rows were selected before I got hold of this. If it was my choice it would have been done a lot differently.

  11. #11
    Registered User
    Join Date
    11-29-2018
    Location
    Sunny Cairns, QLD AUST
    MS-Off Ver
    2010
    Posts
    97

    Re: Selecting a range of columns based on the last column

    I have spent the morning looking at this and I think I have worked out what you are trying to say but not being a VBA wizard I don't think it is possible. I have only been playing with this for a few months on and off when I get some spare time.

+ 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. [SOLVED] Selecting range in a column based on another cell value
    By nekmat97 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-12-2018, 02:11 AM
  2. Selecting a range of columns based on a cell value
    By sa1ntj0hn in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-18-2016, 10:05 AM
  3. Selecting data range based on another column's values
    By ostbert in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 06-28-2014, 06:23 AM
  4. [SOLVED] Selecting all data in specific columns without selecting adjacent column.
    By EnigmaMatter in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-11-2014, 02:20 AM
  5. Selecting a column range based on a value in header row
    By Ad83 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-05-2012, 05:29 PM
  6. Selecting a column range based
    By dems in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-09-2010, 11:47 AM
  7. Selecting range based on adjacent column
    By yaju1120 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-05-2007, 10:41 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