[英]Excel Macro: If Cell A is clicked, copy Cell B and paste as value, then return position to Cell A
I am creating a form template in Excel that has Cell B = TODAY() so, when a user opens the template to fill out the form, today's date is displayed. 我正在Excel中创建具有单元格B = TODAY()的表单模板,因此,当用户打开模板以填写表单时,将显示今天的日期。
However, once the user fills out the form, I would like Cell B to equal the value of TODAY(), so the cell does not update every time the Workbook is opened. 但是,一旦用户填写了表格,我希望单元格B等于TODAY()的值,因此该单元格不会在每次打开工作簿时更新。
My solution to this was to create a Macro that does the following: 我对此的解决方案是创建一个执行以下操作的宏:
When the user clicks on Cell A, a cell that the user is required to fill in with text, Cell B, containing the formula =TODAY(), is copied and pasted as a value to the same position. 当用户单击单元格A时,将需要用户填写文本的单元格B包含公式= TODAY()的单元格被复制并粘贴为相同位置的值。 Then, Cell A is selected again so the user can fill in the required information.
然后,再次选择单元格A,以便用户可以填写所需的信息。
Below is my example VBA, which checks if Cell A is empty instead of being clicked on. 下面是我的示例VBA,它检查单元格A是否为空而不是未单击。 I would like to change this though, if someone could help with that.
如果有人可以帮助我,我想改变一下。
In this example, Cell A is E11 and Cell B is E13. 在此示例中,单元格A为E11,单元格B为E13。
Sub Date_Change(ByVal Target As Range)
Dim Rng As Range
Set Rng = Sheets("Form").Range("E13")
If Sheets("Form").Range("E11") = Empty Then
Sheets("Form").Range("E13") = TODAY()
Else
Rng.Copy
Rng.PasteSpecial xlPasteValues
Set Rng = Nothing
Application.CutCopyMode = False
Cells(11, 5).Select
End Sub
How's this (this goes in the worksheet you want to run it on's module): 怎么样(这在您要在模块上运行它的工作表中):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cellA As Range, cellB As Range
Set cellA = Sheets("Form").Range("E11")
Set cellB = Sheets("Form").Range("E13")
If Target.Row = cellA.Row And Target.Column = cellA.Column Then
Dim Rng As Range
Set Rng = cellA
If cellA = Empty Then
cellB.Value = WorksheetFunction.Text(Month(Now()) & "/" & Day(Now()) & "/" & Year(Now()), "MM/DD/YYYY")
Else
Rng.Copy
Rng.PasteSpecial xlPasteValues
Set Rng = Nothing
Application.CutCopyMode = False
Cells(11, 5).Select
End If
End If
End Sub
You may need to check my declarations, but that should get you going. 您可能需要检查我的声明,但这应该可以帮助您。 It'll check to see if cell E11 was selected, and if so, will run.
它将检查是否已选择单元格E11,如果已选择,它将运行。
Within the workbook object you have a built in event workbook_beforesave... you could put the code there. 在工作簿对象中,您有一个内置事件workbook_beforesave ...您可以在此处放置代码。
I recorded a macro which updates B3 to be a hard value, eliminating the formula. 我记录了一个宏,该宏将B3更新为硬值,从而消除了公式。
I then copied the code out of the macro and hit save. 然后,我将代码从宏中复制出来,然后单击保存。 You may need to update the "range" to account for specific worksheet if multiple sheets in workbook.
如果工作簿中有多个工作表,则可能需要更新“范围”以说明特定的工作表。
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Range("B3").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.