If Isdate not really an IF statement?

bobkap

Active Member
Joined
Nov 22, 2009
Messages
323
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
This line of code (below) is NOT working as a normal IF statement. If I include the "End If" line I get an error message. I also get an error if I try to use a line starting with "else". I need the functionality of a regular IF statement so I can apply code for those situations where my cell does not include a date.

Any suggestions, explanations or ideas would be most appreciated.

"If IsDate(Cells(bkrow, 1)) Then ActiveWorkbook.Sheets.Add Before:=Worksheets(Worksheets.Count)"
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Try this...

Code:
If IsDate(Cells(bkrow, 1)) Then
    ActiveWorkbook.Sheets.Add Before:=Worksheets(Worksheets.Count)
End If
 
Upvote 0
THANKS!! I keep learning VBA. I would have never thought that moving the part after "then" would make this work.

Again, thanks very much.
 
Upvote 0
There are two forms of If statement.

The one liner, which does not have an End If (the Else clause is optional, but must be on the same line if present):

Rich (BB code):
If something Then step_1 Else step_2

Or the multi-line block as Matt showed, which does require an End If:

Rich (BB code):
If something Then
 step_1
Else 
step_2
End If
 
Upvote 0
Thanks for the additional clarification. That would have helped me when I was just started out. :)

In addition when you start getting into nested IF THEN Clauses indention becomes important. So you may want to start practicing....here is an example:


See How all of the statements line up....

Code:
Sub Test()
    If Range("A1") = "Yes" Then
        If Range("B1") = "No" Then
            If Range("C1") = "Another Example" Then
                'Do Something.....
            End If
        End If
    Else
        If Range("D1") = "Something" Then
            'Do Something....
        Else
            'Do Something Different
        End If
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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