Link to home
Start Free TrialLog in
Avatar of John Smith
John SmithFlag for Australia

asked on

How can I user a countif formula in a Userform?

The following formula works in an excel worksheet with on problems. I would like to use something similiar if possible. Can anyone help me?

Private Sub TxtBx2_Change()
    If TxtBx2 = "MD" Then
        Me.TxtBx1.Value = Concatenate(Me.TxtBx2.Text, Right(Concatenate("000", CountIf(ReportList![A3:A200], "=" & TxtBx2.Value) + 1, 3)))
    Else
    If TxtBx2 = "SFC" Then
        Me.TxtBx1.Value = Concatenate(Me.TxtBx2.Text, Right(Concatenate("000", CountIf(ReportList![A3:A200], "=" & TxtBx2.Value) + 1, 3)))
    Else
    If TxtBx2 = "DOM" Then
        Me.TxtBx1.Value = Concatenate(Me.TxtBx2.Text, Right(Concatenate("000", CountIf(ReportList![A3:A200], "=" & TxtBx2.Value) + 1, 3)))
    Else
    End If
End Sub

My wish is to select a check box which then places the appropriate code in textbox2. This will then look for the highest number used for the selected section and adds 1 for help organise the Bin No. in sequence.

Any assistence will be appreciated.

THANKS
Report-Test.xlsm
Avatar of Rory Archibald
Rory Archibald
Flag of United Kingdom of Great Britain and Northern Ireland image

That appears to equate to:

Private Sub TxtBx2_Change()
    Select Case TxtBx2
        case "MD", "SFC", "DOM"
        Me.TxtBx1.Value = Me.TxtBx2.Text & Format(Application.CountIf(Sheets("ReportList").Range("A3:A200"), TxtBx2.Value) + 1, "000")
    End Select
End Sub

Open in new window

Avatar of John Smith

ASKER

Thanks for the quick responce. Works great but also need the number to increase by 1 for the case selections each time a record is saved. eg.
1st MD record should be MD001
2nd MD record becomes MD002 and so on.
1st SFC record should be SFC001
2nd SFC record becomes SFC002 and so on.
I hope this provides a little more clarity. See the "RED" Practice Area for examples.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Rory Archibald
Rory Archibald
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great result many thanks for you assistence.
Keep up this fabulous service.