+ Reply to Thread
Results 1 to 14 of 14

Macro for prompting date entry

Hybrid View

  1. #1
    Registered User
    Join Date
    09-08-2011
    Location
    New York
    MS-Off Ver
    Excel 2003
    Posts
    20

    Macro for prompting date entry

    Hi all, I need to create a condition in which a input prompt pops up for date entry.

    I have two problems currently present in my spreadsheet. The first one is that i need to have the message of the equation
    =IF(AND(F10="Pending",M1="Pending"), "Please Enter Date", "")
    displayed in the cell upon fulfilling the condition followed by the prompting of entry of the date.

    The second problem I currently face is that I can't seem to get the date format to display in the input box as it only displays numbers.

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
         
        Dim Rng1            As Range
        Dim Prompt          As String
        Dim Title           As String
        Dim Date1       As Long
        
         
        Set Rng1 = Me.Range("A1:A50")
        If Intersect(Target(1, 1), Rng1) Is Nothing Then
            Exit Sub
        End If
        Cancel = True
        On Error Resume Next
        Prompt = "Enter date formatted: mm/dd/yyyy"
        Title = "[Please Enter Date]"
        Date1 = InputBox(Prompt, Title)
         
        
         
        If Err <> 0 Then
            On Error GoTo 0
            Exit Sub
        End If
        On Error GoTo 0
         
        Target(1, 1).NumberFormat = "@"
        Target(1, 1).Value = Date1 & ""
         
    End Sub
    If anyone could provide some help to where I went wrong, that would be greatly appreciated.
    Last edited by Armored Wing; 01-17-2012 at 10:55 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Macro for prompting date entry

    Your first problem is not clearly described.
    For Your second problem use the following code instead

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
         
        Dim Rng1            As Range
        Dim Prompt          As String
        Dim Title           As String
        Dim strDate1       As String
        Dim Date1 As Date
         
        Set Rng1 = Me.Range("A1:A50")
        If Intersect(Target(1, 1), Rng1) Is Nothing Then
            Exit Sub
        End If
        Cancel = True
        
        Prompt = "Enter date formatted: mm/dd/yyyy"
        Title = "[Please Enter Date]"
        strDate1 = InputBox(Prompt, Title)
        
        
        On Error Resume Next
        Date1 = CDate(strDate1)
        If Err <> 0 Or strDate1 = "" Then
            Exit Sub
        End If
        On Error GoTo 0
    
        Target(1, 1).Value = VBA.Format(Date1, "MM/DD/YYYY")
        
    End Sub
    Regards,
    Khaled Elshaer
    www.BIMcentre.com

    Remember To Do the Following....
    1. Thank those who have helped you by clicking the Star below their post.
    2. Mark your post SOLVED if it has been answered satisfactorily:
    • Select Thread Tools (on top of your 1st post)
    • Select Mark this thread as Solved

  3. #3
    Registered User
    Join Date
    09-08-2011
    Location
    New York
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Macro for prompting date entry

    All right I have uploaded the sheet that I am currently working on. First, the second code worked so I want to thank you Kelshaer. My first problem now is that when any cell becomes "pending" in column F, I would like the date entry code to execute corresponding to the cell that become pending (so if cell F10 became pending, A10 would pop up the date prompt), otherwise the code won't execute at all. I hope that help clear up some confusion anyone might have.
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Macro for prompting date entry

    Here you go

    Option Explicit
    
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
        Dim r, c, u As Integer
        r = Target.Row
        c = Target.Column
        u = ActiveSheet.Cells.SpecialCells(xlLastCell).Row
        If c > 1 Or r > u Or VBA.UCase(Range("F" & r)) <> "PENDING" Then Exit Sub
        
        Dim Prompt          As String
        Dim Title           As String
        Dim strDate1       As String
        Dim Date1 As Date
        
        Prompt = "Enter date formatted: mm/dd/yyyy"
        Title = "[Please Enter Date]"
        strDate1 = InputBox(Prompt, Title)
        
        
        On Error Resume Next
        Date1 = CDate(strDate1)
        If Err <> 0 Or strDate1 = "" Then
            Exit Sub
        End If
        On Error GoTo 0
        Target(1, 1).Value = VBA.Format(Date1, "mm/dd/yyyy")
    End Sub

  5. #5
    Registered User
    Join Date
    09-08-2011
    Location
    New York
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Macro for prompting date entry

    I tried it out but the prompt does not pop up when pending display in F? Am I missing something?

  6. #6
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Macro for prompting date entry

    Its working for me
    Please find the attached file
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    09-08-2011
    Location
    New York
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Macro for prompting date entry

    Ok the macro does work on the active row and cell but is that a way to make it automatically appear the f column being turned into pending? Since I still have to double click it in order for the box to show up.
    Last edited by Armored Wing; 01-13-2012 at 02:33 PM.

  8. #8
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Macro for prompting date entry

    If you mean to make it launch without considering which column was double clicked then use this:
    Although i dont recommend it this way as you might double click any other column to enter data.
    Option Explicit
    
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
        Dim r , u As Integer
        r = Target.Row
        u = ActiveSheet.Cells.SpecialCells(xlLastCell).Row
        If  r > u Or VBA.UCase(Range("F" & r)) <> "PENDING" Then Exit Sub
        
        Dim Prompt          As String
        Dim Title           As String
        Dim strDate1       As String
        Dim Date1 As Date
        
        Prompt = "Enter date formatted: mm/dd/yyyy"
        Title = "[Please Enter Date]"
        strDate1 = InputBox(Prompt, Title)
        
        
        On Error Resume Next
        Date1 = CDate(strDate1)
        If Err <> 0 Or strDate1 = "" Then
            Exit Sub
        End If
        On Error GoTo 0
        Target(1, 1).Value = VBA.Format(Date1, "mm/dd/yyyy")
    End Sub
    Last edited by Kelshaer; 01-13-2012 at 02:35 PM.

  9. #9
    Registered User
    Join Date
    09-08-2011
    Location
    New York
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Macro for prompting date entry

    I guess what I want is that instead of double clicking, the moment that the cell F10 becomes pending, the cell in A10 will automatically prompt the user in entering a date which will display in cell A10. I don't know if that is feasible or not.

  10. #10
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Macro for prompting date entry

    Hope this will work


    Option Explicit
    
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
        Dim r, c, u As Integer
        r = Target.Row
        c = Target.Column
        u = ActiveSheet.Cells.SpecialCells(xlLastCell).Row
        If c > 1 Or r > u Or VBA.UCase(Range("F" & r)) <> "PENDING" Then Exit Sub
        
        Dim Prompt          As String
        Dim Title           As String
        Dim strDate1       As String
        Dim Date1 As Date
        
        Prompt = "Enter date formatted: mm/dd/yyyy"
        Title = "[Please Enter Date]"
        strDate1 = InputBox(Prompt, Title)
        
        
        On Error Resume Next
        Date1 = CDate(strDate1)
        If Err <> 0 Or strDate1 = "" Then
            Exit Sub
        End If
        On Error GoTo 0
        Target(1, 1).Value = VBA.Format(Date1, "mm/dd/yyyy")
    End Sub
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Column <> 4 And Target.Column <> 5 Then Exit Sub
        If VBA.UCase(Range("F" & Target.Row)) <> "PENDING" Or Range("A" & Target.Row) <> "" Then Exit Sub
            Dim Prompt          As String
        Dim Title           As String
        Dim strDate1       As String
        Dim Date1 As Date
        
        Prompt = "Enter date formatted: mm/dd/yyyy"
        Title = "[Please Enter Date]"
        strDate1 = InputBox(Prompt, Title)
        
        
        On Error Resume Next
        Date1 = CDate(strDate1)
        If Err <> 0 Or strDate1 = "" Then
            Exit Sub
        End If
        On Error GoTo 0
        Range(Target.Row, 1).Value = VBA.Format(Date1, "mm/dd/yyyy")
        
        
    End Sub

  11. #11
    Registered User
    Join Date
    09-08-2011
    Location
    New York
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Macro for prompting date entry

    It auto opens now but now an error message displays the run-time error 1004 with the range being the object that failed.
    Range(Target.Row, 1).Value = VBA.Format(Date1, "mm/dd/yyyy")

  12. #12
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Macro for prompting date entry

    So Sorry, My mistake.
    Here is it:


    
    
    
    Option Explicit
    
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
        Dim r, c, u As Integer
        r = Target.Row
        c = Target.Column
        u = ActiveSheet.Cells.SpecialCells(xlLastCell).Row
        If c > 1 Or r > u Or VBA.UCase(Range("F" & r)) <> "PENDING" Then Exit Sub
        
        Dim Prompt          As String
        Dim Title           As String
        Dim strDate1       As String
        Dim Date1 As Date
        
        Prompt = "Enter date formatted: mm/dd/yyyy"
        Title = "[Please Enter Date]"
        strDate1 = InputBox(Prompt, Title)
        
        
        On Error Resume Next
        Date1 = CDate(strDate1)
        If Err <> 0 Or strDate1 = "" Then
            Exit Sub
        End If
        On Error GoTo 0
        Target(1, 1).Value = VBA.Format(Date1, "mm/dd/yyyy")
    End Sub
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Column <> 4 And Target.Column <> 5 Then Exit Sub
        If VBA.UCase(Range("F" & Target.Row)) <> "PENDING" Or Range("A" & Target.Row) <> "" Then Exit Sub
            Dim Prompt          As String
        Dim Title           As String
        Dim strDate1       As String
        Dim Date1 As Date
        
        Prompt = "Enter date formatted: mm/dd/yyyy"
        Title = "[Please Enter Date]"
        strDate1 = InputBox(Prompt, Title)
        
        
        On Error Resume Next
        Date1 = CDate(strDate1)
        If Err <> 0 Or strDate1 = "" Then
            Exit Sub
        End If
        On Error GoTo 0
        Range("A" & Target.Row).Value = VBA.Format(Date1, "mm/dd/yyyy")
        
        
    End Sub

  13. #13
    Registered User
    Join Date
    09-08-2011
    Location
    New York
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Macro for prompting date entry

    Please don't apologize for the errors, I just wish I was better at VBA so I can complete harder tasks. However, I am extremely grateful for your help, but I was wondering if you could help me out with one last thing. Using the code that you wrote, now I have to apply it to my finalized spreadsheet in which the columns which are pending will automatically prompt the user. I tried the code out but was unable to get the box opened. I will upload the finalized sheet for your reference.

    Once again, I am extremely grateful for your help.
    Attached Files Attached Files

  14. #14
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Macro for prompting date entry

    Try this one

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
        Dim r, c, u As Integer
        r = Target.Row
        c = Target.Column
        u = ActiveSheet.Cells.SpecialCells(xlLastCell).Row
        If c <> 5 And c <> 11 Then Exit Sub
        If r > u Then Exit Sub
        If c = 5 Then
            If VBA.UCase(Range("D" & r)) <> "PENDING" Then Exit Sub
        ElseIf c = 11 Then
            If VBA.UCase(Range("J" & r)) <> "PENDING" Then Exit Sub
        End If
        
        Dim Rng1            As Range
        Dim Prompt          As String
        Dim Title           As String
        Dim strDate1       As String
        Dim Date1 As Date
        
        Prompt = "Enter date formatted: mm/dd/yyyy"
        Title = "[Please Enter Date]"
        strDate1 = InputBox(Prompt, Title)
        
        
        On Error Resume Next
        Date1 = CDate(strDate1)
        If Err <> 0 Or strDate1 = "" Then
            Exit Sub
        End If
        On Error GoTo 0
        Target.Value = VBA.Format(Date1, "mm/dd/yyyy")
    End Sub
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Target.Column <> 2 And Target.Column <> 3 And Target.Column <> 8 And Target.Column <> 9 Then Exit Sub
        
        If Target.Column = 2 Or Target.Column = 3 Then
            If VBA.UCase(Range("D" & Target.Row)) <> "PENDING" Or Range("E" & Target.Row) <> "" Then Exit Sub
        ElseIf Target.Column = 8 Or Target.Column = 9 Then
            If VBA.UCase(Range("J" & Target.Row)) <> "PENDING" Or Range("K" & Target.Row) <> "" Then Exit Sub
        End If
        
        Dim Prompt          As String
        Dim Title           As String
        Dim strDate1       As String
        Dim Date1 As Date
       
        
        Prompt = "Enter date formatted: mm/dd/yyyy"
        Title = "[Please Enter Date]"
        strDate1 = InputBox(Prompt, Title)
        
        
        On Error Resume Next
        Date1 = CDate(strDate1)
        If Err <> 0 Or strDate1 = "" Then
            Exit Sub
        End If
        On Error GoTo 0
        If Target.Column = 2 Or Target.Column = 3 Then
            Range("E" & Target.Row).Value = VBA.Format(Date1, "mm/dd/yyyy")
        ElseIf Target.Column = 8 Or Target.Column = 9 Then
            Range("K" & Target.Row).Value = VBA.Format(Date1, "mm/dd/yyyy")
        End If
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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