简体   繁体   English

Excel VBA代码自动执行

[英]Excel VBA code to automate

I have recorded the following VBA macro in Excel 我已经在Excel中记录了以下VBA宏

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. 我想输入月份,按[ENTER],输入日期,按[ENTER],输入年份,按[ENTER],显示日期并向下移动到下一个单元格,重复循环直到停止执行。

Out of curiosity, why would you use Ctrl+x (built in hotkey for "Cut" command) to hotkey this macro procedure? 出于好奇,您为什么要使用Ctrl + x(“剪切”命令的内置热键)热键此宏过程?

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. 简单Do ... Loop使用messageBox提示您继续(或退出),并使用InputBox捕捉要放置在工作表上的值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM