+ Reply to Thread
Results 1 to 5 of 5

Need to get values based on multiple conditions through Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    06-10-2023
    Location
    Mumbai India
    MS-Off Ver
    2019
    Posts
    21

    Need to get values based on multiple conditions through Macro

    So far my code is working OK however I dont seem to be getting desired values since all the values are dynamic and change from time to time, hence the need for change, I need to embed this in my code, I have no idea how to acheive it. I need these values only when column D & J is clicked.

    Formula: copy to clipboard
    =IF(H47<=90,G7-(H47*M47)*5%,
    IF(H47<=80,G47-(H47*M47)*6%,
    IF(H47<=70,G47-(H47*M47)*8%,
    IF(H47<=65,G47-(H47*M47)*11%,
    IF(H47<=60,G47-(H47*M47)*14%,
    IF(H47<=55,G47-(H47*M47)*17%,
    IF(H47<=50,G47-(H47*M47)*19%,
    IF(H47<=40,G47-(H47*M47)*22%,
    IF(H47<=30,G47-(H47*M47)*25%,
    ))))))))


    =IF(A47<=90,G47+(F47*A47)*5%,
    IF(A47<=80,G47+(F47*A47)*6%,
    IF(A47<=70,G47+(F47*A47)*8%,
    IF(A47<=65,G47+(F47*A47)*11%,
    IF(A47<=60,G47+(F47*A47)*14%,
    IF(A47<=55,G47+(F47*A47)*17%,
    IF(A47<=50,G47+(F47*A47)*19%,
    IF(A47<=40,G47+(F47*A47)*22%,
    IF(A47<=30,G47-(F47*A47)*25%,
    ))))))))

    Current Code in use which doesnt give desired values:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Column <> 4 And Target.Column <> 10 Then Range("A3,M3").ClearContents
    
    If Intersect(Target, Range("J4:J76,D4:D76")) Is Nothing Or Target.Count > 1 Then Exit Sub
    
    With Target
        Select Case .Column
            Case [J1].Column
                G = .Offset(, -3).Value
                H = .Offset(, -2).Value
                M = .Offset(, 3).Value
                [M3].Value = WorksheetFunction.MRound(G - (H * M) * 0.1, 0.05)
                
            Case [D1].Column
                G = .Offset(, 3).Value
                A = .Offset(, -3).Value
                F = .Offset(, 2).Value
                [A3].Value = WorksheetFunction.MRound(G + (A * F) * 0.1, 0.05)
                            
        End Select
    End With
    Attached Files Attached Files
    Last edited by mahmad558; 06-16-2023 at 10:30 AM.

  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
    48,180

    Re: Need to get values based on multiple conditions through Macro

    This formula won't work for two reasons

    Formula: copy to clipboard

    =IF(H47<=90,G7-(H47*M47)*5%,
    IF(H47<=80,G47-(H47*M47)*6%,
    IF(H47<=70,G47-(H47*M47)*8%,
    IF(H47<=65,G47-(H47*M47)*11%,
    IF(H47<=60,G47-(H47*M47)*14%,
    IF(H47<=55,G47-(H47*M47)*17%,
    IF(H47<=50,G47-(H47*M47)*19%,
    IF(H47<=40,G47-(H47*M47)*22%,
    IF(H47<=30,G47-(H47*M47)*25%,
    ))))))))


    1. The first line of the formula refers to G7, not G47

    2. If a value is less than 30, it will also be less than all the higher test values that precede it. So the formula will return the <=90 result. You need to reverse the order of the tests.

    The second formula also has the same issue (2).
    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
    06-10-2023
    Location
    Mumbai India
    MS-Off Ver
    2019
    Posts
    21

    Re: Need to get values based on multiple conditions through Macro

    The point here is to embed the code and this is for the entire column range G4 : G76 , A4 : A76 , F4 to F76, H4 : H76, M4 : M76
    the <= sign can be changed to >= depedning on the result I get.
    ***Corrections***

    Formula: copy to clipboard
    =IF(F49>=90,G49+(F49*A49)*5%,
    IF(F49>=80,G49+(F49*A49)*6%,
    IF(F49>=70,G49+(F49*A49)*8%,
    IF(F49>=65,G49+(F49*A49)*11%,
    IF(F49>=60,G49+(F49*A49)*14%,
    IF(F49>=55,G49+(F49*A49)*17%,
    IF(F49>=50,G49+(F49*A49)*19%,
    IF(F49>=40,G49+(F49*A49)*22%,
    IF(F49>=30,G49-(F49*A49)*25%,
    )))))))))

    =IF(H47>=90,G47-(H47*M47)*5%,
    IF(H47>=80,G47-(H47*M47)*6%,
    IF(H47<=70,G47-(H47*M47)*8%,
    IF(H47>=65,G47-(H47*M47)*11%,
    IF(H47>=60,G47-(H47*M47)*14%,
    IF(H47>=55,G47-(H47*M47)*17%,
    IF(H47>=50,G47-(H47*M47)*19%,
    IF(H47>=40,G47-(H47*M47)*22%,
    IF(H47>=30,G47-(H47*M47)*25%,
    )))))))))
    Last edited by mahmad558; 06-16-2023 at 08:17 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
    48,180

    Re: Need to get values based on multiple conditions through Macro

    I'm not going to spend any time embedding a formula in code when I know it won't work.


    Fast answers need visual help. Please read the yellow banner at the top of this page on how to attach a file and a mocked up solution.

  5. #5
    Registered User
    Join Date
    06-10-2023
    Location
    Mumbai India
    MS-Off Ver
    2019
    Posts
    21

    Re: Need to get values based on multiple conditions through Macro

    Have uploaded the file for your reference, the idea is that when I click on Column J or Column D, I need this calculation, which you can see in column N & O, this value should display in Cell A3 when column D is clicked and in M3 when Column J is clicked.

    Instead of using <= I have used >=, I have corrected that.

+ 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] Looking up values based on multiple conditions then put sum the values in anther sheet.
    By cuchi33 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 08-05-2017, 11:35 AM
  2. Replies: 1
    Last Post: 10-01-2013, 02:41 AM
  3. Replies: 5
    Last Post: 09-25-2013, 12:31 AM
  4. [SOLVED] Formula to get look up values based on multiple conditions
    By Excel Dumbo in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-29-2013, 12:17 AM
  5. [SOLVED] Trying to add conditioning and values based on multiple conditions.
    By SadenShard in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-27-2011, 12:58 AM
  6. Insert multiple values from seperate table based on multiple conditions
    By drakesong in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 02-28-2011, 06:04 PM
  7. Calculate Values based on Multiple Conditions
    By creativefusion in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 05-24-2009, 09:27 PM

Tags for this Thread

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