Create "if then" statement in button click

Miler13

New Member
Joined
Feb 5, 2017
Messages
43
So what I am trying to do is when a button is clicked, depending on if a certain cell contains a text, will bring up a certain userform. So if "I6" has a date in it, userform1 would show. If "I6" and "I10" have a date in it, then useform2 would show. If "I6", "I10" and "I14" have a date in it, it would bring up userform3.

It seems like when I try to do a If Then statement I can get it to appear but then the following userform also shows.

Any help?
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Perhaps.
Code:
Private Sub CommandButton1_Click()

    If IsDate(Range("16").Value) And IsDate(Range("I10").Value) And IsDate(Range("I14").Value) Then
        UserForm3.Show
    End If

    If IsDate(Range("16").Value) And IsDate(Range("I10").Value)  Then
        UserForm2.Show
    End If


    If IsDate(Range("16").Value) Then
        UserForm1.Show
    End If
 
Upvote 0
Hi,
try

Code:
Sub CommandButton1_Click()
    Dim ObjForm As Object
    Dim cell As Range
    Dim i As Integer
    For Each cell In Range("I6,I10,I14")
        If Not IsDate(cell.Text) Then Exit For Else i = i + 1
    Next cell
    If i > 0 Then Set ObjForm = UserForms.Add("UserForm" & i): ObjForm.Show
End Sub

solution assumes dates must be filled in sequence i.e. I6 onward.

Dave
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,089
Members
453,021
Latest member
Justyna P

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