I'm receiving an error with my VBA and don't understand why:
I'm trying to make a MsgBox appear when the activecell in the active workbook does not meet a criteria.
here's my short line of code:
If ActiveWorkbook.ActiveCell <> ActiveCell.NumberFormat = "mm/dd/yyyy hh:mm:ss" Then MsgBox "Please Enter a Date"
The error I receive is:
Run-time error '438':
Object doesn't support this property or method.
Does anyone have an idea about why this would happen?
EDIT:
Currently we have a macro in our workbooks that automatically records the time NOW() when an analyst clicks an empty cell. They can go back and change this date (because sometimes they need to), but sometimes they forget to add in the MM/DD/YYYY format, and just have hh:mm:ss, which doesn't help when we're trying use these cells to subtract from different dates. I want the analyst to be required to make sure the cell they just changed is in the format 'mm/dd/yyyy hh:mm:ss'
SAMPLE: Here's some sample code I have so far:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
'
' TimeStamp Macro
'
Dim AutoTime
AutoTime = True 'set to False to stop auto date/time insertion on mouse click
If (AutoTime And ActiveCell = "" And ActiveCell.Column > 6 And ActiveCell.Column < 17 And ActiveCell.Row > 3) Then
ActiveCell.FormulaR1C1 = "=NOW()"
Selection.Copy
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Selection.NumberFormat = "hh:mm:ss"
End Sub
Which someone I work with provided. What this does it enters in NOW() to an active cell when an analyst clicks it, if the cell is blank.
So,
G H I
1/5/2017
Becomes,
G H I
1/5/2017 1/5/2017 09:30:00 A.M.
Now the analyst can again click I, and the cell will return NOW().
The issue:
Sometimes analyst change these times, especially when the NOW() function wasn't needed. What they don't do is include the '1/5/2017 ' in front, so now I have
G H I
1/5/2017 1/5/2017 09:30:00 A.M. 10:45:00 A.M.
I want a message box to appear when any of the cells in my range that they changed manually doesn't include 'mm/dd/yyyy' because excel doesn't like I1 - H1
Because there is no ActiveCell property inside ActiveWorkbook.
ActiveCell is application property and it will be always on the ActiveWorkbook.
If Not IsDate(ActiveCell) Or ActiveCell.NumberFormat <> "mm/dd/yyyy hh:mm:ss" Then
MsgBox "Enter date!"
End If
To check if the ActiveCell contains a Valide Date +Time and not 1900 as date :
Sub TEST()
If Not IsEmpty(ActiveCell) Then
If Not Application.Evaluate("=AND(YEAR(" & ActiveCell.Value2 & ") >=2016,MOD(" & ActiveCell.Value2 & ",2))") Then
MsgBox "Not a valid date time!!"
End If
End If
End Sub
Any date before the year 2016 will be flagged. so will any entry with just date and no time.
Breaking your line down into its components
If ActiveWorkbook.ActiveCell <> ActiveCell.NumberFormat = "mm/dd/yyyy hh:mm:ss" Then MsgBox "Please Enter a Date"
The first thing that Excel does is look for the ActiveCell
property of the ActiveWorkbook
object. That property doesn't exist, so you get your error.
But let's pretend it does (and that you had just used ActiveCell
), then the second thing that happens is that it compares ActiveCell
's default Value
property with ActiveCell
's NumberFormat
property. Those two values are unlikely to be the same, so a test for <>
will be True
.
Next, a comparison of True
is made with the string "mm/dd/yyyy hh:mm:ss". Even if you don't get a "type mismatch" error, that would return a False
.
Based on your comments to the question, what you want is:
If ActiveCell.Value2 < 1 Then MsgBox "Please Enter a Date"
How the cell is formatted should not impact on whether the date is valid or not, it only affects how the cell's value is displayed .
The functionality to test for the validity of entered values needs to go into the Change
event rather than the SelectionChange
event, so I would recommend changing your code to:
Option Explicit
Private Const AutoTime As Boolean = True 'set to False to stop auto date/time insertion on mouse click
Private Sub Worksheet_Change(ByVal Target As Range)
If Not AutoTime Then
Exit Sub
End If
Application.EnableEvents = False
Dim c As Range
For Each c In Target
If c.Value <> "" And c.Column > 6 And c.Column < 17 And c.Row > 3 Then
If Not IsNumeric(c.Value2) Then
MsgBox "Cell " & c.Address & " - Please enter a valid date/time"
ElseIf c.Value2 < 1 Then
MsgBox "Cell " & c.Address & " - Please enter the date as well as the time"
End If
End If
Next
Application.EnableEvents = True
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'
' TimeStamp Macro
'
If Not AutoTime Then
Exit Sub
End If
Application.EnableEvents = False
Dim c As Range
For Each c In Target
If c.Value = "" And c.Column > 6 And c.Column < 17 And c.Row > 3 Then
c.Value = Now()
c.NumberFormat = "hh:mm:ss"
End If
Next
Application.EnableEvents = True
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.