Worksheet Change事件未复制和粘贴数据。

huangapple go评论86阅读模式
英文:

Worksheet Change event not copying and pasting data

问题

以下是您的代码的翻译部分:

我的当前代码如下。

我有一个工作簿中的两个工作表。

1.) 名称为 - 数据和粘贴的工作表

2.) 数据工作表受保护。从单元格J3:O1000开始,列J到O未受保护。行1和2保留为标题。

3.) 当列N中有**YES**时,范围J:O会自动排序,下面的代码会帮助排序,而No将始终在YES之后,在末尾是空白单元格。

4.) 这个代码可以工作,但它没有将显示为列N中有YES的J:O行复制并粘贴为值。

我无法理解为什么它不起作用。TIA

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    Dim lastRow As Long
    Dim DataSheet As Worksheet
    Dim PastedSheet As Worksheet
    Dim sourceRange As Range
    Dim destinationRow As Long
    Dim isDataSheetUnprotected As Boolean
    
    Application.EnableEvents = False
    
    Set DataSheet = ThisWorkbook.Sheets("Data")
    Set PastedSheet = ThisWorkbook.Sheets("Pasted")
    
    Set KeyCells = Range("N3:N" & Cells(Rows.Count, "N").End(xlUp).Row)
    
    If Not Application.Intersect(KeyCells, Target) Is Nothing Then
        lastRow = DataSheet.Cells(DataSheet.Rows.Count, "J").End(xlUp).Row
        
        isDataSheetUnprotected = False
        If DataSheet.ProtectContents Then
            DataSheet.Unprotect Password:="Mama, I'm Coming Home"
            isDataSheetUnprotected = True
        End If
        
        DataSheet.Range("J3:O" & lastRow).Sort Key1:=DataSheet.Range("N3:N" & lastRow), _
            Order1:=xlDescending, Header:=xlNo
        
        If UCase(Target.Value) = "YES" Then
            MsgBox "复制第 " & Target.Row & " 行的数据"
            destinationRow = PastedSheet.Cells(PastedSheet.Rows.Count, "A").End(xlUp).Row + 1
            MsgBox "粘贴工作表中的目标行: " & destinationRow
            Set sourceRange = DataSheet.Range("J" & Target.Row & ":K" & Target.Row)
            sourceRange.Copy
            DataSheet.Range("A" & destinationRow).PasteSpecial xlPasteValues
            MsgBox "数据已复制并粘贴"
            Application.CutCopyMode = False
        End If
        
        If isDataSheetUnprotected Then
            DataSheet.Protect Password:="Mama, I'm Coming Home"
        End If
    End If
    
    Application.EnableEvents = True
End Sub

希望这可以帮助您理解代码的内容。如果您有任何其他问题,请随时提问。

英文:

My present code is below.

I have two sheets in a workbook.

1.) Sheet Named - Data and Pasted

2.) Data Sheet is protected. Columns J to O are unprotected, Starting from cell J3:O1000. Rows 1 and 2 are reserved for headers.

3.) The range J:O automatically sorts with the help of the below code whenever there is YES in column N , while No will always be after YES, in the end blanks.

4.) This is working but it is not copying and pasting as values of those rows from J:O where it shows YES in column N

I can't understand why it is not working. TIA

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    Dim lastRow As Long
    Dim DataSheet As Worksheet
    Dim PastedSheet As Worksheet
    Dim sourceRange As Range
    Dim destinationRow As Long
    Dim isDataSheetUnprotected As Boolean
    
    Application.EnableEvents = False
    
    Set DataSheet = ThisWorkbook.Sheets("Data")
    Set PastedSheet = ThisWorkbook.Sheets("Pasted")
    
    Set KeyCells = Range("N3:N" & Cells(Rows.Count, "N").End(xlUp).Row)
    
    If Not Application.Intersect(KeyCells, Target) Is Nothing Then
        lastRow = DataSheet.Cells(DataSheet.Rows.Count, "J").End(xlUp).Row
        
        isDataSheetUnprotected = False
        If DataSheet.ProtectContents Then
            DataSheet.Unprotect Password:="Mama, I'm Coming Home"
            isDataSheetUnprotected = True
        End If
        
        DataSheet.Range("J3:O" & lastRow).Sort Key1:=DataSheet.Range("N3:N" & lastRow), _
            Order1:=xlDescending, Header:=xlNo
        
If UCase(Target.Value) = "YES" Then
    MsgBox "Copying data for row " & Target.Row
    destinationRow = PastedSheet.Cells(PastedSheet.Rows.Count, "A").End(xlUp).Row + 1
    MsgBox "Destination row in Pasted sheet: " & destinationRow
    Set sourceRange = DataSheet.Range("J" & Target.Row & ":K" & Target.Row)
    sourceRange.Copy
    DataSheet.Range("A" & destinationRow).PasteSpecial xlPasteValues
    MsgBox "Data copied and pasted"
    Application.CutCopyMode = False
End If
        
        If isDataSheetUnprotected Then
            DataSheet.Protect Password:="Mama, I'm Coming Home"
        End If
    End If
    
    Application.EnableEvents = True
End Sub

答案1

得分: 1

Your code is pasting the data on the data sheet instead of on the pasted sheet. I've changed the line as needed:

If UCase(Target.Value) = "YES" Then
    MsgBox "Copying data for row " & Target.Row
    destinationRow = PastedSheet.Cells(PastedSheet.Rows.Count, "A").End(xlUp).Row + 1
    MsgBox "Destination row in Pasted sheet: " & destinationRow
    Set sourceRange = DataSheet.Range("J" & Target.Row & ":K" & Target.Row)
    sourceRange.Copy
    PastedSheet.Range("A" & destinationRow).PasteSpecial xlPasteValues 'changed this line
    MsgBox "Data copied and pasted"
    Application.CutCopyMode = False
End If
英文:

Your code is pasting the data on the data sheet instead of on the pasted sheet. I've changed the line as needed:

If UCase(Target.Value) = "YES" Then
    MsgBox "Copying data for row " & Target.Row
    destinationRow = PastedSheet.Cells(PastedSheet.Rows.Count, "A").End(xlUp).Row + 1
    MsgBox "Destination row in Pasted sheet: " & destinationRow
    Set sourceRange = DataSheet.Range("J" & Target.Row & ":K" & Target.Row)
    sourceRange.Copy
    PastedSheet.Range("A" & destinationRow).PasteSpecial xlPasteValues 'changed this line
    MsgBox "Data copied and pasted"
    Application.CutCopyMode = False
End If

huangapple
  • 本文由 发表于 2023年8月10日 21:46:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定