简体   繁体   English

在单元格区域中单击任何单元格时,将其复制并粘贴为值

[英]Copy and Paste as Value when any cell is clicked within a range of cells

I have a set of VBA codes that copy cell "AC14" in Sheet2 and paste as value to cell "Q4" in Sheet6. 我有一组VBA代码,可复制Sheet2中的单元格“ AC14”并将其值粘贴到Sheet6中的单元格“ Q4”。 I have another set of code to apply a filter in Sheet6 when the value of cell "Q4" in Sheet6 changed, which I am happy about. 我有另一组代码可以在Sheet6中更改单元格“ Q4”的值时在Sheet6中应用过滤器,对此我感到很高兴。 The codes though not perfect it works to an extent. 这些代码虽然不完善,但在一定程度上可以奏效。 Below are my codes for the copy and paste part. 以下是我用于复制和粘贴部分的代码。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Not Intersect(Range("AC14"), Target) Is Nothing Then
Range("AC14").Select
Selection.Copy
Sheet6.Activate
Sheet6.Range("Q4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
End If

End Sub 结束子

What I would like to improve upon are the following: 我想在以下方面进行改进:

1) Change the Range("AC14") to a dynamic cell within a dynamic range in column "AC" starting from row 11 to a row number that changes from time to time. 1)将Range(“ AC14”)更改为列“ AC”中动态范围内的动态单元格,该动态范围从第11行开始到不时更改的行号。 So that when I (or the user) click on any cell within this range the code will run correctly. 这样,当我(或用户)单击此范围内的任何单元格时,代码将正确运行。

Notes: Sheet2 is a worksheet registering changes made to a project I am working on. 注意:Sheet2是一个工作表,用于记录对我正在处理的项目所做的更改。 Each row represents one change of my project and as time goes by the number of changes (rows) occurs could get up to 1500. Rows 1 to 10 of Sheet2 are allocated for column labels 每行代表我的项目的一个更改,随着时间的流逝,更改(行)的数量可能达到1500。Sheet2的第1到10行分配给列标签

2) When my cursor is on cell "AC14" in Sheet2, When I click on it again it doesn't take me to Sheet6. 2)当我的光标位于Sheet2的“ AC14”单元格上时,再次单击它不会带我进入Sheet6。 I understand this as it is not a change (in vba speakas the cursur is already there) in the worksheet. 我了解这是因为它不是工作表中的更改(在vba中,因为cursur已经存在)。 However, if I click on other cell and go back to click of cell "AC14" again, the codes will work. 但是,如果我单击其他单元格,然后再次单击单元格“ AC14”,则代码将起作用。 Is there a way around this? 有没有解决的办法?

I would grately appreciate it if anyone can shed me some light on how to solve the above issues. 如果有人能向我阐明如何解决上述问题,我将不胜感激。 Thanks in advance. 提前致谢。

Sean 肖恩

Re item 2: It looks like you really need to define your requirements. 关于项目2:看起来您确实需要定义需求。 Currently, you are using a Worksheet_SelectionChange event. 当前,您正在使用Worksheet_SelectionChange事件。 By its very nature, the code will run when the selection is changed, either with the mouse or the keyboard. 就其本质而言,当使用鼠标或键盘更改选择时,代码将运行。 The code will NOT run if a cell that is already selected cell is clicked again with the mouse. 如果用鼠标再次单击已选择的单元格,则该代码将不会运行。 Because that does not change the selection. 因为那不会改变选择。

There are other events that you might want to employ, but none of them will work with just a single click on an already selected cell, so, in the truest sense of your question: no, there is not a workaround for that. 您可能还希望使用其他事件,但是单击已选择的单元格就无法使它们起作用,因此,从您的问题的最真实的意义上讲:不,没有解决方法。

This might work for you: 这可能对您有用:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Target.Cells.CountLarge > 1 Then Exit Sub

    If Target.Row >= 14 And Target.Column = 29 and Target.Value <> "" Then

        Sheet6.Range("Q4").Value = Target.Value
        Target.Offset(0, -1).Select  'select the cell to the left
        Sheet6.Activate

    End If

End Sub

暂无
暂无

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

相关问题 当第一个单元格上的值发生变化时,Excel VBA 代码复制一行中的一系列单元格并粘贴到不同的工作表但相同的行/范围 - Excel VBA code to copy a range of cells in a row and paste in a different sheet but same row/range, when the value on 1st cell changes 将单元格值复制到一系列单元格 - Copy cell value to a range of cells 在单元格范围内复制粘贴值 - Copy paste values within cell range 如果某个范围中的单元格的值为“ x”,则将某个范围的单元格复制并粘贴到另一张工作表中 - If cell in a range has value “x” copy and paste a range of cells into a different sheet 复制一个单元格值并将其粘贴到多个单元格 - copy and paste one cell value to multiple cells 单击时的宏按钮将复制单元格范围并粘贴到另一个工作表的下一个空白行 - Macro Button that when clicked will copy range of cells and paste to next blank row of another worksheet 从范围内的单元格复制值并将它们粘贴到范围内的随机单元格中 - Copy values from cells in range and paste them in random cell in range Excel VBA如何将WorkSheet名称与单元格值匹配,然后复制并粘贴一系列单元格 - Excel VBA How to match WorkSheet name with Cell value, then Copy and Paste a Range of Cells 宏以基于单元格值复制范围和粘贴 - Macro to Copy Range and Paste Based on Cell Value 根据单元格值复制/粘贴列范围 - Copy/Paste column range based on cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM