+ Reply to Thread
Results 1 to 3 of 3

VBA code to find and delete cell contents based on userform combobox selections

Hybrid View

anfdrew VBA code to find and delete... 07-05-2013, 11:49 PM
AlphaFrog Re: VBA code to find and... 07-06-2013, 02:05 AM
anfdrew Re: VBA code to find and... 07-06-2013, 04:13 PM
  1. #1
    Registered User
    Join Date
    07-20-2012
    Location
    us
    MS-Off Ver
    Excel 2003
    Posts
    42

    VBA code to find and delete cell contents based on userform combobox selections

    I have 3 Worksheets that are exactly the same in setup to record dates for different events(For this example you can call them sheet1, sheet2 and sheet3). I have a userform that has two comboboxs and a textbox. The first combobox (combobox1) allows the user to select from a list of names that is in column “a” on each work sheet. Combobox 2 allows the user to select the worksheet. The text box allows the user to enter a date.

    What I need is a VBA code that when a user clicks on a delete button it selects the worksheet from combobox 2, finds the row which the name selected in combobox1 is on and finds the date in the textbox within that row and deletes that date only.

    I do not have any idea how to set this code up. Please help

    Thanks

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

    Re: VBA code to find and delete cell contents based on userform combobox selections

    Private Sub CommandButton1_Click()
        
        Dim r As Variant, c As Variant
        
        If Me.ComboBox2.ListIndex = -1 Then
            MsgBox "No sheet selected. ", , "Invalid Entry"
            Exit Sub
        ElseIf Me.ComboBox1.ListIndex = -1 Then
            MsgBox "No name selected. ", , "Invalid Entry"
            Exit Sub
        ElseIf Not IsDate(Me.TextBox1.Value) Then
            MsgBox "Invalid date. ", , "Invalid Entry"
            Exit Sub
        End If
        
        Sheets(Me.ComboBox2.Value).Select
        
        r = Application.Match(Me.ComboBox1.Value, Range("A:A"), 0)
        If IsError(r) Then
            MsgBox "No match for " & Me.ComboBox1.Value, , "No Name Match Found"
            Exit Sub
        End If
        
        c = Application.Match(CLng(CDate(Me.TextBox1.Value)), Rows(r), 0)
        If IsError(c) Then
            MsgBox "No match for date " & Me.TextBox1.Value, , "No Date Match Found"
            Exit Sub
        End If
        
        Cells(r, c).ClearContents
        
    End Sub
    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.

  3. #3
    Registered User
    Join Date
    07-20-2012
    Location
    us
    MS-Off Ver
    Excel 2003
    Posts
    42

    Re: VBA code to find and delete cell contents based on userform combobox selections

    Thank you! this worked perfectly!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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