简体   繁体   English

工作簿上的输入框打开

[英]Input Box on workbook open

I am trying to come up with some vba code to open an input box automatically as soon as the workbook is opened and have the user enter a date and then have the date placed in the A1 cell. 我试图提出一些vba代码,一旦打开工作簿并自动打开输入框并让用户输入日期,然后将日期放在A1单元格中。 I have written the code below but the input box is not pulling up at all it just opens the workbook and moves on.. not sure what is happening. 我已经编写了下面的代码,但输入框根本没有拉起它只是打开工作簿并继续...不确定发生了什么。 Any and all help is appreciated. 任何和所有的帮助表示赞赏。

Thanks! 谢谢!

Option Explicit
Private Sub workbook_open()
Dim cellvalue As Variant
Dim ws As Worksheet

Set ws = Worksheets("Workbench Report")

ReShowInputBox: cellvalue = Application.InputBox("Please Enter Todays Date (dd/mm/yyyy)")

If cellvalue = False Then Exit Sub
If IsDate(cellvalue) And CDate(cellvalue) < Date Then
ws.Range("A1").Value = DateValue(cellvalue)
Else: MsgBox ("Invalid Date!")
GoTo ReShowInputBox
End If

End Sub

Your code triggers upon the Workbook opening for me. 您的代码会在我打开工作簿时触发。 Try these steps. 尝试这些步骤。

  1. Open up Excel and Save As, changing the extension to .XSLM 打开Excel并另存为,将扩展名更改为.XSLM
  2. Open up the VBA Editor (ALT + F11) 打开VBA编辑器(ALT + F11)
  3. In the left-hand window, locate your macro file (the one you just created and named - it's in brackets after "VBA Project"), drilldown to "This Workbook" and double-click it. 在左侧窗口中,找到您的宏文件(您刚创建并命名的文件 - 在“VBA项目”之后的括号中),向下钻取到“此工作簿”并双击它。
  4. Paste your code into the right-hand window 将代码粘贴到右侧窗口中
  5. Save the file and re-open. 保存文件并重新打开。

See attached diagram. 见附图。

By the way, "cellValue = false" should probably be cellValue = "" since InputBox is returning a string and not a boolean value. 顺便说一句,“cellValue = false”应该是cellValue =“”,因为InputBox返回一个字符串而不是一个布尔值。

在此输入图像描述

For Workbook_Open events the script needs to reside in the private module (ThisWorkbook) 对于Workbook_Open事件,脚本需要驻留在私有模块中(ThisWorkbook)

From Ozgrid : 来自奥兹格里德

the Workbook_Open event is a procedure of the Workbook Object and as such, the Workbook_Open procedure MUST reside in the private module of the Workbook Object (ThisWorkbook). Workbook_Open事件是工作簿对象的过程,因此,Workbook_Open过程必须驻留在工作簿对象(ThisWorkbook)的私有模块中。

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

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