Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9242833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:40:10+00:00 2026-06-18T08:40:10+00:00

Please can someone help with the following code. it gives me an error at

  • 0

Please can someone help with the following code. it gives me an error at the following line:

Set range = "C5:L14"

This is the complete code:

Private Sub Worksheet_Change(ByVal Target As Excel.range)
Dim ws As Worksheet
Dim range As Worksheet

Set ws = Application.ActiveSheet
Set range = "C5:L14"

If Not Application.Intersect(Target, range("C5:L14")) Is Nothing Then

    If range("C5:L14").Value = "" Then Exit Sub

    If range("C5:L14").Date = "< today()" Then Exit Sub

    If range("C5:L14").Date = "> today()" Then MsgBox ("Future dates not allowed!")

    Else
        MsgBox ("Please enter date as follows yyyy-mm")
    End If

End Sub

The date is formatted to “2013 Jan” on the cells. Future dates are not allowed and the user should only type in the date as “2013-01”. The format should change it correctly. If they type in “2013 Jan” the Conditional formatting does not pick it up. Have tried DATA VALIDATION but it only limits me to one.

I need the macro to make sure a user doesn’t enter an incorrect date in the cells specified.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:40:11+00:00Added an answer on June 18, 2026 at 8:40 am

    What you are trying can be solved without VBA as well. However I am showing you both the methods. Take your pick

    NON VBA

    Select the cell where you want to apply Data Validation and then follow these steps.

    Step 1

    enter image description here

    Step 2

    enter image description here

    Step 3

    enter image description here

    Step 4

    enter image description here

    In Action

    enter image description here

    VBA

    I have commented the code so you will not have any problem in understanding it

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
        Dim rng As Range
        Dim aCell As Range
    
        '~~> The below two lines are required. Read up more on
        '~~> http://stackoverflow.com/questions/13860894/ms-excel-crashes-when-vba-code-runs/13861640#13861640
        On Error GoTo Whoa
        Application.EnableEvents = False
    
        '~~> Set your range
        Set rng = Range("C5:L14")
    
        If Not Application.Intersect(Target, rng) Is Nothing Then
            '~~> Loop through all cells in the range
            For Each aCell In rng
                If aCell.Value <> "" Then
                    If aCell.Value > Date Then
                        aCell.ClearContents
                        MsgBox "Future date not allowed in cell " & aCell.Address
                    ElseIf IsDate(aCell.Value) = False Then
                        aCell.ClearContents
                        MsgBox "Incorrect date in cell " & aCell.Address
                    Else
                        aCell.Value = Format(aCell.Value, "yyyy-mm")
                    End If
                End If
            Next
        End If
    
    Letscontinue:
        Application.EnableEvents = True
        Exit Sub
    Whoa:
        MsgBox Err.Description
        Resume Letscontinue
    End Sub
    

    Hope this helps?

    EDIT:

    A slight change. In the Step 4 of the Non VBA Method, I typed “yyyy mm” by mistake. Change that to “yyyy-mm”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please help me with the following code: Dim NewOutputFile As Workbook Set
Please can someone help? I have the following code which uploads a file to
can someone please help me. why does this return an error: Dim stuff As
Can someone please help me to understand this following partial parallel sum algorithm implementation
I am new to android can someone help me please, following is the code
Can someone please help me with the following code? The methods runs until ss.accept();
Can someone please help me figure out a way to achieve the following (see
I wonder whether someone can help me please. I'm using the following script to
Im a beginner to css, please can someone help me get this element to
Can someone please help me out with a JavaScript/jQuery solution for this arithmetic problem:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.