Consulting

Results 1 to 13 of 13

Thread: Customize a Formula Based on User Input

  1. #1
    VBAX Regular
    Joined
    Jun 2004
    Posts
    16
    Location

    Customize a Formula Based on User Input

    I am new to VB ( so be nice <G> )and I am hoping someone can help solve a problem I can?t figure out.
    What I am trying to do is to customize a formula based on input from a user. Can anyone suggest how I can take the input that I have get from the user and plug it into and write a formula into a cell. It is important that the formula be written to a cell rather than doing the If statement with vb.

    Here is the formula. #REF will equal the input that was given by the user.


    PHP Code:
    =IF(#REF!G2>79,"Discharge",IF(#REF!G2>71,"Final",IF(#REF!G2>63, "Second", IF(#REF!G2>55,"First",IF(#REF!G2>31," Informational",""))))) 

    Here is a test script that I am using.

    Sub TEST()
    MyInput = InputBox("Enter Sheet Name")
    Sheets("Home").Select
    Range("k1").Select
    ActiveCell.FormulaR1C1 = _
    "=IF(" & MyInput & "!G2>79,""Discharge"", _
    IF(" & MyInput & "!G2>71,""Final""," & "IF(" & MyInput & "!G2>63, _
    ""Second"", IF(" & MyInput & "!G2>55,""First""," & _
    "IF(" & MyInput & "!G2>31,""Informational"","""")))))"
    End Sub




    Here is the result that is being placed in the cell. The text is being placed in the cell but there are quotes around the cell references [ ?G2? ] and this is causing the formula to croak.



    PHP Code:
    =IF(arggg!'G2'>79,"Discharge",IF(arggg!'G2'>71,"Final",IF(arggg!'G2'>63,"Second",IF(arggg!'G2'>55,"First",IF(arggg!'G2'>31,"Informational",""))))) 


    Any help would be greatly appreciated. I have already spent several hours trying to figure things (reading mostly) out for this project and since it is being done on my own time I would like to spend as little time as needed.
    Last edited by Anne Troy; 06-17-2004 at 12:39 PM.

  2. #2
    BoardCoder
    Licensed Coder
    VBAX Expert mark007's Avatar
    Joined
    May 2004
    Location
    Leeds, UK
    Posts
    622
    Location
    Try:


    x= InputBox("Enter Sheet Name")
    Sheets("Home").Range("k1").Formula "=IF(" & x & "!G2>79,""Discharge"",IF(" & x & "!G2>71,""Final"",IF(" & x & "!G2>63,""Second"",IF(" & x & "!G2>55,""First"",IF(" & x & "!G2>31,"" Informational"","""")))))"
    Last edited by mark007; 06-16-2004 at 07:43 AM. Reason: Pressed tab and submitted b4 I wanted too :rolleyes:
    "Computers are useless. They can only give you answers." - Pablo Picasso
    Mark Rowlinson FIA | The Code Net

  3. #3
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    Hi bturner2,
    since it looks like you aren't using the R1C1-Syntax anyway (at least as far as I can tell), why don't you just assign it using .Formula instead?

    I just tried your code with just removing the "R1C1", and it worked (Excel97)

  4. #4
    VBAX Regular NateO's Avatar
    Joined
    Jun 2004
    Location
    Minneapolis, MN
    Posts
    90
    Location

    Nested Ifs

    Hello, avoid using 5 nested ifs here, the following uses a binary search algorithm and will blow the doors of the nested if logic in terms of performance.


    Sub TEST()
     Dim myInput As Variant
     Let myInput = Application.InputBox("Enter Sheet Name")
     If Not myInput = False Then _
        Sheets("Home").Range("k1").Formula = _
        "=VLOOKUP(" & myInput & "!g2,{-10000,"""";32,""Informational"";56,""First"";64,""Second"";72,""Final"";80,""Discharge""},2,1)"
     End Sub
    Bon chance.
    Regards,
    Nate Oliver

  5. #5
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    As an alternative...

    Sub testing()
    Dim sht As String, MyInput As String
    sht = InputBox("Enter Sheet Name")
    MyInput = Sheets(sht).Range("G2")
    Sheets("Home").Select
    Range("K1").Select
    Select Case MyInput
        Case Is > 79
            Selection.Value = "Discharge"
        Case Is > 71
            Selection.Value = "Final"
        Case Is > 63
            Selection.Value = "Second"
        Case Is > 55
           Selection.Value = "First"
        Case Is > 31
            Selection.Value = "Informational"
        Case Else
                Selection.Value = ""
        End Select
    End Sub

  6. #6
    VBAX Regular NateO's Avatar
    Joined
    Jun 2004
    Location
    Minneapolis, MN
    Posts
    90
    Location
    Also, you can pass a value with Vlookup():

      Sub tester()
      Dim myInput As Variant
      Let myInput = Application.InputBox("Enter Sheet Name")
      On Error Resume Next
      If Not myInput = False Then _
      	Sheets("Home").Range("k1").Value = _
      	WorksheetFunction.VLookup(Sheets(myInput).Range("g2").Value, _
      	[{-10000,"";32,"Informational";56,"First";64,"Second";72,"Final";80,"Discharge"}], 2, 1)
      If CBool(Err.Number) Then MsgBox "Bad Sheet Name"
      End Sub
    Regards,
    Nate Oliver

  7. #7
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by NateO
    Also, you can pass a value with Vlookup():...

    If they've got everything in a table to lookup (why I went with Case Select ). Nice job though! That's excellent!

    edit: scratch that table bit. Just waking up and re-read your post Nate. oops!

  8. #8
    VBAX Regular NateO's Avatar
    Joined
    Jun 2004
    Location
    Minneapolis, MN
    Posts
    90
    Location
    Hello Zack,

    Right, the 2nd argument is the array to lookup ('table').

    Thanks and have a nice day.

    Incidentally,
    with the Select Case approach, which there's absolutely nothing wrong with, avoid selecting the range, unless there's a presentation aspect/reason to do so. Perhaps there is...
    Regards,
    Nate Oliver

  9. #9
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Please see my edit ( ). I realized that after I posted, ugh. Slow morning. Very nice though! I'm trying to work on my Case Select skills anyway - as they're near non-existent at the moment..

    Sorry 'bout the confusion.

    And as for the selection, I almost put that as a variable and use that in the code instead of *hard* selecting it, but I was retaining some of the original code posted. Kind of a mix I guess.

  10. #10
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    bturner2,

    Did any of these solutions work for you? If so, tell us which one and we'll mark this thread as solved. If not, let us know!

  11. #11
    VBAX Regular
    Joined
    Jun 2004
    Posts
    16
    Location
    WOW! You guys are fast. I have had a similar message posted on several other msg boards with no help. Low and behold I come back her and I have all sorts of help! Guess where I will be coming to from now on.



    Yes I have got it working now. The answer Steiner posted was the one that would best suite my needs. I did learn allot from the answers and will look at how they are suggesting I try to do the same thing. The main reason I am not using some of the other answers is because this formula is doing nothing more than giving a quick visual aid to a manager concerning sick leave. With approx 200 employees this is the easiest way I have found to do the limited function or a visual aid. I guess I should have explained in a little better detail of what I was doing and why.



    Thanks for all the help. I am sure you will be seeing me post again soon J

  12. #12
    BoardCoder
    Licensed Coder VBAX Expert mark007's Avatar
    Joined
    May 2004
    Location
    Leeds, UK
    Posts
    622
    Location
    Glad you like the forum!

    "Computers are useless. They can only give you answers." - Pablo Picasso
    Mark Rowlinson FIA | The Code Net

  13. #13
    VBAX Regular
    Joined
    May 2004
    Location
    Springfield, MO
    Posts
    39
    You could also use the INDIRECT function to avoid using VBA altogether. To do this, you will need to reference a cell on the worksheet that contains the user's choice for worksheet name. Let's suppose this is cell F2. NateO's VLOOKUP formula can then be placed directly in the cell:
    =VLOOKUP(INDIRECT(F2 & "!G2"),{-10000,"";31,"Informational";55,"First";63,"Second";71,"Final";79,"Discharge"},2)

Posting Permissions

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