Changing Date in Input Box

helpexcel

Well-known Member
Joined
Oct 21, 2009
Messages
656
I'm using the code below and today's date is pre-populated in the Input box. Is there a way to make it today() - 7 days?

thanks!


Code:
[I]Sub DeleteFromDate()
[/I]Application.ScreenUpdating = FalseApplication.DisplayAlerts = False
Dim LR As Long
LR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
DateR = Application.InputBox("Enter based on date to delete", TitleMsg, FormatDateTime(Date, vbShortDate), Type:=1)
Cells.AutoFilter Field:=2, Criteria1:="<=" & DateR
ALR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
If ALR > 2 Then
    Range("A2:A" & LR).SpecialCells(xlCellTypeVisible).Select
    Range("A2:A" & LR).Delete
    Range("A1").Activate
End If
Cells.AutoFilter
MsgBox "Finished deleting rows"
Application.DisplayAlerts = True
Application.ScreenUpdating = True [I]End Sub[/I]
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try:

Code:
DateR = Application.InputBox("Enter based on date to delete", TitleMsg, FormatDateTime([COLOR=#ff0000]Date - 7[/COLOR], vbShortDate), Type:=1)
 
Upvote 0
Any idea on how to make the cancel button work on the inputbox?

Hi,
When you select the Cancel button using InputBox method, it returns False.

Rich (BB code):
DateR = Application.InputBox("Enter based on date to delete", TitleMsg, FormatDateTime(Date, vbShortDate), Type:=1)
'cancel pressed
If VarType(DateR) = vbBoolean Then Exit Sub
Assuming your variable is declared as variant data type then
Add the line shown in red & see if does what you want

Dave
 
Last edited:
Upvote 0
works great thanks!

Any idea on how to make the cancel button work on the inputbox?


Try this

Code:
Sub DeleteFromDate()
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  Dim LR As Long
  [COLOR=#0000ff]Dim DateR As Variant[/COLOR]
  Dim TitleMsg As String, ALR As Long
  LR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
  DateR = Application.InputBox("Enter based on date to delete", TitleMsg, FormatDateTime(Date, vbShortDate), Type:=1)
[COLOR=#0000ff]  If DateR = "" Or DateR = False Then[/COLOR]
[COLOR=#0000ff]    MsgBox "Operation cancelled"[/COLOR]
[COLOR=#0000ff]    Exit Sub[/COLOR]
[COLOR=#0000ff]  End If[/COLOR]
  Cells.AutoFilter Field:=2, Criteria1:="<=" & DateR
  ALR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
  If ALR > 2 Then
      Range("A2:A" & LR).SpecialCells(xlCellTypeVisible).Select
      Range("A2:A" & LR).Delete
      Range("A1").Activate
  End If
  Cells.AutoFilter
  MsgBox "Finished deleting rows"
  Application.DisplayAlerts = True
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top