Advice for userform code

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,699
Office Version
  1. 2007
Platform
  1. Windows
Morning,

I have a userform which opens when i first come to the worksheet.
I then enter all the data in each TextBox & when i press a button the data is then entered into the worksheet.

Once the item has been delivered to the customer i then like to enter the date of its delivery.
At present i come to the worksheet,the userform opens,i then close the userform,look for the customer in the spreadsheet & enter the date in the field for that customer.

Long winded i agree.
So i have now put on the userform a CommandButton3

So this is how i think it should work but need some help with the code please.

All customers names are in column B
The delivered date will always be entered in column G

Once i click the CommandButton3 i should see a list of all the customers name from column B.
I select the customer in question, say TOM JONES row 123
I then enter a date like 19/02/2019
This date should then be entered into the worksheet & in the cell G123 alongside the customer TOM JONES.

Many thanks.

This is the current userform code.

Code:
Private Sub CommandButton1_Click()Cancel = 0
If TextBox2.Text = "" Then
    Cancel = 1
    MsgBox "Customer`s Name Not Entered", vbCritical, "POSTAGE TRANSFER SHEET"
    TextBox2.SetFocus
ElseIf TextBox3.Text = "" Then
    Cancel = 1
    MsgBox "Item Description Not Entered", vbCritical, "POSTAGE TRANSFER SHEET"
    TextBox3.SetFocus
ElseIf TextBox4.Text = "" Then
    Cancel = 1
    MsgBox "Tracking Number Not Entered", vbCritical, "POSTAGE TRANSFER SHEET"
    TextBox4.SetFocus
ElseIf ComboBox1.Text = "" Then
    Cancel = 1
    MsgBox "Username Not Entered", vbCritical, "POSTAGE TRANSFER SHEET"
    TextBox5.SetFocus
    
ElseIf OptionButton1.Value = False And OptionButton2.Value = False And OptionButton3.Value = False Then
    Cancel = 1
    MsgBox "You Must Select An Ebay Account", vbCritical, "POSTAGE TRANSFER SHEET"
    
ElseIf OptionButton4.Value = False And OptionButton5.Value = False And OptionButton6.Value = False Then
    Cancel = 1
    MsgBox "You Must Select An Origin", vbCritical, "POSTAGE TRANSFER SHEET"
    
End If


If Cancel = 1 Then
        Exit Sub
End If


Dim i As Long
Dim x As Long
Dim ctrl As Control
Dim LastRow As Long
LastRow = ThisWorkbook.Worksheets("POSTAGE").Cells(Rows.Count, 1).End(xlUp).Row
    


    
 With ThisWorkbook.Worksheets("POSTAGE")
    .Cells(LastRow + 1, 1).Value = TextBox1.Text: TextBox1.Value = ""
    .Cells(LastRow + 1, 2).Value = TextBox2.Text: TextBox2.Value = ""
    .Cells(LastRow + 1, 3).Value = TextBox3.Text: TextBox3.Value = ""
    .Cells(LastRow + 1, 5).Value = TextBox4.Text: TextBox4.Value = ""
    .Cells(LastRow + 1, 9).Value = ComboBox1.Text: ComboBox1.Value = ""
    .Cells(LastRow + 1, 4).Value = TextBox6.Text: TextBox6.Value = ""
    If OptionButton1.Value = True Then .Cells(LastRow + 1, 8).Value = "DR": OptionButton1.Value = False
    If OptionButton2.Value = True Then .Cells(LastRow + 1, 8).Value = "IVY": OptionButton2.Value = False
    If OptionButton3.Value = True Then .Cells(LastRow + 1, 8).Value = "N/A": OptionButton3.Value = False
    If OptionButton4.Value = True Then .Cells(LastRow + 1, 6).Value = "EBAY": OptionButton4.Value = False
    If OptionButton5.Value = True Then .Cells(LastRow + 1, 6).Value = "WEB SITE": OptionButton5.Value = False
    If OptionButton6.Value = True Then .Cells(LastRow + 1, 6).Value = "N/A": OptionButton6.Value = False
End With
TextBox1.Value = Format(CDbl(Date), "dd/mm/yyyy")
TextBox2.SetFocus
TextBox1.Value = Now
TextBox1.Value = Format(CDbl(Date), "dd/mm/yyyy")
End Sub
Private Sub CommandButton2_Click()
Unload PostageTransferSheet


End Sub
Private Sub DHL_Click()
ActiveWorkbook.FollowHyperlink Address:="http://www.dhl.co.uk/en/express/tracking.html", NewWindow:=True


End Sub
Private Sub HERMES_Click()
ActiveWorkbook.FollowHyperlink Address:="https://www.myhermes.co.uk/tracking-results.html", NewWindow:=True


End Sub
Private Sub LABEL_Click()
TrackingLabel.Show


End Sub
Private Sub ROYALMAIL_Click()
ActiveWorkbook.FollowHyperlink Address:="https://www.royalmail.com/track-your-item", NewWindow:=True


End Sub


Private Sub TextBox1_Change()
    TextBox1 = UCase(TextBox1)
End Sub
Private Sub TextBox2_Change()
    TextBox2 = UCase(TextBox2)
End Sub
Private Sub TextBox3_Change()
    TextBox3 = UCase(TextBox3)
End Sub
Private Sub TextBox4_Change()
    TextBox4 = UCase(TextBox4)
End Sub
Private Sub ComboBox1_Change()
    ComboBox1 = UCase(ComboBox1)
End Sub
Private Sub TextBox6_Change()
    TextBox6 = UCase(TextBox6)
End Sub
Private Sub CustomerSearchBox_Change()
'Modified  10/3/2018  5:51:42 AM  EDT
Dim SearchString As String
Dim SearchRange As Range
SearchString = CustomerSearchBox.Value
Dim LastRow As Long
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
Set SearchRange = Range("B2:B" & LastRow).Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
If SearchRange Is Nothing Then MsgBox SearchString & "  Not Found": Exit Sub
SearchRange.Select
Unload Me
End Sub
Private Sub UserForm_Initialize()
'Modified  10/3/2018  5:51:42 AM  EDT
Application.ScreenUpdating = False
Dim LastRow As Long
LastRow = Sheets("POSTAGE").Cells(Rows.Count, "B").End(xlUp).Row
Dim Lastrowa As Long
Sheets("POSTAGE").Cells(8, 2).Resize(LastRow - 7).Copy Sheets("POSTAGE").Cells(1, 12)
Lastrowa = Sheets("POSTAGE").Cells(Rows.Count, "L").End(xlUp).Row
Sheets("POSTAGE").Cells(1, 12).Resize(Lastrowa).Sort key1:=Cells(1, 12).Resize(Lastrowa), order1:=xlAscending, Header:=xlNo
CustomerSearchBox.List = Sheets("POSTAGE").Cells(1, 12).Resize(Lastrowa).Value
Sheets("POSTAGE").Cells(1, 12).Resize(Lastrowa).Clear
Application.ScreenUpdating = True


'USERNAME COMBOBOX




TextBox1.Value = Format(CDbl(Date), "dd/mm/yyyy")
TextBox2.SetFocus
End Sub
Private Sub TextBox2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    Dim fndRng As Range, findString As String, i As Integer


If Me.TextBox2.Value = "" Then Exit Sub
findString = Me.TextBox2.Value


With Sheets("POSTAGE").Range("B:B")
    Set fndRng = .Find(What:=findString, LookIn:=xlValues, lookat:=xlWhole, _
                   SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
                   
    If Not fndRng Is Nothing Then
        'what was entered already exists - alter the name until not found
        For i = 2 To 20
            findString = Me.TextBox2.Value & " " & i
            Set fndRng = .Find(What:=findString, LookIn:=xlValues, lookat:=xlWhole, _
                       SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
            If fndRng Is Nothing Then Exit For
        Next i
        
        'message saying what name should be
        'MsgBox "The name to use is " & findString
        
        'enter that name into textbox 2
        With Me.TextBox2
            .Value = findString
            .SelStart = 0
            .SelLength = Len(.Text)
            .SetFocus
        End With
        
        'cancel moving out of text box
        Cancel = True
    End If
End With
    
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Re: Advice for userform code please

I suggest the following, I see that you have loaded the clients in this control: "CustomerSearchBox". Then you can select the client from that list, capture the date in the textbox3 and press the commandbutton3.

Try the following code:

Code:
Private Sub CommandButton3_Click()


    Dim sh As Worksheet
    Dim b As Range
    Dim wName As String
    
    If CustomerSearchBox.ListIndex = -1 Then
        MsgBox "Select customer name from list"
        Exit Sub
    End If
    
    If TextBox3.Value = "" Or Not IsDate(TextBox3.Value) Then
        MsgBox "Enter a valid date"
        TextBox3.SetFocus
        Exit Sub
    End If
    
    wName = CustomerSearchBox.List(CustomerSearchBox.ListIndex)
    Set sh = Sheets("POSTAGE")
    Set b = sh.Columns("B").Find(wName, LookIn:=xlValues, lookat:=xlWhole)
    If Not b Is Nothing Then
        sh.Cells(b.Row, "G").Value = CDate(TextBox3.Value)
        MsgBox "Delivered date updated"
    End If
End Sub
 
Upvote 0
Re: Advice for userform code please

Hmmm,
Not to sure what was supposed to happen with that.

I click DATE RECEIVED,i see a message Select customer from name list.
I click ok to the message & select the customers name.
That customer is then shown on the worksheet & there name in column B is selected.
Now i enter the date into the Column G on that same row.

This works but i then need to manually open the userform again should i need to input some more data whether a date or to complete the whole form etc.

I think i would like to click DATE RECEIVED.
Something is then now on the screen with the customers names.
I select a customer & enter a date.

At this point the date should be entered into the worksheet without me seeing it happen etc ???

The userform is still open at the time so i can the continue entering dates or just completing the complete form.

Thanks
 
Upvote 0
Re: Advice for userform code please

Hi Dante,
Is your code supposed to allow me to enter a date on the userform or take me to the worksheet to enter it ?

I ask as it takes me to the worksheet
 
Upvote 0
Re: Advice for userform code please

Hi Dante,
Is your code supposed to allow me to enter a date on the userform or take me to the worksheet to enter it ?

I ask as it takes me to the worksheet



The date is entered in a textbox and when you press the button that date is passed to the sheet.
 
Upvote 0
Re: Advice for userform code please

OK
It doesnt work like that for me.

Please tell me how it should be done by the code you supplied
 
Upvote 0
Re: Advice for userform code please

Again. Let's make your idea


I think i would like to click DATE RECEIVED.
Something is then now on the screen with the customers names.
I select a customer & enter a date.

What is "DATE RECEIVED"? is it a commandbutton? where is that button, on sheet or on useform? a new userform?


"Something is then now on the screen with the customers names" something like a listbox? do you have a listbox on userform?

"enter a date" Where will you put the date? in a textbox inside the userform? or In a cell on the sheet, in which cell? on which sheet?

So what? Are you going to press another button to put the date in the row corresponding to the customer?



I await your comments
 
Upvote 0
Re: Advice for userform code please

Ok
Date Received is the text on my CommandButton3
This button is on the user form as shown I’m in video clip.

So I press CommandButton3.
Now this is where I need help.
I need to select customers name and assign a date to that name.

With the info we have on the worksheet the customers name is located I. Column B & the date will be entered in column G

I’m not sure of how to do the part in the middle.

Maybe Pressing CommandButton3 now shows a list box which has a list of customers names. Also on this list box is a text field etc where I enter the date.

The list box would have a CANCEL & OK button.
Cancel will close list box.
Ok will enter the date I just entered in column G on the same row as the customer I selected.

I am the. Still on the userform for waiting to continue with my next job input etc.
List box might not be the correct name.

But you get the idea ?

MAIT are you reading this ?
 
Upvote 0
Re: Advice for userform code please

I think it’s call an Input Box.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top