简体   繁体   English

Excel宏:如果单击了单元格A,则复制单元格B并粘贴为值,然后将位置返回到单元格A

[英]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.

相关问题 Excel宏,要基于另一个单元格值复制和粘贴单元格值? - Excel macro, to copy and paste a cell value based on another cell value? VB Excel无法复制和粘贴拉单元格位置的宏 - VB Excel can't Copy and Paste Macro that Pulls the Cell Position 编写一个 Excel 宏来查找和赋值,复制该值并粘贴到单元格中 - Write an Excel macro to find and value, copy that value and paste into a Cell 基于单元格值的Excel宏复制范围粘贴偏移量 - Excel Macro Copy Range Paste offset based on cell value Excel宏,根据另一个单元格值复制和粘贴多个单元格? - Excel macro, to copy and paste a multiple cells based on another cell value? Excel VBA-如果单元格A等于X,则自动执行,然后复制并粘贴到单元格B中 - Excel VBA - automatic if Cell A equals X then copy and paste in Cell B 宏以基于单元格值复制范围和粘贴 - Macro to Copy Range and Paste Based on Cell Value Excel VBA-将值定义为单元格位置,复制到同一单元格位置下方,粘贴 - Excel VBA - Define value as cell position, copy below same cell position, paste 如何从Excel单元格复制值并将其按比例粘贴到新单元格中 - How to copy a value from an Excel cell and paste it into a new cell at scale Excel宏,如果单元格具有值,则将单元格复制到某个位置 - Excel Macro, Copy Cell to a location if a cell has a value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM