Consulting

Results 1 to 13 of 13

Thread: If Statement

  1. #1
    VBAX Regular
    Joined
    Jun 2005
    Posts
    87
    Location

    If Statement

    I can't figure out an IF statement.
    I want one cell to show more than one value result based on other cells.

    IF H1>0, Then GS0740101 AND IF I1>0 Then GS0741102 AND IF J1>0 then GS0741203
    So if all three cells are greater than 0, it will show all three GS numbers (would need a comma between them all).

    Is this possible?

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    For all three values, try
    =IF(AND(H1>0,I1>0,J1>0),H1 & ", " & I1 & ", " & J1)
    Do you need a result for 1 and 2 cells as well?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Regular
    Joined
    Jun 2005
    Posts
    87
    Location
    This is giving a FALSE result - is the "AND" making it look to see if ALL have to be greater than 0 in order to be TRUE? I want it if even only 1 is greater than 0, then it will show the value. If H1, H2 and H4 are greater than 0 but H3 is not, it will show H1, H2, H4.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by spaz9876
    I can't figure out an IF statement.
    I want one cell to show more than one value result based on other cells.

    IF H1>0, Then GS0740101 AND IF I1>0 Then GS0741102 AND IF J1>0 then GS0741203
    So if all three cells are greater than 0, it will show all three GS numbers (would need a comma between them all).

    Is this possible?
    =SUBSTITUTE(IF(H1>0,"GS0740101",",")&","&IF(I1>0,"GS0741102",",")&","&IF(J1>0,"GS0741023",","),",,","")
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    This can also be done with a User Defined Function (UDF), which can be more flexible as well.
    Option Explicit 
    Function DoList(MyCells As Range)
    Dim Cel
    Dim Tmp As String
    'Check each cell in selection
    For Each Cel In MyCells
    'Set the comparison
    If Cel >= 1 Then
    'Create string
    Tmp = Tmp & Cel & ", "
    End If
    Next
    'Remove end comma and space
    DoList = Left(Tmp, Len(Tmp) - 2)
    End Function
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Regular
    Joined
    Jun 2005
    Posts
    87
    Location
    The Substitute one isnt working.
    The VBA Code works however I want if H1 is greater than zero, value is I1 instead of it taking the value of H1.

    And thank you for helping me!

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you post a sample of your layout?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    VBAX Regular
    Joined
    Jun 2005
    Posts
    87
    Location
    Here it is. Thanks!

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Revised to return offset value

    Option Explicit
    
    Function DoList(MyCells As Range)
        Dim Cel
        Dim Tmp As String
         'Check each cell in selection
        For Each Cel In MyCells
             'Set the comparison
            If Cel >= 1 Then
                 'Create string
                Tmp = Tmp & Cel.Offset(0, 1) & ", "
            End If
        Next
         'Remove end comma and space
        DoList = Left(Tmp, Len(Tmp) - 2)
    End Function
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  10. #10
    VBAX Regular
    Joined
    Jun 2005
    Posts
    87
    Location
    Ok that works - only thing is it puts commas in funny. Looks like this:
    GS-07F-0101M, , , , GS-07F-8789D, , , , , GS-07F-7820C, , ,

  11. #11
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I'm not getting that problem.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  12. #12
    VBAX Regular
    Joined
    Jun 2005
    Posts
    87
    Location
    Sorry - I had 2 columns selected I changed it to =dolist(H8:H18) instead of (H8:I18) and now it works. THanks! What do I owe you?

  13. #13
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Just help another when you can!
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •