Changing Date

Nestes2000

New Member
Joined
Oct 23, 2018
Messages
1
Hi Guys,

I'm new with macros/VBA...

How can I create a button that ask me a date and then change the dates on a column with the date I inputed?

Many Thanks in Advance
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Welcome to the message board!

Here's a little code you can adjust to suit your needs:
Code:
Sub ChangeDates()

Dim MyText As String
Dim MyDate As Date
Dim Rng As Range


'VBA doesn't crash but goes to error handler if InputBox returns a value that can't be changed to a date
On Error GoTo ErrorHandler


'Adjust the Rng to match your needs
Set Rng = Range("B2:B" & Cells(Rows.Count, 2).End(xlUp).Row)


'InputBox returns a string:
MyText = Application.InputBox("Enter a date")


'CDate turns strings to dates:
MyDate = CDate(MyText)


With Rng
    .Value = MyDate
End With


'If a date was entered the macro stops here:
Exit Sub


'otherwise the code jumps here:
ErrorHandler:
    MsgBox "Try again with a suitable date.", vbOKOnly, MyText & " ?"


End Sub

You can either insert a button from the Form Controls or use any shape and assign the macro to that.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,388
Messages
6,119,226
Members
448,878
Latest member
Da9l87

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