Limiting Input by Time of Day

by Allen Wyatt
(last updated December 10, 2016)


Steven needs to ensure that nobody enters information into a worksheet between 4:00 pm and 6:30 pm each day. He wonders if it is possible to use Data Validation to prevent a user from entering information in a worksheet during a particular time each day.

There are two general ways you can go about this. One approach is to use Data Validation to check the time and either allow or disallow data entry.

  1. Select all the cells in the worksheet.
  2. Display the Data tab of the ribbon.
  3. Click the Data Validation tool in the Data Tools group. Excel displays the Data Validation dialog box. (See Figure 1.)
  4. Figure 1. The Data Validation dialog box.

  5. Using the Allow drop-down list, choose Custom.
  6. Enter the following in the Formula box:
  7.        =OR(24*MOD(NOW(),1)<16,24*MOD(NOW(),1)>18.5)
  8. Make changes on the Error Alert tab, as desired.
  9. Click OK.

The problem with this approach is in the very first step: You need to select all the cells in the worksheet in order to prevent data being entered in any of them. Plus, if you already are using Data Validation in any of the cells, this approach will overwrite those settings.

For these reasons, it may be better to use a macro-based approach. All such approaches can utilize event handlers to check for any changes. The following relies on the Worksheet_Change event, which means it is triggered only when Excel detects a change in the worksheet.

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim sMsg As String

    sMsg = "No entries allowed between 4:00 pm and 6:30 pm!"
    If Time >= "4:00:00 PM" And Time <= "6:30:00 PM" Then
        MsgBox sMsg, vbCritical
        With Application
            .EnableEvents = False
            .Undo   ' This undoes the change the person made
            .EnableEvents = True
        End With
    End If
End Sub

Essentially, every time there is a change in the worksheet, the handler checks to see if it is between 4:00 pm and 6:30 pm. If it is, then a message box is displayed to indicate the error, and then the .Undo method is used to roll back any change that was attempted.

If you prefer, you could take a different approach and protect the worksheet if it is within the banned time:

Private Sub Worksheet_Activate()
     If Time >= "4:00:00 PM" And Time <= "6:30:00 PM" Then
        MsgBox "Worksheet is protected."
        MsgBox "You are free to edit now."
    End If
End Sub

The Worksheet_Activate event handler is invoked every time the worksheet is activated (selected). If the worksheet is activated anytime outside of the banned time, then it is unprotected. Of course, the user could still manually unprotect the worksheet even during the banned time, so it is a good idea to use this approach in conjunction with an approach that is triggered every time a change is attempted, as discussed earlier.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13503) applies to Microsoft Excel 2007, 2010, 2013, and 2016.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...


Creating 3-D Formatting for a Cell

The formatting capabilities provided by Excel are quite diverse. This tip examines how you can use those capabilities to ...

Discover More

Grabbing the MRU List

Excel keeps track of the most recent workbooks you've used. If you want to access that information in a macro, you'll ...

Discover More

Using an Exact Number of Digits

Excel allows you to format numeric data in all sorts of ways, but specifying a number of digits independent of the ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Inserting and Deleting Rows in a Protected Worksheet

You've protected and saved your worksheet with explicit instructions that you be allowed to insert and delete rows. But ...

Discover More

Password Protecting Specific Columns in a Worksheet

When you are developing a worksheet for others to use, you might want to protect some of the information in that ...

Discover More

Functioning Check Boxes in a Protected Worksheet

Want to protect a worksheet but maintain the ability to make changes to the check boxes you add to the worksheet? Here is ...

Discover More

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.


If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 9 + 8?

2016-12-13 06:23:42


Thank you all for comments and also thank you zeddy for finding mistake.

2016-12-12 11:04:24


NOTE: If your banned time was before 8:30am and after 6:00pm (e.g. 'outside office hours') , you can't just edit the code above with your new start and finish times i.e. the following doesn't work..
If Time >= "6:00:00 PM" And Time <= "8:30:00 AM" Then
..doesn't work.
You would need to use..
If Time > TimeValue( "6:00:00 PM") Or Time < TimeValue( "8:30:00 AM") Then

If you wanted apply restrictions to all other Users except yourself, you could add something like this as the first vba code line:
If Environ("UserName") = "Richard" then Exit Sub '<<adjust for allowed Username

To find your Username, run a test routine with this..
Msgbox Environ("UserName")

If you wanted to only allow changes to be made on a particular computer , you could use something like this..
If Environ("ComputerName") <> "DELL-09" then '<<adjust for name of allowed Computer

To find the name of the computer you are working on, run a test routine with this..
Msgbox Environ("ComputerName")

..these are simple methods. More robust methods can be used.


2016-12-12 09:37:55


Hi Oksana,

I read your comment and I think the way to block a user is again in VBA:
' Declare for call to mpr.dll. For Get User Name" sub.
Declare Function WNetGetUser Lib "mpr.dll" _
Alias "WNetGetUserA" (ByVal lpName As String, _
ByVal lpUserName As String, lpnLength As Long) As Long
Const NoError = 0 'The Function call was successful

then this sub..
Sub GetUserName()

' Buffer size for the return string.
Const lpnLength As Integer = 255

' Get return buffer space.
Dim status As Integer
Dim txtTimeStampFile As String ' Original Date stamp variable for file naming
Dim txtTimeStampFixedFile As String ' Fixed Date stamp variable for file naming

' Get name of calling sheet and save it so that you can return to it...
ReturnSheet = ActiveSheet.Name

' For getting user information.
Dim lpName, lpUserName As String
' Unprotect the sheets.

' Assign the buffer size constant to lpUserName.
lpUserName = Space$(lpnLength + 1)

' Get the log-on name of the person using product.
status = WNetGetUser(lpName, lpUserName, lpnLength)
txtTimeStampFile = Now()
txtTimeStampFixedFile = Format(txtTimeStampFile, "yyyy/mm/dd hh:mm:ss")
' See whether error occurred.
If status = NoError Then
' This line removes the null character. Strings in C are null-
' terminated. Strings in Visual Basic are not null-terminated.
' The null character must be removed from the C strings to be used
' cleanly in Visual Basic.
lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
ActiveCell.Offset(1, 0).Select
ActiveCell.Value = lpUserName
ActiveCell.Offset(0, 1).Value = txtTimeStampFixedFile
' An error occurred.
MsgBox "Unable to get the name."
End If
' Display the name of the person logged on to the machine.
' MsgBox "The person logged on this machine is: " & lpUserName

End Sub

This wil lwrite the user name to a cell that you then can compare with to see if it is the name tha you want blocked.

2016-12-12 06:47:51


The other flaw in this approach is the security settings within the particular installation of Excel itself. If the security settings are such that macros are prohibited from running then there will be nothing to stop anyone editing the worksheet.

To overcome this there should be a default of having the worksheet protected come what may. So the protection should be applied every time the workbook is saved and only removed during the permitted time period or for a particular change. So that if macros are disabled the workbook will only be opened with full protection being ON; only Users with macros enabled will be permitted to makes changes and then only during the permitted timeslot.

2016-12-11 06:58:35


Thank you, Allen! it's very interesting.
Does exist the way to protect the file from definite user?

This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.