+ Reply to Thread
Results 1 to 19 of 19

Pull Vlookup from excel sheet and use the lookup value as userform textbox value

Hybrid View

  1. #1
    Registered User
    Join Date
    11-09-2018
    Location
    Mars
    MS-Off Ver
    Best Version
    Posts
    15

    Pull Vlookup from excel sheet and use the lookup value as userform textbox value

    I have this code

    =IF(XOR(13>=$Q2,$Q2=""),(($Q2-(VLOOKUP($C2,DATA!$D$1:$E$22,2,0)))*100)*0.02,IF($Q2>=14,(((($Q2-(VLOOKUP($C2,DATA!$D$1:$E$22,2,0))))*10)*0.02),""))
    It is looking up a product flavor (from sheet "Data") and matching its blend score (sheet "Data") to determine the necessary adjustment to the equipment. I know I can do this in the userform I just don't know the proper syntax

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,932

    Re: Pull Vlookup from excel sheet and use the lookup value as userform textbox value

    One easy option is to copy the result from the cell that has this formula into the textbox.

    To use it in VBA code you can put the formula inside a call to Evaluate; you will need to double all the quotes.

    A user can type into a textbox. Is the user allowed to change this value?
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    11-09-2018
    Location
    Mars
    MS-Off Ver
    Best Version
    Posts
    15

    Re: Pull Vlookup from excel sheet and use the lookup value as userform textbox value

    Hi 6StringJazzer,


    On the userform there are 22 option buttons that correlate to the product flavors in sheet "Data," column 1. As well there is an input textbox where the user will input the blend score (correlating to sheet "Data," column 2).

    After the user selects the option button and blend score I want it to essentially lookup the textbox and option button and perform the calculation included in the vlookup then display the result in another textbox.

    I had originally set up a search row with
    Private Sub CommandButton3_Click()
    Dim i As Long
    If OptBCan.Value = True Then
     With Worksheets("CAN")
        For i = 2 To 10000
          If .Cells(i, 22).Value = TBoxBlend.Text Then TBoxMic.Value = .Cells(i, 26).Value: Exit Sub
        Next
      End With
      TBoxMic.Value = "N/A"
      End If
    If OptBBot.Value = True Then
     With Worksheets("BOT")
        For i = 2 To 10000
          If .Cells(i, 17).Value = TBoxBlend.Text Then TBoxMic.Value = .Cells(i, 22).Value: Exit Sub
        Next
      End With
      TBoxMic.Value = "N/A"
      End If
    End Sub
    In this scenario it uses the excel vlookup after the user submits the data; however, this will not work because I need the value prior to the user submitting the data.

    My new code will be in this command button; however, what is the syntax to do this?
    This is what it should do:

    =IF(XOR(13>=TextBox.Value,TextBox.Value=""),(($TextBox.Value-(VLOOKUP("Selected Option Button",DATA!$D$1:$E$22,2,0)))*100)*0.02,IF(TextBox.Value>=14,(((($TextBox.Value-(VLOOKUP("Selected Option Button",DATA!$D$1:$E$22,2,0))))*10)*0.02),""))
    How can I have it determine the selected option button
    Last edited by jdpaul88; 11-19-2018 at 02:55 PM.

  4. #4
    Registered User
    Join Date
    11-09-2018
    Location
    Mars
    MS-Off Ver
    Best Version
    Posts
    15

    Re: Pull Vlookup from excel sheet and use the lookup value as userform textbox value

    Bump 11/21/18 at 9:02 AM CST

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,932

    Re: Pull Vlookup from excel sheet and use the lookup value as userform textbox value

    In your example formula, what value do you mean by "Selected Option Button"? The option button is either TRUE or FALSE. I suspect you mean some value associated with it, but I don't know what.

    Your description is much more complicated than what was suggested by your original post so I would suggest attaching your file so I see the whole picture. The paper clip icon does not work for attachments. To attach a file, under the text box where you type your reply click the Go Advanced button. On the next screen scroll down and click on Manage Attachments, which will show a pop-up window to Select and Upload a file. Then close the window.

    There is no built-in way to find out which option button is selected. Do you have just one group of option buttons, or more than one and using GroupName? Here is a sample of finding out which button is selected and using the caption in your formula, which may or may not be what you need.

    There is no practical way for me to test this without your file.
    ' this code goes in your existing sub
    
       Const GroupName = "MyGroup"
       
       Evaluate ("IF(XOR(13>=" & TextBox.Value & "," & TextBox.Value & "="""")," & _
                   "((" & TextBox.Value & "-(VLOOKUP(" & SelectedOptionButton(GroupName) & ",DATA!$D$1:$E$22,2,0)))*100)*0.02," & _
                   "IF(" & TextBox.Value & ">=14," & _
                      "((((" & TextBox.Value & "-(VLOOKUP(" & SelectedOptionButton(GroupName) & ",DATA!$D$1:$E$22,2,0))))*10)*0.02)," & _
                      """""" & _
                      ")" _
                )
    ' end of code for existing sub
    
    ' this code is added in your module
    
    Private Function SelectedOptionButton(GroupName As String) As String
    
    
       
       Dim c As Control
       
       For Each c In Controls
          If TypeName(c) = "OptionButton" Then
             If c.GroupName = GroupName Then
                If c.Value Then
                   SelectedButton = c.Caption
                   Exit Function
                End If
             End If
          End If
          
       
       Next c
    
    End Function
    Your formula refers to $TextBox.Value but $ is invalid for a textbox so I assume you left it in by mistake.

  6. #6
    Registered User
    Join Date
    11-09-2018
    Location
    Mars
    MS-Off Ver
    Best Version
    Posts
    15

    Re: Pull Vlookup from excel sheet and use the lookup value as userform textbox value

    I have declared/grouped all pertinent 22 option buttons in userform_initialize() as

    OptB1.Groupname = "Product"
    OptB2.Groupname = "Product"
    ...
    OptB22.Groupname = "Product"

    I added the module code and it looks like this now

       Dim c As Control
       
       For Each c In Controls
          If TypeName(c) = "OptionButton" Then
             If c.GroupName = Product Then
                If c.Value Then
                   SelectedOptionButton = c.Caption
                   Exit Function
                End If
             End If
          End If
          
       
       Next c
    I placed the evaluate code in the textbox_change() where the calculated value should be displayed

    the code is:

    Private Sub TBoxMic_Change()
     Const GroupName = Product
    
       Evaluate ("IF(XOR(13>=" & TBoxBlend.Value & "," & TBoxBlend.Value & "="""")," & "((" & TBoxBlend.Value & "-(VLOOKUP(" & SelectedOptionButton(Product) & ",DATA!$D$1:$E$22,2,0)))*100)*0.02," & "IF(" & TBoxBlend.Product & ">=14," & "((((" & TBoxBlend.Value & "-(VLOOKUP(" & SelectedOptionButton(Product) & ",DATA!$D$1:$E$22,2,0))))*10)*0.02)," & """""" & ")")
       
    End Sub

    The only issue is that it is not displaying the value in TBoxMic; I do have a command button I could place this code in but it seems it is missing some code to add the resulting value to TBoxMic


    Note: In the module code I added the word "Option" to change the code
        Dim c As Control
       
       For Each c In Controls
          If TypeName(c) = "OptionButton" Then
             If c.GroupName = GroupName Then
                If c.Value Then
                   SelectedButton = c.Caption
                   Exit Function
                End If
             End If
          End If
          
       
       Next c
    to

       Dim c As Control
       
       For Each c In Controls
          If TypeName(c) = "OptionButton" Then
             If c.GroupName = Product Then
                If c.Value Then
                   SelectedOptionButton = c.Caption
                   Exit Function
                End If
             End If
          End If
          
       
       Next c
    I currently have the evaluate code in the SelectedOptionButton_Click()

    and everything is compiling without a sub undefined error; however, as mentioned, I need the piece to place the value

    Something like TBoxMic = Evaluate.Value??

    Best Regards,

    Jon
    Last edited by jdpaul88; 11-21-2018 at 03:37 PM.

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,932

    Re: Pull Vlookup from excel sheet and use the lookup value as userform textbox value

    I need to spend some time to digest this, but Product needs to be in quotes in this line of your code.

             If c.GroupName = Product Then
    I strongly recommend to everyone that they declare variables. Doing so prevents a lot of bugs and runtime errors. If Option Explicit were used in this code, it would have flagged Product as an undeclared variable.

    It would really make this faster if I had your file.

  8. #8
    Registered User
    Join Date
    11-09-2018
    Location
    Mars
    MS-Off Ver
    Best Version
    Posts
    15

    Re: Pull Vlookup from excel sheet and use the lookup value as userform textbox value

    Pete,


    See attached
    Attached Files Attached Files

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,932

    Re: Pull Vlookup from excel sheet and use the lookup value as userform textbox value

    Here is your file with the code corrected to work. However, I don't know if the final answer being calculated by the formula is the one you want. You will have to test that. Hopefully the code will be clear now that it's all complete and in one place, but let me know if you have questions.

    You only have one set of option buttons so you don't need to use the GroupName field. It can be left blank.

    I corrected an error in the code that builds the formula; it was missing a parenthesis at the end.
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    11-09-2018
    Location
    Mars
    MS-Off Ver
    Best Version
    Posts
    15

    Re: Pull Vlookup from excel sheet and use the lookup value as userform textbox value

    Hi Jeff,

    Awesome work. I will test the code for functionality; following confirmation testing I will mark this thread as solved.

    Regards,

    Jon

  11. #11
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,932

    Re: Pull Vlookup from excel sheet and use the lookup value as userform textbox value

    It occurred to me that it might be better for your user interface to use a combobox to select the number, rather than a big collection of option buttons. If you are interested in that alternative let me know.

  12. #12
    Registered User
    Join Date
    11-09-2018
    Location
    Mars
    MS-Off Ver
    Best Version
    Posts
    15

    Re: Pull Vlookup from excel sheet and use the lookup value as userform textbox value

    Jeff,

    Is this suggestion of a combobox more for aesthetics or for a reduction in code?

  13. #13
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,932

    Re: Pull Vlookup from excel sheet and use the lookup value as userform textbox value

    It probably wouldn't reduce the code (which shouldn't be a goal in and of itself) but might be a nice interface for the user and a smaller footprint for the form. It also means that if you add more options, you don't have to change the form (i.e., add more option buttons). I'm not trying to convince you to do it, just throwing it out and as an option.

  14. #14
    Registered User
    Join Date
    11-09-2018
    Location
    Mars
    MS-Off Ver
    Best Version
    Posts
    15

    Re: Pull Vlookup from excel sheet and use the lookup value as userform textbox value

    Jeff,

    How would the code change if I have declared a set of option buttons with a groupname?

  15. #15
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,932

    Re: Pull Vlookup from excel sheet and use the lookup value as userform textbox value

    You would change this one line

       Const GroupName = ""
    to whatever your groupname is

       Const GroupName = "mygroupname"
    If you only have one group of buttons, you don't need a groupname. If you have more than one group then it's needed.

  16. #16
    Registered User
    Join Date
    11-09-2018
    Location
    Mars
    MS-Off Ver
    Best Version
    Posts
    15

    Re: Pull Vlookup from excel sheet and use the lookup value as userform textbox value

    Thanks Jeff,

    What needs to change when the option button caption changes from an integer to a word; I played around with your code and replaced the caption value of option button "1" to "New"

    When I run the code I get run-time error

    Could not set the value property, type mismatch

    Do we need to declare the caption button to include letters?

  17. #17
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,932

    Re: Pull Vlookup from excel sheet and use the lookup value as userform textbox value

    When you use Evaluate, the VLOOKUP is expecting a number. To use strings, you must put the value in value in quotes. I have completely overhauled the code to solve this problem. The solution involves coding the logic in VBA instead of using Evaluate on a worksheet formula, which I probably should have done in the first place.

    Option Explicit
    
    Private Sub CommandButton3_Click()
       
       Const GroupName = ""
       
       Dim OBcaption As String
       Dim Expression As String
       
       OBcaption = SelectedOptionButton(GroupName)
       
       If TBoxBlend = "" Then
          MsgBox "Set a test value first"
          
       ElseIf OBcaption = "" Then
          MsgBox "Select an option button first"
          
       Else
    
          If (13 >= TBoxBlend.Value Or TBoxBlend.Value = "") Then
             TBoxMic = ((5 - Application.WorksheetFunction.VLookup(OBcaption, Worksheets("DATA").Range("$D$1:$E$22"), 2, False)) * 100) * 0.02
          ElseIf TBoxBlend.Value >= 14 Then
             TBoxMic = ((5 - Application.WorksheetFunction.VLookup(OBcaption, Worksheets("DATA").Range("$D$1:$E$22"), 2, False)) * 10) * 0.02
          Else
             TBoxMic = ""
          End If
       End If
       
    End Sub
            
    Private Function SelectedOptionButton(GroupName As String) As String
       
       Dim c As Control
       
       For Each c In Controls
          If TypeName(c) = "OptionButton" Then
             If c.GroupName = GroupName Then
                If c.Value Then
                   SelectedOptionButton = c.Caption
                   Exit Function
                End If
             End If
          End If
       
       Next c
    
    End Function

  18. #18
    Registered User
    Join Date
    11-09-2018
    Location
    Mars
    MS-Off Ver
    Best Version
    Posts
    15

    Re: Pull Vlookup from excel sheet and use the lookup value as userform textbox value

    Works great! Awesome help Jeff

    Thanks.

    One other thing. How do I send a picture to back in the userform; I want the option buttons to overlay on an image. The image still blocks the viewing of the option buttons when I perform the send to back operation

  19. #19
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,932

    Re: Pull Vlookup from excel sheet and use the lookup value as userform textbox value

    I don't know how to force controls on top of the picture. If you want to use the picture as your form background, then you can go to the Picture property of the form, rather than using a Picture control. Then everything is on top of the image.

+ 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. copy text from userform textbox to sheet textbox
    By Dfrost in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-28-2015, 02:19 AM
  2. Userform textbox to excel sheet
    By wonderd in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-16-2014, 03:13 AM
  3. [SOLVED] Vlookup through 3 sheets from one lookup value result in all textbox in userform vba
    By breadwinner in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-11-2014, 05:17 AM
  4. How to pull Data from Excel Sheet and show it in UserForm?
    By z-eighty2 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-26-2014, 01:13 PM
  5. [SOLVED] EXCEL USERFORM- pull data from sheet 1 into userform
    By PANTECH in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-18-2013, 11:10 AM
  6. [SOLVED] VBA UserForm - Have entry in TextBox fill Another TextBox by VLOOKUP
    By msquared99 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-26-2012, 10:24 PM
  7. Use textbox input and VLookup to update another textbox on same userform
    By gcoug in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-22-2011, 10:39 AM

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