![](/img/trans.png)
[英]VBA - How to Copy and Paste the last value in a column to another Sheet
[英]How to copy unique, changing value and paste in another sheet VBA
我對VBA很陌生,我正試圖看看我是否可以為某個進程創建代碼。 我有一個電子表格,在第一行(公司A,公司B等)中有一些唯一標識符和公司名稱。 在以下列中,還有一些其他列,例如位置,密鑰聯系人等,它們對應於每個公司。 最后,有一個“評論”欄目。 這些評論會定期更新。
我要做的是創建一個宏,允許我找到唯一公司的評論,復制(或剪切)它並將其粘貼到同一工作簿中的“歷史評論”表中,以便我可以維護過去評論的記錄。 有沒有辦法創建一個宏來做到這一點? 我已經創建了一些東西,如果我輸入確切的Cell Name,它會復制該注釋並粘貼它但我想看看我是否可以指定一個單元格,我可以輸入公司名稱,宏將查看該單元格中的內容然后復制相應的注釋,將其粘貼到背板中,然后清除單元格以便我可以輸入新的注釋。 我不知道這是否可以遠程實現,但任何幫助將不勝感激!
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對象(而不是模塊)中。 根據需要替換頂部的常量。 每次更改時它都會自動運行; 它檢查是否在我們指定的列中進行了更改(如果您願意,可以是指定的單元格); 如果有變化,它會在我們的記錄表中找到第一個空單元格並復制那里單元格中的值; 然后它清除目標單元格以獲取新條目並重新選擇它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.