+ Reply to Thread
Results 1 to 14 of 14

Macro for finding a specific value by comparing two columns

  1. #1
    Registered User
    Join Date
    05-15-2013
    Location
    NZ
    MS-Off Ver
    Excel 2003
    Posts
    8

    Macro for finding a specific value by comparing two columns

    Hi,

    I am new to VBA and thinking if there is a more elegant way to achieve what I am trying to do in the code.

    I am trying to find the value of "Q" ("C18") at which any value in column "D" is greater than the corresponding value in column "B" ?

    Basically can I use "For i = 1 to 5" instead of having to write down each cell address to get the required value of "Q" ? My code is as follows. Any help would be appreciated. Thanx

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Macro for finding a specific value by comparing two columns

    It's not clear for me, attach please a sample file with data and desired result
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    05-15-2013
    Location
    NZ
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Macro for finding a specific value by comparing two columns

    File attached.

    Thanx much for helping me out.
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro for finding a specific value by comparing two columns

    Okay if the value of C5 is greater than the values in column D, so what then?

  5. #5
    Registered User
    Join Date
    05-15-2013
    Location
    NZ
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Macro for finding a specific value by comparing two columns

    No, if the value of any cells of column D is greater than the corresponding value in column B , then that specific value of "C5" is the answer because values in column B are being calculated by using value "C5".

    Thanx

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,517

    Re: Macro for finding a specific value by comparing two columns

    Macro?

    C18:

    =SUMPRODUCT(($D$8:$D$12>$B$8:$B$12)*1)

  7. #7
    Registered User
    Join Date
    05-15-2013
    Location
    NZ
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Macro for finding a specific value by comparing two columns

    That is not what I am doing.

    Again,

    Value in "C5" changes the values in Column "D". Then Column "D" is compared with column "B" and if any value of column "D" is greater than the corresponding value in column "B", then that "C5" value is the answer. If after one iteration, none of the values in column D are greater than the corresponding values in column B then the value of "C5" is increased by "1" that is "C5" + 1 = New "C5" and the values of column D are computed again and then compared with corresponding values in column B and so forth till the result is achieved.

  8. #8
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Macro for finding a specific value by comparing two columns

    Please Login or Register  to view this content.

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,517

    Re: Macro for finding a specific value by comparing two columns

    Now I see what you mean
    Please Login or Register  to view this content.
    Or just

    =SUMPRODUCT(MIN(B8:B12-C8:C12))+1
    Last edited by jindon; 05-16-2013 at 05:51 AM.

  10. #10
    Registered User
    Join Date
    05-15-2013
    Location
    NZ
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Macro for finding a specific value by comparing two columns

    Hi, Thanx for the code. But it is not giving me the answer I want. I am trying to that value of "C5" at which any One value in column "D" is greater than the corresponding value in column "B".

    The correct answer is "52" and at this value of "C5", only one value of column D is greater than the corresponding value of column B.

    Many thanx for your help if you can guide me in the right direction.

  11. #11
    Registered User
    Join Date
    05-15-2013
    Location
    NZ
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Macro for finding a specific value by comparing two columns

    Hi, Thanx for the code. But it is not giving me the answer I want. I am trying to that value of "C5" at which any One value in column "D" is greater than the corresponding value in column "B".

    The correct answer is "52" and at this value of "C5", only one value of column D is greater than the corresponding value of column B.

    Many thanx for your help if you can guide me in the right direction.

  12. #12
    Registered User
    Join Date
    05-15-2013
    Location
    NZ
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Macro for finding a specific value by comparing two columns

    Hi, thanx Patel4, I tweaked your code to get the desired result. Now it works. Many Thanx

    Please Login or Register  to view this content.

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,517

    Re: Macro for finding a specific value by comparing two columns

    Quote Originally Posted by Maxro View Post

    The correct answer is "52" and at this value of "C5", only one value of column D is greater than the corresponding value of column B.
    Both code and the formula are working as they should
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    05-15-2013
    Location
    NZ
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Macro for finding a specific value by comparing two columns

    Thanx for that jindon.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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