+ Reply to Thread
Results 1 to 4 of 4

Clear userform if expiry date exceeds Today

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-06-2009
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    361

    Clear userform if expiry date exceeds Today

    Hi, attached is a file that we use to generate labels for our reagents. After selecting a reagent and the lot number, an expiry date is populated based on the data in the "Expiry" tab. What I hope to achieve is to have a popup msg appear when a user selects an expired chemical and the userform will clear so that the user can start again. Have tried some methods including addition of a textbox that shows today's date, but seems like im not getting it right.The textbox showing today's date can be removed if it is not required.
    Last edited by bqheng; 11-05-2015 at 09:14 PM.

  2. #2
    Forum Contributor Obsessed's Avatar
    Join Date
    05-22-2013
    Location
    Cincinnati, Ohio
    MS-Off Ver
    Excel 365
    Posts
    215

    Re: Clear userform if expiry date exceeds Today

    Here you go...just modified your cboLot event handler:

    Private Sub cboLot_Change()
        Dim rng As Range
        Dim cell As Range
        
        Set sh = ThisWorkbook.Sheets("Expiry")
        Set rng = sh.Range("A2", sh.Cells(sh.Rows.Count, "A").End(xlUp))
        
        On Error Resume Next
        
        For Each cell In rng.Cells
            If cell.Value = Me.cboReagent.List(Me.cboReagent.ListIndex) Then
                If CStr(cell(1, 2).Value) = Me.cboLot.List(Me.cboLot.ListIndex) Then
                    TextBox3.Value = cell(1, 3).Text
                    g_Date_Expiry = cell(1, 3).Value
                    If cell(1, 3).Value <= Date Then
                        MsgBox ("This lot expired. Please select different lot.")
                        Unload UserForm1
                        UserForm1.Show
                    End If
                    Exit Sub
                End If
            End If
        Next cell
    End Sub
    Want to show appreciation for the help you received from a member? Give them reps by clicking the bottom left of their post!

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Clear userform if expiry date exceeds Today

    You may want to consider checking the expiry date when populating the Lot combobox and just omitting expired lots.

    Private Sub cboReagent_Change()
        Dim cUnique As Collection
        Dim index As Integer
        index = cboReagent.ListIndex
        
        cboLot.Clear
        
        Dim rng As Range
        Dim cell As Range
        Dim vNum As Variant
        Dim sh As Worksheet
        
        Set sh = ThisWorkbook.Sheets("Expiry")
        
        'Extract unique chemical names from column A
        Set rng = sh.Range("A2", sh.Cells(sh.Rows.Count, "A").End(xlUp))
        Set cUnique = New Collection
        
        On Error Resume Next
        For Each cell In rng.Cells
            If cell.Value = Me.cboReagent.List(index) And cell.Offset(, 2).Value > Date Then
                cUnique.Add cell(1, 2).Value, CStr(cell(1, 2).Value)
            End If
        Next cell
        On Error GoTo 0
        If cUnique.Count = 0 Then cUnique.Add "No active lots available."
        
        For Each vNum In cUnique
            Me.cboLot.AddItem vNum
        Next vNum
        
        If cboReagent.Value = "Kastle Meyer Reagent" Or cboReagent.Value = "3% (w/w) Hydrogen Peroxide" Or cboReagent.Value = "Absolute Alcohol" Then
        cboName.Enabled = True
        TextBox2.Enabled = False
        Else
        cboName.Enabled = False
        TextBox2.Enabled = True
        End If
    
    End Sub
    You could also test the date when populating the
    Last edited by AlphaFrog; 11-04-2015 at 10:16 PM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Forum Contributor
    Join Date
    05-06-2009
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    361

    Re: Clear userform if expiry date exceeds Today

    Thank you both for your suggestions. Have opted for AlphaFrog's code and tweaked it to disable the commandbutton

+ 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. [SOLVED] Clear multiple cells when a dropdown is changed and reset to today's date
    By GregHorn27 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-20-2015, 10:37 AM
  2. autocalculate expiry date / expiry date colour warnings
    By kimbling1 in forum Excel General
    Replies: 8
    Last Post: 08-27-2014, 02:53 AM
  3. [SOLVED] Prefill text box in userform with today's date but allow user to enter unique date
    By moosetales in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-02-2014, 09:17 AM
  4. [SOLVED] Listbox in userform to populate with all rows containing the Date today AND 2 weeks prior
    By cath1509 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-05-2013, 08:50 PM
  5. Userform: Calender to open with today's date as default
    By Ashali in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-11-2012, 07:01 AM
  6. Clear cell based on expiry date in another cell
    By hezcal11 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-11-2011, 12:01 PM
  7. Cmdbutton to add today's date in a userform
    By Dave12345 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-22-2010, 01:34 PM

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