我想将选定的行从Sheet1复制到Sheet2。下面的代码将覆盖Sheet2文件中的现有值。请协助停止覆盖我现有的值。
Sub CopySelection()
Dim xlSel As Excel.Range
Set xlSel = Excel.Application.Selection
xlSel.Copy Excel.Application.Sheets("All_Data").Range("A1")
End Sub
任何帮助将不胜感激。
下面的代码将复制在“ Sheet1 ”中选择的范围(对其进行修改以适合包含要复制数据的工作表的名称),然后将其粘贴到工作表“ All_Data ”的第一个可用行的A列中。
让我知道下面(经过测试)的代码是否有效:
Sub CopySelection()
Dim Sht1 As Worksheet
Dim Sht2 As Worksheet
Dim xlSel As Range
Dim LastRow As Long
' modify "Sheet1" to your sheet source (where you make your Selection to copy)
Set Sht1 = ThisWorkbook.Sheets("Sheet1")
' sheet "All_Data" is your target sheet (Where you paste the selection)
Set Sht2 = ThisWorkbook.Sheets("All_Data")
Set xlSel = Selection
'option 1: find last row with data in Sheet "All_Data" at Column A
LastRow = Sht2.Cells(Sht2.Rows.Count, "A").End(xlUp).Row
'option 2: (less reliable) find last row with data in Sheet "All_Data" using UsedRange
'LastRow = Sht2.UsedRange.Rows(Sht2.UsedRange.Rows.Count).Row
' paste the copied range in Column A, the first row after row with data
xlSel.Copy Sht2.Range("A" & LastRow + 1)
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句