Solved

Excel 2010 userform

Posted on 2012-03-14
8
416 Views
Last Modified: 2012-03-24
Any sheet 1-20 on the attached program is an order taking form. To the right of the sheet is a button for "Inventory Adjustment". This userform has a drop down box for "PRODUCT". Right now you have to scroll down to pick the item needed. How would I change the code, so that the item number could be typed in, and the program would help you find the item in the data base? For instance, if  an "A" was typed, the window would show items that start with "A". If the next letter was "AA" the window would show items starting with "AA", and so on. But would not let you type an item was not in the data base.
FG.xls
0
Comment
Question by:CRHIV
  • 5
  • 3
8 Comments
 
LVL 14

Expert Comment

by:Zack Barresse
ID: 37723213
Hi there,

You can use something like this in your userform code module...


Private Sub CboProduct_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Dim wsCheck As Worksheet
    Dim rCheck As Range
    Dim rFilter As Range
    Dim cProd As Range
    Dim sChars As String
    Set wsCheck = ThisWorkbook.Worksheets("INVENTORY")
    Set rCheck = wsCheck.Range("A2", wsCheck.Cells(wsCheck.Rows.Count, 1).End(xlUp))
    Set rFilter = wsCheck.Range("A3", wsCheck.Cells(wsCheck.Rows.Count, 1).End(xlUp))
    sChars = Me.CboProduct.Value
    If sChars = vbNullString Then
        Me.CboProduct.Clear
        For Each cProd In wsCheck.Range("A3", wsCheck.Cells(wsCheck.Rows.Count, 1).End(xlUp))
            With Me.CboProduct
                .AddItem cProd.Value
                .List(.ListCount - 1, 1) = cProd.Offset(0, 3).Value
            End With
            Next cProd
        Exit Sub
    End If
    If KeyCode < 33 Or KeyCode > 126 Then Exit Sub
    rCheck.AutoFilter 1, sChars & "*"
    On Error Resume Next
    Me.CboProduct.Clear
    For Each cProd In rFilter.SpecialCells(xlCellTypeVisible)
        With Me.CboProduct
            .AddItem cProd.Value
            .List(.ListCount - 1, 1) = cProd.Offset(0, 3).Value
        End With
    Next cProd
    wsCheck.AutoFilterMode = False
End Sub


Basically it's using AutoFilter to filter for the characters you entered into the combobox and 1) clearing the combobox, 2) re-populating the combobox with the filtered values.  You should also add this line to a userform close routine (I didn't see one in your file, so just add it to your userform code module)...


Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    ThisWorkbook.Worksheets("INVENTORY").AutoFilterMode = False
End Sub


HTH

Regards,
Zack Barresse
0
 

Author Comment

by:CRHIV
ID: 37724403
Thanks for the code. Can it be altered a bit? When the "Inv Adj" is opened, can the cursor be positioned in the "PRODUCT" field? When a letter is entered, can the drop down box appear with a field of 8 items and the descriptions listed starting with the item of the closest match for the first letter? When you leave the "PRODUCT" field, it needs to pull the other information off of the "INVENTORY" sheet to fill in "BATCH NO", "LOCATION", "CURRENT" count of inventory. The way it is now, if you start typing, there is no look up help. The only way to get look up help is to first open the drop down box. Then even if the item is selected with the mouse, it doesn't populate the other data when you leave the "PRODUCT" field. The only way to get it to populate the other fields is to do it with the original drop down box.
0
 

Author Comment

by:CRHIV
ID: 37724756
Attached is the latest FG.xls with changes you suggested. See if I put them in right.
FG.xls
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:CRHIV
ID: 37724763
Thanks,

Chuck
0
 
LVL 14

Accepted Solution

by:
Zack Barresse earned 500 total points
ID: 37725163
Hi Chuck,

Let me try to answer your questions.

When the "Inv Adj" is opened, can the cursor be positioned in the "PRODUCT" field?
Yes.  In the Userform_Initialize() routine, before the "End Sub", add this line of code...
Me.CboProduct.SetFocus

Open in new window


When a letter is entered, can the drop down box appear with a field of 8 items and the descriptions listed starting with the item of the closest match for the first letter?
AFAIK we don't have this type of functionality or latitude.
When you leave the "PRODUCT" field, it needs to pull the other information off of the "INVENTORY" sheet to fill in "BATCH NO", "LOCATION", "CURRENT" count of inventory. The way it is now, if you start typing, there is no look up help. The only way to get look up help is to first open the drop down box. Then even if the item is selected with the mouse, it doesn't populate the other data when you leave the "PRODUCT" field. The only way to get it to populate the other fields is to do it with the original drop down box.
A workaround for that would be to use a ListBox to show the data and have the textbox instead of a combobox where the user can type the data.

Attached is the latest FG.xls with changes you suggested. See if I put them in right.
The only thing I didn't see in the code was the UserForm_QueryClose event, which was toward the end of my post.  You should really have that as you don't want your data on the INVENTORY sheet to stay filtered if someone closes the form, and that code takes off the autofilter.

HTH

Regards,
Zack Barresse
0
 

Author Closing Comment

by:CRHIV
ID: 37758321
Said what I wanted to do could not be done.
0
 
LVL 14

Expert Comment

by:Zack Barresse
ID: 37759945
Huh.  Not that I mind about the points awarded, but some people here *really* do care.  Awarding an "Average" answer because of the software limitations is, well, naive.  I'm more than happy to tell you things you can't do in Excel all day long, but it certainly shouldn't reflect on the poster.  For future reference, since it can be a big deal to people, I would highly recommend you base your point awards based on merit, not what you deem is appropriate based on application limitations in reference to the poster.  Kind of unfair to the posters.  :)

Have a good one,
Zack
0
 

Author Comment

by:CRHIV
ID: 37760720
Sorry. I felt, with my limited programming knowledge, that it could be done. I didn't know how to go about it. Every seemingly impossible question I had ever asked had been answered to a workable solution. Thanks for your help.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Distribute Values over date range 15 36
Excel Macro 9 15
Strategy Mapping Excel WB/WS 2 24
Excel 2010 Pivot Table Calculated Fields 2 3
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question