如何复制唯一的更改值并粘贴到另一个工作表VBA中

我对VBA相当陌生,正试图查看是否可以为某个进程创build代码。 我有一个电子表格,在第一行(公司A,公司B等)有一些唯一的标识符和公司名称。 在下面的列中,还有其他一些列,如位置,关键联系人等,对应于每个公司。 最后,有一个“评论”栏。 这些评论得到定期更新。

我想要做的是创build一个macros,这将允许我find独特公司的评论,复制(或剪切)它,并将其粘贴到同一工作簿中的“历史评论”表中,以便我可以维护logging过去的评论。 有没有办法创build一个macros来做到这一点? 我创build了一些东西,如果我把确切的单元名称,它将复制该评论和粘贴,但我想看看我是否可以指定一个单元格,我可以input公司名称,macros观将然后复制相应的注释,粘贴到底层,然后清除单元格,以便input新的注释。 我不知道这是否是远程可能的,但任何帮助将不胜感激!

Sub Range_copy() Dim cellwant As Variant Dim cellhistory As Variant Dim LRow As Variant Dim Account As Variant Worksheets("AAG").Select Worksheets("AAG").Range("I3").Select cellwant = Selection.Value FindString = Sheets("AAG").Range("B5:B65").Value cellwant = Selection.Value Worksheets("AAG").Range(cellwant).copy Worksheets("Sheet2").Activate Worksheets("Sheet2").Range("A1").Select 

这个问题对于公司名称的“相应评论”会有些模糊。 然而,我认为你正在寻找的可以使用Worksheet_Change事件,它会自动触发一个给定的工作表进行更改。

 Private Sub Worksheet_Change(ByVal Target As Range) Const csCommentCol As Integer = 5 'The column that contains the comments Const csTarget As String = "Sheet2" 'The worksheet with the record of comments Const csTargetCol As String = "A" 'The column on the sheet with the list Dim shtTarget As Worksheet Dim lngLast As Long Dim strOldComment As String Dim strNewComment As String Application.EnableEvents = False 'Prevent this procedure from triggering repeatedly If Target.Column = csCommentCol Then 'Check if it's the comment column that's being changed Set shtTarget = Sheets(csTarget) 'Define our target sheet. Only here for clarity later lngLast = shtTarget.Range(csTargetCol & Rows.Count).End(xlUp).Row + 1 'Find the first empty row strNewComment = Target.Value 'Copy the newly entered comment into a variable for safekeeping Application.Undo 'Undo the change to return to the old value strOldComment = Target.Value 'Copy the old value into a string for future use Target.Value = strNewComment 'Restore the new comment shtTarget.Range(csTargetCol & lngLast).Value = Target.Value 'Copy the value over Target.Select End If Application.EnableEvents = True End Sub 

将这些代码放在工作表中包含注释的Sheet对象(不是模块)中。 根据需要replace顶部的常量。 每次更改都会自动运行; 它会检查是否在我们指定的列中进行了更改(如果您愿意,可以是指定的单元格); 如果有变化,它会在我们的logging表中​​find第一个空单元格,并在那里复制单元格中的值; 然后清除目标单元格以重新input新的条目。