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

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

Worksheet Change event not copying and pasting data

问题

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

  1. 我的当前代码如下。
  2. 我有一个工作簿中的两个工作表。
  3. 1.) 名称为 - 数据和粘贴的工作表
  4. 2.) 数据工作表受保护。从单元格J3O1000开始,列JO未受保护。行12保留为标题。
  5. 3.) 当列N中有**YES**时,范围J:O会自动排序,下面的代码会帮助排序,而No将始终在YES之后,在末尾是空白单元格。
  6. 4.) 这个代码可以工作,但它没有将显示为列N中有YESJ:O行复制并粘贴为值。
  7. 我无法理解为什么它不起作用。TIA
  8. Private Sub Worksheet_Change(ByVal Target As Range)
  9. Dim KeyCells As Range
  10. Dim lastRow As Long
  11. Dim DataSheet As Worksheet
  12. Dim PastedSheet As Worksheet
  13. Dim sourceRange As Range
  14. Dim destinationRow As Long
  15. Dim isDataSheetUnprotected As Boolean
  16. Application.EnableEvents = False
  17. Set DataSheet = ThisWorkbook.Sheets("Data")
  18. Set PastedSheet = ThisWorkbook.Sheets("Pasted")
  19. Set KeyCells = Range("N3:N" & Cells(Rows.Count, "N").End(xlUp).Row)
  20. If Not Application.Intersect(KeyCells, Target) Is Nothing Then
  21. lastRow = DataSheet.Cells(DataSheet.Rows.Count, "J").End(xlUp).Row
  22. isDataSheetUnprotected = False
  23. If DataSheet.ProtectContents Then
  24. DataSheet.Unprotect Password:="Mama, I'm Coming Home"
  25. isDataSheetUnprotected = True
  26. End If
  27. DataSheet.Range("J3:O" & lastRow).Sort Key1:=DataSheet.Range("N3:N" & lastRow), _
  28. Order1:=xlDescending, Header:=xlNo
  29. If UCase(Target.Value) = "YES" Then
  30. MsgBox "复制第 " & Target.Row & " 行的数据"
  31. destinationRow = PastedSheet.Cells(PastedSheet.Rows.Count, "A").End(xlUp).Row + 1
  32. MsgBox "粘贴工作表中的目标行: " & destinationRow
  33. Set sourceRange = DataSheet.Range("J" & Target.Row & ":K" & Target.Row)
  34. sourceRange.Copy
  35. DataSheet.Range("A" & destinationRow).PasteSpecial xlPasteValues
  36. MsgBox "数据已复制并粘贴"
  37. Application.CutCopyMode = False
  38. End If
  39. If isDataSheetUnprotected Then
  40. DataSheet.Protect Password:="Mama, I'm Coming Home"
  41. End If
  42. End If
  43. Application.EnableEvents = True
  44. 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

  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2. Dim KeyCells As Range
  3. Dim lastRow As Long
  4. Dim DataSheet As Worksheet
  5. Dim PastedSheet As Worksheet
  6. Dim sourceRange As Range
  7. Dim destinationRow As Long
  8. Dim isDataSheetUnprotected As Boolean
  9. Application.EnableEvents = False
  10. Set DataSheet = ThisWorkbook.Sheets("Data")
  11. Set PastedSheet = ThisWorkbook.Sheets("Pasted")
  12. Set KeyCells = Range("N3:N" & Cells(Rows.Count, "N").End(xlUp).Row)
  13. If Not Application.Intersect(KeyCells, Target) Is Nothing Then
  14. lastRow = DataSheet.Cells(DataSheet.Rows.Count, "J").End(xlUp).Row
  15. isDataSheetUnprotected = False
  16. If DataSheet.ProtectContents Then
  17. DataSheet.Unprotect Password:="Mama, I'm Coming Home"
  18. isDataSheetUnprotected = True
  19. End If
  20. DataSheet.Range("J3:O" & lastRow).Sort Key1:=DataSheet.Range("N3:N" & lastRow), _
  21. Order1:=xlDescending, Header:=xlNo
  22. If UCase(Target.Value) = "YES" Then
  23. MsgBox "Copying data for row " & Target.Row
  24. destinationRow = PastedSheet.Cells(PastedSheet.Rows.Count, "A").End(xlUp).Row + 1
  25. MsgBox "Destination row in Pasted sheet: " & destinationRow
  26. Set sourceRange = DataSheet.Range("J" & Target.Row & ":K" & Target.Row)
  27. sourceRange.Copy
  28. DataSheet.Range("A" & destinationRow).PasteSpecial xlPasteValues
  29. MsgBox "Data copied and pasted"
  30. Application.CutCopyMode = False
  31. End If
  32. If isDataSheetUnprotected Then
  33. DataSheet.Protect Password:="Mama, I'm Coming Home"
  34. End If
  35. End If
  36. Application.EnableEvents = True
  37. 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:

  1. If UCase(Target.Value) = "YES" Then
  2. MsgBox "Copying data for row " & Target.Row
  3. destinationRow = PastedSheet.Cells(PastedSheet.Rows.Count, "A").End(xlUp).Row + 1
  4. MsgBox "Destination row in Pasted sheet: " & destinationRow
  5. Set sourceRange = DataSheet.Range("J" & Target.Row & ":K" & Target.Row)
  6. sourceRange.Copy
  7. PastedSheet.Range("A" & destinationRow).PasteSpecial xlPasteValues 'changed this line
  8. MsgBox "Data copied and pasted"
  9. Application.CutCopyMode = False
  10. 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:

  1. If UCase(Target.Value) = "YES" Then
  2. MsgBox "Copying data for row " & Target.Row
  3. destinationRow = PastedSheet.Cells(PastedSheet.Rows.Count, "A").End(xlUp).Row + 1
  4. MsgBox "Destination row in Pasted sheet: " & destinationRow
  5. Set sourceRange = DataSheet.Range("J" & Target.Row & ":K" & Target.Row)
  6. sourceRange.Copy
  7. PastedSheet.Range("A" & destinationRow).PasteSpecial xlPasteValues 'changed this line
  8. MsgBox "Data copied and pasted"
  9. Application.CutCopyMode = False
  10. 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:

确定