+ Reply to Thread
Results 1 to 8 of 8

Using Date in InputBox for criteria in code

  1. #1
    SMac
    Guest

    Using Date in InputBox for criteria in code

    I have the following code:

    Sub ShadeCells()

    Dim dblSum As Double
    Dim cell As Range
    On Error GoTo Errhandler:
    dblSum = 0
    With ActiveSheet.Range("A:A")
    For Each cell In .Range("D:D")
    If cell.Text <> "" Then
    If cell.Value <= DateValue("2/08/2005") Then
    cell.Offset(0, 1).Interior.ColorIndex = 16
    If IsNumeric(cell.Offset(0, 1)) Then
    dblSum = dblSum + cell.Offset(0, 1).Value
    End If
    Else
    cell.Offset(0, 1).Interior.ColorIndex = xlNone
    End If
    Else
    cell.Offset(0, 1).Interior.ColorIndex = xlNone
    End If
    Next
    Application.EnableEvents = False

    .Range("G1").Value = dblSum
    .Font.Bold = True
    .NumberFormat = "$#,##0.00"

    Application.EnableEvents = True
    End With
    Errhandler:
    Application.EnableEvents = True
    End Sub

    Works great, now instead of going in and changing the date every 2 weeks
    (this is how often I use this code) I would like an InputBox come up and ask
    for the date to use and then use that entry in the criteria section:

    If cell.Value <= DateValue("2/08/2005") Then

    Any suggestions would be great, Thanks!

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Why bother with an input box?

    You could use the Date statement in VBA to work out the criteria.

  3. #3
    Chip Pearson
    Guest

    Re: Using Date in InputBox for criteria in code

    Try something like

    Dim S As String
    S = InputBox("Enter A Date")
    If S = "" Then
    Exit Sub
    End If
    ' your code
    If cell.Value <= DateValue(S) Then
    ' your code


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com




    "SMac" <[email protected]> wrote in message
    news:[email protected]...
    >I have the following code:
    >
    > Sub ShadeCells()
    >
    > Dim dblSum As Double
    > Dim cell As Range
    > On Error GoTo Errhandler:
    > dblSum = 0
    > With ActiveSheet.Range("A:A")
    > For Each cell In .Range("D:D")
    > If cell.Text <> "" Then
    > If cell.Value <= DateValue("2/08/2005") Then
    > cell.Offset(0, 1).Interior.ColorIndex = 16
    > If IsNumeric(cell.Offset(0, 1)) Then
    > dblSum = dblSum + cell.Offset(0, 1).Value
    > End If
    > Else
    > cell.Offset(0, 1).Interior.ColorIndex = xlNone
    > End If
    > Else
    > cell.Offset(0, 1).Interior.ColorIndex = xlNone
    > End If
    > Next
    > Application.EnableEvents = False
    >
    > .Range("G1").Value = dblSum
    > .Font.Bold = True
    > .NumberFormat = "$#,##0.00"
    >
    > Application.EnableEvents = True
    > End With
    > Errhandler:
    > Application.EnableEvents = True
    > End Sub
    >
    > Works great, now instead of going in and changing the date
    > every 2 weeks
    > (this is how often I use this code) I would like an InputBox
    > come up and ask
    > for the date to use and then use that entry in the criteria
    > section:
    >
    > If cell.Value <= DateValue("2/08/2005") Then
    >
    > Any suggestions would be great, Thanks!




  4. #4
    Gary Brown
    Guest

    RE: Using Date in InputBox for criteria in code

    Sub ShadeCells()
    Dim dblSum As Double
    Dim cell As Range
    Dim strAnswer As String '<==== added

    On Error GoTo Errhandler:
    strAnswer = _
    Application.InputBox(Prompt:="Enter Date: ", _
    Title:="Use format mm/dd/yyyy...", _
    Default:=today(), Type:=2) '<==== added

    dblSum = 0
    With ActiveSheet.Range("A:A")
    For Each cell In .Range("D:D")
    If cell.Text < "" Then
    If cell.Value <= DateValue(strAnswer) Then '<==== changed

    HTH,
    Gary Brown


    "SMac" wrote:

    > I have the following code:
    >
    > Sub ShadeCells()
    >
    > Dim dblSum As Double
    > Dim cell As Range
    > On Error GoTo Errhandler:
    > dblSum = 0
    > With ActiveSheet.Range("A:A")
    > For Each cell In .Range("D:D")
    > If cell.Text <> "" Then
    > If cell.Value <= DateValue("2/08/2005") Then
    > cell.Offset(0, 1).Interior.ColorIndex = 16
    > If IsNumeric(cell.Offset(0, 1)) Then
    > dblSum = dblSum + cell.Offset(0, 1).Value
    > End If
    > Else
    > cell.Offset(0, 1).Interior.ColorIndex = xlNone
    > End If
    > Else
    > cell.Offset(0, 1).Interior.ColorIndex = xlNone
    > End If
    > Next
    > Application.EnableEvents = False
    >
    > .Range("G1").Value = dblSum
    > .Font.Bold = True
    > .NumberFormat = "$#,##0.00"
    >
    > Application.EnableEvents = True
    > End With
    > Errhandler:
    > Application.EnableEvents = True
    > End Sub
    >
    > Works great, now instead of going in and changing the date every 2 weeks
    > (this is how often I use this code) I would like an InputBox come up and ask
    > for the date to use and then use that entry in the criteria section:
    >
    > If cell.Value <= DateValue("2/08/2005") Then
    >
    > Any suggestions would be great, Thanks!


  5. #5
    Gary Brown
    Guest

    RE: Using Date in InputBox for criteria in code

    oops...
    s/b
    strAnswer = _
    Application.InputBox(Prompt:="Enter Date: ", _
    Title:="Use format mm/dd/yyyy...", _
    Default:=Format(Now, "mm/dd/yyyy"), Type:=2)

    Today doesn't work in vba, changed to Format(Now, "mm/dd/yyyy")

    "Gary Brown" wrote:

    > Sub ShadeCells()
    > Dim dblSum As Double
    > Dim cell As Range
    > Dim strAnswer As String '<==== added
    >
    > On Error GoTo Errhandler:
    > strAnswer = _
    > Application.InputBox(Prompt:="Enter Date: ", _
    > Title:="Use format mm/dd/yyyy...", _
    > Default:=today(), Type:=2) '<==== added
    >
    > dblSum = 0
    > With ActiveSheet.Range("A:A")
    > For Each cell In .Range("D:D")
    > If cell.Text < "" Then
    > If cell.Value <= DateValue(strAnswer) Then '<==== changed
    >
    > HTH,
    > Gary Brown
    >
    >
    > "SMac" wrote:
    >
    > > I have the following code:
    > >
    > > Sub ShadeCells()
    > >
    > > Dim dblSum As Double
    > > Dim cell As Range
    > > On Error GoTo Errhandler:
    > > dblSum = 0
    > > With ActiveSheet.Range("A:A")
    > > For Each cell In .Range("D:D")
    > > If cell.Text <> "" Then
    > > If cell.Value <= DateValue("2/08/2005") Then
    > > cell.Offset(0, 1).Interior.ColorIndex = 16
    > > If IsNumeric(cell.Offset(0, 1)) Then
    > > dblSum = dblSum + cell.Offset(0, 1).Value
    > > End If
    > > Else
    > > cell.Offset(0, 1).Interior.ColorIndex = xlNone
    > > End If
    > > Else
    > > cell.Offset(0, 1).Interior.ColorIndex = xlNone
    > > End If
    > > Next
    > > Application.EnableEvents = False
    > >
    > > .Range("G1").Value = dblSum
    > > .Font.Bold = True
    > > .NumberFormat = "$#,##0.00"
    > >
    > > Application.EnableEvents = True
    > > End With
    > > Errhandler:
    > > Application.EnableEvents = True
    > > End Sub
    > >
    > > Works great, now instead of going in and changing the date every 2 weeks
    > > (this is how often I use this code) I would like an InputBox come up and ask
    > > for the date to use and then use that entry in the criteria section:
    > >
    > > If cell.Value <= DateValue("2/08/2005") Then
    > >
    > > Any suggestions would be great, Thanks!


  6. #6
    Bob Phillips
    Guest

    Re: Using Date in InputBox for criteria in code

    Date does though.

    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "Gary Brown" <[email protected]> wrote in message
    news:[email protected]...
    > oops...
    > s/b
    > strAnswer = _
    > Application.InputBox(Prompt:="Enter Date: ", _
    > Title:="Use format mm/dd/yyyy...", _
    > Default:=Format(Now, "mm/dd/yyyy"), Type:=2)
    >
    > Today doesn't work in vba, changed to Format(Now, "mm/dd/yyyy")
    >
    > "Gary Brown" wrote:
    >
    > > Sub ShadeCells()
    > > Dim dblSum As Double
    > > Dim cell As Range
    > > Dim strAnswer As String '<==== added
    > >
    > > On Error GoTo Errhandler:
    > > strAnswer = _
    > > Application.InputBox(Prompt:="Enter Date: ", _
    > > Title:="Use format mm/dd/yyyy...", _
    > > Default:=today(), Type:=2) '<==== added
    > >
    > > dblSum = 0
    > > With ActiveSheet.Range("A:A")
    > > For Each cell In .Range("D:D")
    > > If cell.Text < "" Then
    > > If cell.Value <= DateValue(strAnswer) Then '<==== changed
    > >
    > > HTH,
    > > Gary Brown
    > >
    > >
    > > "SMac" wrote:
    > >
    > > > I have the following code:
    > > >
    > > > Sub ShadeCells()
    > > >
    > > > Dim dblSum As Double
    > > > Dim cell As Range
    > > > On Error GoTo Errhandler:
    > > > dblSum = 0
    > > > With ActiveSheet.Range("A:A")
    > > > For Each cell In .Range("D:D")
    > > > If cell.Text <> "" Then
    > > > If cell.Value <= DateValue("2/08/2005") Then
    > > > cell.Offset(0, 1).Interior.ColorIndex = 16
    > > > If IsNumeric(cell.Offset(0, 1)) Then
    > > > dblSum = dblSum + cell.Offset(0, 1).Value
    > > > End If
    > > > Else
    > > > cell.Offset(0, 1).Interior.ColorIndex = xlNone
    > > > End If
    > > > Else
    > > > cell.Offset(0, 1).Interior.ColorIndex = xlNone
    > > > End If
    > > > Next
    > > > Application.EnableEvents = False
    > > >
    > > > .Range("G1").Value = dblSum
    > > > .Font.Bold = True
    > > > .NumberFormat = "$#,##0.00"
    > > >
    > > > Application.EnableEvents = True
    > > > End With
    > > > Errhandler:
    > > > Application.EnableEvents = True
    > > > End Sub
    > > >
    > > > Works great, now instead of going in and changing the date every 2

    weeks
    > > > (this is how often I use this code) I would like an InputBox come up

    and ask
    > > > for the date to use and then use that entry in the criteria section:
    > > >
    > > > If cell.Value <= DateValue("2/08/2005") Then
    > > >
    > > > Any suggestions would be great, Thanks!




  7. #7
    Gary Brown
    Guest

    Re: Using Date in InputBox for criteria in code

    Ohhhhhhhhhh, be quite ;O>

    Why go with something nice and simple like that when you can get real
    complicated? :O>

    Thanks for the note.
    Gary


    "Bob Phillips" wrote:

    > Date does though.
    >
    > --
    >
    > HTH
    >
    > RP
    > (remove nothere from the email address if mailing direct)
    >
    >
    > "Gary Brown" <[email protected]> wrote in message
    > news:[email protected]...
    > > oops...
    > > s/b
    > > strAnswer = _
    > > Application.InputBox(Prompt:="Enter Date: ", _
    > > Title:="Use format mm/dd/yyyy...", _
    > > Default:=Format(Now, "mm/dd/yyyy"), Type:=2)
    > >
    > > Today doesn't work in vba, changed to Format(Now, "mm/dd/yyyy")
    > >
    > > "Gary Brown" wrote:
    > >
    > > > Sub ShadeCells()
    > > > Dim dblSum As Double
    > > > Dim cell As Range
    > > > Dim strAnswer As String '<==== added
    > > >
    > > > On Error GoTo Errhandler:
    > > > strAnswer = _
    > > > Application.InputBox(Prompt:="Enter Date: ", _
    > > > Title:="Use format mm/dd/yyyy...", _
    > > > Default:=today(), Type:=2) '<==== added
    > > >
    > > > dblSum = 0
    > > > With ActiveSheet.Range("A:A")
    > > > For Each cell In .Range("D:D")
    > > > If cell.Text < "" Then
    > > > If cell.Value <= DateValue(strAnswer) Then '<==== changed
    > > >
    > > > HTH,
    > > > Gary Brown
    > > >
    > > >
    > > > "SMac" wrote:
    > > >
    > > > > I have the following code:
    > > > >
    > > > > Sub ShadeCells()
    > > > >
    > > > > Dim dblSum As Double
    > > > > Dim cell As Range
    > > > > On Error GoTo Errhandler:
    > > > > dblSum = 0
    > > > > With ActiveSheet.Range("A:A")
    > > > > For Each cell In .Range("D:D")
    > > > > If cell.Text <> "" Then
    > > > > If cell.Value <= DateValue("2/08/2005") Then
    > > > > cell.Offset(0, 1).Interior.ColorIndex = 16
    > > > > If IsNumeric(cell.Offset(0, 1)) Then
    > > > > dblSum = dblSum + cell.Offset(0, 1).Value
    > > > > End If
    > > > > Else
    > > > > cell.Offset(0, 1).Interior.ColorIndex = xlNone
    > > > > End If
    > > > > Else
    > > > > cell.Offset(0, 1).Interior.ColorIndex = xlNone
    > > > > End If
    > > > > Next
    > > > > Application.EnableEvents = False
    > > > >
    > > > > .Range("G1").Value = dblSum
    > > > > .Font.Bold = True
    > > > > .NumberFormat = "$#,##0.00"
    > > > >
    > > > > Application.EnableEvents = True
    > > > > End With
    > > > > Errhandler:
    > > > > Application.EnableEvents = True
    > > > > End Sub
    > > > >
    > > > > Works great, now instead of going in and changing the date every 2

    > weeks
    > > > > (this is how often I use this code) I would like an InputBox come up

    > and ask
    > > > > for the date to use and then use that entry in the criteria section:
    > > > >
    > > > > If cell.Value <= DateValue("2/08/2005") Then
    > > > >
    > > > > Any suggestions would be great, Thanks!

    >
    >
    >


  8. #8
    Bob Phillips
    Guest

    Re: Using Date in InputBox for criteria in code

    LOL!

    "Gary Brown" <[email protected]> wrote in message
    news:[email protected]...
    > Ohhhhhhhhhh, be quite ;O>
    >
    > Why go with something nice and simple like that when you can get real
    > complicated? :O>
    >
    > Thanks for the note.
    > Gary
    >
    >
    > "Bob Phillips" wrote:
    >
    > > Date does though.
    > >




+ 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