+ Reply to Thread
Results 1 to 13 of 13

Compare which column has first non-blank cell

  1. #1
    Registered User
    Join Date
    09-19-2013
    Location
    NY, NY
    MS-Off Ver
    Excel 2010
    Posts
    31

    Question Compare which column has first non-blank cell

    Hello everyone,

    I have a spreadsheet that has two columns populated with formulas in each cell starting at H3 and I3, which compare different data each, and only display an error code.
    I need a macro which finds the first cell with an error code (non-blank) in each column, compares the row number of the two cells, and goes to the cell with the lowest row number in whichever column it appears (H or I). If the first non-blank in each column happens on the same row, it should go the one in H.

    At the risk of overstating, here's an example:
    If first non-blank cell in H is at H23, and if first non-blank cell in I is at I17. Compare 23 to 17 and go to I17.
    If they both happen at, say, H34 and I34, go to H34.

    So basically:
    find first non-blank in H
    find first non-blank in I
    if row # in H <= row # in I
    goto H#
    else
    goto I#

    This looks so simple, but...

    Cheers,
    Alphonse

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Compare which column has first non-blank cell

    Suggest you post a sample workbook to test with.

    (Though I may not be able to access it on my laptop)

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    09-19-2013
    Location
    NY, NY
    MS-Off Ver
    Excel 2010
    Posts
    31

    Question Re: Compare which column has first non-blank cell

    Hello TM,
    Thanks for answering.
    Here is a simple test file. It is not complete, but it serves the purpose.
    Cheers.
    Attached Files Attached Files
    Last edited by Alphonse68; 09-26-2013 at 11:26 AM.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Compare which column has first non-blank cell

    Try this:

    Please Login or Register  to view this content.

    Regards, TMS
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    09-19-2013
    Location
    NY, NY
    MS-Off Ver
    Excel 2010
    Posts
    31

    Re: Compare which column has first non-blank cell

    WOW! Works like a charm!! Thank you so very much.

    There is some code in it that I would have never thought of, even if I knew they existed, short of divine intervention, like the definition of the awf, Transpose, and UBound. Thank you, also, for the explanations; they sure helped in understanding most of how it works.

    Most of all, thank you for your time in writing the routine. May good things always come your way.

    Cheers.
    Alphonse

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Compare which column has first non-blank cell

    You're welcome. Thanks for the rep.


    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  7. #7
    Registered User
    Join Date
    09-19-2013
    Location
    NY, NY
    MS-Off Ver
    Excel 2010
    Posts
    31

    Re: Compare which column has first non-blank cell

    Hi TMS,
    I know we already closed this post, but I forgot to ask, is the line Option Explicit need to go with the code when I add it to the rest of my macros? I ask because I want to make it part of a set of routines bunched together to do about 5 things at once at the push of a button.
    Also, what does it mean or do?

    Cheers,
    Alphonse

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

    Re: Compare which column has first non-blank cell

    By Mr. Tom Urtis


    Before you go without the Option Explicit statement, take a moment to carefully consider that decision.

    The term "Option Explicit" means that the VBA code author must define the names of all the variables being used or referred to in the code. Not doing so is called "implicit variable declaration", where you can name a variable without explicitly defining it.

    Why should you explicitly declare variables?

    Option Explicit at the top of a module forces every variable to be declared. There are several reasons why this is a good idea to maintain:

    - Spelling errors are identified.
    - Your variables can be properly declared by you, not Excel, to have the appropriate memory and system resources assigned to accommodate them. Excel will assign Variant as the variable default type for undeclared variables, the most inefficient use of resources.
    - Confusion is avoided if a variable is the same name as a property or method.

    Consider the following code:

    Please Login or Register  to view this content.
    Without Option Explicit to guard against spelling errors, cell A1 will never have a value returned into it because of the spelling inconsistency between FindData (line 2) and FineData (line 3). The macro will always exit because FineData will always be thought of as empty.

    This kind of mistake might be noticeable with a macro this size, but if it is part of a larger procedure these mistakes are hard to identify. Why take the chance?

    Keeping Option Explicit as the default statement is highly recommended; it forces better code writing practice, and helps avoid coding errors.

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Compare which column has first non-blank cell

    @jolivanes: and your point is?

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

    Re: Compare which column has first non-blank cell

    @TMShucks.
    In reply to the question: "Also, what does it mean or do?"

  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,411

    Re: Compare which column has first non-blank cell

    @jolivanes: ah, my apologies. I completely missed post #7 where that question was asked.

    So, a very thorough and helpful explanation ... thank you ... I was just bemused as to what had prompted your response (and I certainly wouldn't have had anything so detailed to hand).

    Thanks, TMS

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

    Re: Compare which column has first non-blank cell

    No apologies needed TMShucks.
    Have a great weekend.

    John

  13. #13
    Registered User
    Join Date
    09-19-2013
    Location
    NY, NY
    MS-Off Ver
    Excel 2010
    Posts
    31

    Re: Compare which column has first non-blank cell

    Hello TMS and John,
    Thanks for the info. I learned a lot from this post. I gathered that the Option Explicit line is good for writing code, but it does not need to included once the coding is completed. I have a series of tasks that I grouped into one Sub, I copied the one from this post in the middle of it without the OE line, and it works just fine. Thanks again to both of you. Take care.
    Cheers,
    Alphonse.

+ 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. Replies: 0
    Last Post: 03-05-2013, 09:22 AM
  2. [SOLVED] Compare 4 column values then insert a 5th columns value to a blank column/cell value
    By JasonKMcCoy in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 10-27-2012, 06:01 PM
  3. Compare two columns and delete cell in one column if cell next to it is blank
    By jacobhandson in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 12-17-2011, 01:51 PM
  4. Find first blank cell in column if rightmost cell is non blank
    By Mothman in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-19-2010, 02:22 AM
  5. Find Blank Cell and Compare...
    By deaerator in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-30-2009, 06:58 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