+ Reply to Thread
Results 1 to 13 of 13

Entering Dates by typing the month number only

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-20-2014
    Location
    Greece
    MS-Off Ver
    20 yr. old Excel 2002!
    Posts
    710

    Entering Dates by typing the month number only

    Hi

    In a cell formatted as date (month-year) is it possible to:

    i.e enter "12":

    FORMULA BAR shows: Current Year-December-01 (since day not specified).
    The CELL shows: Dec-Current year (2 digit is fine).

    Thanks
    Last edited by drgkt; 01-05-2017 at 06:26 AM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Entering Dates by typing the month number only

    Hi

    You could only do that in the same cell with a Sheet Change macro which personally I think would be OTT

    Why not just enter the month number in a cell, say A1 and alongside in say B1 use the formula

    Formula: copy to clipboard
    =DATE(YEAR(TODAY()),A1,1)


    and Custom format B2 to "mmm yy"
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Contributor
    Join Date
    01-20-2014
    Location
    Greece
    MS-Off Ver
    20 yr. old Excel 2002!
    Posts
    710

    Re: Entering Dates by typing the month number only

    I was looking not to add another column...
    How will the macro work,
    enter number, execute macro, go to next cell enter number, execute macro...?

  4. #4
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Entering Dates by typing the month number only

    Try in attached file first and then adapt this to suit your requirements
    To illustrate, 3 ranges are automatically changed from month number to "month-year"
    B2:B20 =2017 , E2:E20 = 2018, F2:F20 = 2016


    Place VBA in the relevant sheet module
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Selection.Count > 1 Then Exit Sub
        Dim myMonth As Integer
        
        'ranges to be converted from "number" to "Month-year"
        Set myrange = Union(Range("B2:B20"), Range("E2:E20"), Range("F2:F20"))
    
        If Not Intersect(Target, myrange) Is Nothing Then
            On Error Resume Next
            myMonth = Target.Value
            If Err.Number = 13 Then Err.Clear: Exit Sub
            If myMonth > 12 Or myMonth < 1 Then Exit Sub
            
            
            Application.EnableEvents = False 'allows VBA to write a new value to the cell
            Select Case Target.Column
                Case 2 'Column B (= 2nd column)is current year
                Target.Value = MonthName(myMonth, True) & "-" & Year(Date)
                Case 5 'Column E (= 5th column)is next year
                Target.Value = MonthName(myMonth, True) & "-" & Year(Date) + 1
                Case 6 'Column F (= 6th column)is prior year
                Target.Value = MonthName(myMonth, True) & "-" & Year(Date) - 1
            End Select
            Application.EnableEvents = True
    
        End If
    
    End Sub
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    01-05-2017
    Location
    carrer rockledge
    MS-Off Ver
    2013
    Posts
    5

    Re: Entering Dates by typing the month number only

    Using Add-In A-Tools to view calendar (click to input date)
    2017-01-03 13_10_09-Book1 - Excel.png
    2017-01-03 13_10_32-Book1 - Excel.png

  6. #6
    Forum Contributor
    Join Date
    01-20-2014
    Location
    Greece
    MS-Off Ver
    20 yr. old Excel 2002!
    Posts
    710

    Re: Entering Dates by typing the month number only

    Quote Originally Posted by thinhvd View Post
    Using Add-In A-Tools to view calendar (click to input date)
    This is what I have...
    Attached Images Attached Images

  7. #7
    Registered User
    Join Date
    01-05-2017
    Location
    carrer rockledge
    MS-Off Ver
    2013
    Posts
    5

    Re: Entering Dates by typing the month number only

    You must download Add-in A-Tools
    Note: Close all office application before installing
    Installation finish and open excel files. Any cells which is formated date can view as picture.

  8. #8
    Forum Contributor
    Join Date
    01-20-2014
    Location
    Greece
    MS-Off Ver
    20 yr. old Excel 2002!
    Posts
    710

    Re: Entering Dates by typing the month number only

    That's great Kev_!

    Suppose the target range is the entire column A and the year is current year. How do I modify the code?

    In VBA can things be "rem"ed out like in batch files? (Easier to modify the code by moving rems around instead of deleting and rewriting...)

  9. #9
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Entering Dates by typing the month number only

    Try this
    All entries in Column A (below row 1) amended from month number to "month-2017"

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Selection.Count > 1 Then Exit Sub
        If Target.Row < 2 Then Exit Sub
        Dim myMonth As Integer
        
        'range to be converted from "number" to "Month-year"
        Set myrange = Columns("A:A")
    
        If Not Intersect(Target, myrange) Is Nothing Then
            On Error Resume Next
            myMonth = Target.Value
            If Err.Number = 13 Then Err.Clear: Exit Sub
            If myMonth > 12 Or myMonth < 1 Then Exit Sub
            
            Application.EnableEvents = False 'allows VBA to write a new value to the cell
                Target.Value = MonthName(myMonth, True) & "-" & Year(Date)
            Application.EnableEvents = True
    
        End If
    
    End Sub

  10. #10
    Forum Contributor
    Join Date
    01-20-2014
    Location
    Greece
    MS-Off Ver
    20 yr. old Excel 2002!
    Posts
    710

    Re: Entering Dates by typing the month number only

    Thanks!

    I guess NO REMs in VBA?

  11. #11
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Entering Dates by typing the month number only

    You are welcome

    In vba, these 2 lines are the same. The apostrophe is a shortcut for Rem
    Rem My wise words
    ' My wise words
    If you are happy with your solution, please go to Thread Tools (top of thread) and mark the thread as solved.
    thanks

  12. #12
    Forum Contributor
    Join Date
    01-20-2014
    Location
    Greece
    MS-Off Ver
    20 yr. old Excel 2002!
    Posts
    710

    Re: Entering Dates by typing the month number only

    One more question.
    Is there a place in VBA Editor where I can store this as inactive and then drop it into a sheet when I need it?

  13. #13
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Entering Dates by typing the month number only

    There are a few ways to disable the macro.

    One way
    Add a button on the worksheet with this code behind it - it toggles events ON and OFF
    Private Sub CommandButton1_Click()
      Application.EnableEvents = Not Application.EnableEvents
    Note that this enables/disables ALL event macros

    Another way
    (I would elect for this)
    Add this as the first line of code in Private Sub Worksheet_Change
    If Range("A1") = "OFF" Then Exit Sub
    If OFF is entered in cell A1 (or your chosen cell) the macro exits without doing anything
    You could have a choice of OFF and ON in the cell (using a dropdown)

    Yet another way
    (a bit of a "back of fag-packet" solution!)
    Rename the macro with an X in front of the name
    so "Private Sub Worksheet_Change.." , become "XPrivate Sub Worksheet_Change..."
    and then it won't run

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Number of Days in each month between two dates
    By marshymell0 in forum Excel General
    Replies: 11
    Last Post: 06-15-2015, 01:28 AM
  2. Number of Month between two dates
    By suhabthan in forum Excel General
    Replies: 7
    Last Post: 03-04-2015, 12:59 PM
  3. [SOLVED] count the number of dates for last month and the number of dates for this month
    By JmundleBofA in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-25-2014, 08:37 PM
  4. [SOLVED] Convert number into Dates (End of Month)
    By acsishere in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-14-2013, 02:17 AM
  5. Counting number of unique dates per month in a list of duplicate dates
    By Rackle83 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 04-22-2013, 10:15 AM
  6. Replies: 2
    Last Post: 12-01-2012, 03:06 PM
  7. Count number of dates in one month
    By korygrandy in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-18-2008, 04:20 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1