Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Filter listbox passed comobox value

0

Supposed this code is looking for the last column in the worksheet data of the table (code #) but I want this txtCode is look for the last number in listbox after filtering with the second combox calling (CbSubCategory)

Private Sub UserForm_Activate() 
Me.txtCode.Value = Application.WorksheetFunction.Max(Sheets("Data").Range("AN7:AN20000")) + 1 
Application.Calculation = xlCalculationAutomatic
Answer
Discuss

Discussion

Hi Amin and welcome to the Forum. 

To get an answer to your (somewhat brusque) question, please edit your question and use the Add Files... button to attach a representative Excel file. Without that, it's difficult for us to know what you're trying. 
John_Ru (rep: 6142) Jan 11, '24 at 5:18 pm
Supposed this code is looking for the last column in the worksheet data of the table (code #)  but I want this txtCodeis looking for the last number in listbox after filtering with the second combox calling (CbSubCategory)
 
Private Sub UserForm_Activate()
'Me.txtCode.Value = Application.WorksheetFunction.Max(Sheets("Data").Range("AN7:AN20000")) + 1
Application.Calculation = xlCalculationAutomatic
 
 
End Sub
Amin25 (rep: 16) Jan 13, '24 at 10:24 am
Thanks for adding the file (but repeating your question text does not help).

Please see my Answer and file and be sure to mark the ANswer as Selected if it works as you expect.
John_Ru (rep: 6142) Jan 13, '24 at 11:21 am
Add to Discussion

Answers

0
Selected Answer

Amin

It seems your question was referring to the Userform "frmCodeItem".(please say so next time!).

In the attached revoised file, I've chnaged your UserForm code as as follows:

To get the item number (for the last item in the LB_)1 list, if any) showing, I modified this code, as shown in bold:

Private Sub CbSubCategory_Change()

   << existing code>>
            End If

    ' show the item number for the last item in list (if any)
    If s > 0 Then Me.txtCode.Value = Me.LB_01.List(s - 1, 0)

End Sub

Also, when the Depatment is changed, the current code left theUserform showing selections from the old subcategory (so items NOT in the new Department). I changed this code as follows (in bold):

Private Sub comDepartment_Change()

   << existing code>>
      End If

    ' clear the SubCategory and other boxes to allow new selection
    Me.CbSubCategory = ""
    Me.txtItemName = ""
    Me.txtPrice = ""
    Me.TxtSalePrice = ""
    Me.comUnit = ""
    Me.LB_01.RowSource = ""

End Sub

Hope this fixes your problem. If so, please be sure to mark this Answer as Selected.

Discuss

Discussion

Thank you so much, It's solved the problems 
Amin25 (rep: 16) Jan 13, '24 at 12:04 pm
Glad that worked. Thanks for selecting my Answer, Amin. Please also delete the duplicate Question (from today) I want to code to show last number in first column in listbox
John_Ru (rep: 6142) Jan 13, '24 at 12:14 pm
I need one more thing for this. I want to add items and after added and modified can be shown in listbox direct 
 If s > 0 Then Me.txtCode.Value = Me.LB_01.List(s - 1, 0) +1
Amin25 (rep: 16) Jan 13, '24 at 3:00 pm
Amin

Not sure what you mean. Please ask a new question on that, explaining better and referring to this Question/ Answer plus attaching a file.

Note that I doubt I have any time to respond this weekend (but Willie or Don might, if they have spare time).

Also, thanks for deleting the other, duplicate question 
John_Ru (rep: 6142) Jan 13, '24 at 4:45 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login