0

I have recorded the following VBA macro in Excel

Sub EnterDate()
'
' EnterDate Macro

' Enter date at any point in a worksheet and move cursor down

'

' Keyboard Shortcut: Ctrl+x

'
    ActiveCell.FormulaR1C1 = "12/15/2014"

    ActiveCell.Offset(1, 0).Range("A1").Select

End Sub

What I need to be able to do is to be able to insert different years, months, and days, and loop back to repeat the process in the cells below. The above code only inserts the defined date. I would like to enter month, hit [ENTER], enter day, hit [ENTER], enter year hit [ENTER], display the date and move down to the next cell, repeating the loop until stopping execution.

2
  • ActiveCell.Offset(1, 0).Range("A1").Select in nonsensical. Starting from the active cell, you move down one row and then select cell A1? Did you mean Range("A1").Offset(1,0) instead? Commented Jul 29, 2014 at 20:25
  • Why can't you just type the dates you want into Excel? Is there a sequence here or anything repeating? Commented Jul 29, 2014 at 20:27

1 Answer 1

1

Out of curiosity, why would you use Ctrl+x (built in hotkey for "Cut" command) to hotkey this macro procedure?

Simple Do ... Loop with a messageBox to prompt you to continue (or quit) and InputBox to capture the values you want to put on the sheet.

Sub EnterDate()
'
' EnterDate Macro

' Enter date at any point in a worksheet and move cursor down

'

' Keyboard Shortcut: Ctrl+x

Dim mb as VbMsgBoxResult
Dim y as String, m as String, d as String

Do 

    y = Application.InputBox("Year?")
    m = Application.InputBox("Month?")
    d = Application.InputBox("Day?")

    ActiveCell.Value = m & "/" & d & "/" & y
    ActiveCell.Offset(1, 0).Select

    mb = MsgBox("Continue?", vbYesNo)
Loop While Not mb = vbNo

End Sub
Sign up to request clarification or add additional context in comments.

3 Comments

Many thanks, David. I am just trying to learn VB, weaning myself away from Lotus123 which I love only because of its simplicity to write macros. This is my first simple exercise. In Lotus123, a macro named \x with the code "@date({?},{?},{?})~{down}{branch \x}" does the job perfectly. In Excel, using VB, it ain't so simple. Your code works beautifully, except that selecting "No" for "Continue?" does not stop execution. Not a big deal, but I love your approach. Thanks for the help.
Try now, I forgot to set the mb variable to the message box result :)
Works smoother than a baby's bottom! I'm now really getting interested in learning more VB - I used to program in Basic many, many years ago. I greatly appreciate help from folks like you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.