粘贴数据时自动运行宏VBA [英] Automatically Run Macro When Data Is Pasted VBA

查看:247
本文介绍了粘贴数据时自动运行宏VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作表,其中在前三列(A,B,C)中存储数据.这些值在宏中使用.

I have a worksheet where in the first three columns (A,B,C) I store data. These values are used in a macro.

我想知道在将数据粘贴到这些列之后如何使此宏自动运行.我几乎可以确定我将使用Worksheet-Change模块,但是对于代码我一无所知.

I would like to know how it is possible to make this macro run automatically after data is pasted onto these columns. I am almost sure that I will use the Worksheet-Change module, but as for the code I am clueless.

谢谢.

推荐答案

一个简单的实现:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:C" & ThisWorkbook.Worksheets(1).UsedRange.Rows.Count))      Is Nothing Then
    'Call your Macro to do stuff
    End If
End Sub

相交检查目标是否在您要监视的范围内.因此,如果C列中的某些内容发生了变化,则Intersect将返回Nothing,并且不会调用您的宏.请记住,Worksheet_Change事件会在发生任何更改时触发,甚至双击单元格也是如此.如果您在此工作表中所做的只是复制并粘贴数据,然后运行宏,那应该没问题,但是如果您要进一步处理数据,则可能必须考虑更复杂的解决方案.示例包括镜像您的工作表并在工作表更改事件之前/之后对其进行比较.您可以在此处了解更多信息:检测是否通过编辑实际更改了单元格值

Intersect checks if Target is in the range you want to monitor. So if something changes in columns past C, Intersect will return Nothing and your macro won't be called. Just keep in mind that the Worksheet_Change event fires on any change, even double clicking into the cells. If all you are doing in this Worksheet is copy&pasting data and then running your Macro, this should be fine, but if you are manipulating your data further, you might have to look at more sophisticated solutions. Examples include mirroring your Worksheet and comparing it pre/post Worksheet Changed Event. You can read more on this here: Detect whether cell value was actually changed by editing

这篇关于粘贴数据时自动运行宏VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆