+ Reply to Thread
Results 1 to 8 of 8

Discontiguous Column Selection

  1. #1
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Discontiguous Column Selection

    I have a macro that boils out the differences between the cells in Column A and Column B. I've had this one for years. Not posting it because the existing code isn't the issue.

    This morning I saw two macros at http://www.excelfox.com/forum/f22/hi...d-column-2047/ that also work on columns A and B, which reminded me that there's a change I want to make to my original macro.

    What I'm looking for is a way to adapt either my original macro, or the two at the link above, to handle 2 separated columns that I've selected, so I'm limited to not only A:B, but instead can compare C with F or G with H.

    I think all I need to do is set a variable to each column (ie Col1 would be "C", Col2 would be "F"). Where I'm confused is, if I have two different columns selected, how do I tell VBA that "the column on the left is Col1, the column on the right is Col2"? Any help would be appreciated.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,598

    Re: Discontiguous Column Selection

    Just use two inputboxes to get the address and set the variables for each.
    Ben Van Johnson

  3. #3
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Discontiguous Column Selection

    I could go that route, but that would require my user to read InputBox1, select Col1 range, then click to accept, then read InputBox2, select Col2 range, then click to accept. I was hoping there would be a way to do it with their already-selected columns. For instance, if their selection at the beginning of the Macro ISN'T two column, go the inputbox way. If it IS 2 columns, then compare the left one to the right one. That's where I'm getting stuck.

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,811

    Re: Discontiguous Column Selection

    What I'm looking for is a way to adapt either my original macro, or the two at the link above, to handle 2 separated columns that I've selected, so I'm limited to not only A:B, but instead can compare C with F or G with H.
    Assuming you are already familiar with the basics of Excel's object model, I think what you are looking for is the Areas object: https://msdn.microsoft.com/EN-US/lib.../ff197473.aspx When you select two separate columns, your selection will consist of two areas, and you can write your code to see each area separately.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Discontiguous Column Selection

    if the user has selected A:B you'll only get one area-unless they selected one column and then ctrl+clicked the next so you've gotta test for that too
    easy way is
    Please Login or Register  to view this content.
    that'll work with entire columns or just a range within two columns
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  6. #6
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Discontiguous Column Selection

    Thanks Mr. Shorty, that really gets me started. By counting columns I can ensure 2 and only 2 columns are selected. With Areas, I can differentiate between Areas(1) and Areas(2), unless they're adjacent.

    JosephP, I appreciate the intersect code, but here's where I'm still struggling; using the Intersect configuration, I can verify that the two columns are adjacent, but that leads me back to how do I call each column by a unique name? In other words, the user won't always pick A:B. Sometimes he'll pick U:V, or F:G. Once we get into the macro proper we do different functions with Col1 vs Col2, so using Intersect how do I differentiate between Col1 and Col2 when they're adjacent?
    Please Login or Register  to view this content.

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Discontiguous Column Selection

    this works for contiguous and discontiguous selections
    Please Login or Register  to view this content.
    you get the columns in the order they were selected not necessarily left to right if that matters

  8. #8
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Discontiguous Column Selection

    Thanks so much for the column selection code; I'll use that time and again, I'm sure.

    I've had a rethink on my original code (Original and Updated are both pasted below). My original only worked with A and B, and wrote the results in C, D and E, I wanted to expand the capability to compare ANY two columns, but on reflection I don't necessarily want to change the sheet those two columns come from, so my updated macro copies the two columns to a Comparison sheet and does the comparison there, so I'm back to working with A:E. I think this tweak could be easily put into either or both of the macros I listed at the start of this thread as well if anybody wants to tackle those.

    Thanks for all the help, and I hope someone else enjoys the updated code.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

+ 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] repetitive/ discontiguous autofill
    By bulina2k in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 11-14-2014, 07:15 AM
  2. Copy/Paste Discontiguous columns
    By jomili in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-15-2011, 01:12 PM
  3. [SOLVED] Looping Thru Cells in a Discontiguous Range
    By Larry.LeBlanc@O in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-10-2008, 04:16 PM
  4. Is it possible to plot discontiguous cells?
    By Peter Bernadyne in forum Excel Charting & Pivots
    Replies: 8
    Last Post: 05-11-2006, 04:50 PM
  5. Is it possible to plot discontiguous cells?
    By Peter Bernadyne in forum Excel General
    Replies: 0
    Last Post: 05-09-2006, 05:32 PM
  6. [SOLVED] Copying discontiguous ranges to Word
    By Garry in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-14-2005, 10:50 PM
  7. Code for Printing Discontiguous Ranges
    By GoFigure in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-14-2005, 05:00 PM

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