我想将“Sheet 1”和“sheet 2”的数据复制到“Sheet 3”
我有以下代码
Sub sbCopyRangeToAnotherSheet()
Sheets("Sheet1").Range("A1:P100").Copy
Sheets("Sheet3").Activate
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
答案 0 :(得分:3)
尝试这个简短的例程。您正在复制整个Worksheet.UsedRange property,因此我假设您在第1行中没有应该不复制的列标题标签。
Sub this_and_that()
'clear Sheet3
With Worksheets("Sheet3")
.Cells(1, 1).CurrentRegion.Clear
End With
With Worksheets("Sheet1")
.Cells(1, 1).CurrentRegion.Copy _
Destination:=Worksheets("Sheet3").Cells(1, 1)
End With
With Worksheets("Sheet2")
.Cells(1, 1).CurrentRegion.Copy _
Destination:=Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
End With
End Sub
你没有说明在Sheet3中开始粘贴数据的确切位置(在激活工作表时默认为ActiveCell property并不精确)所以我选择在Copy& amp;之前简单地清除Sheet3。糊。
答案 1 :(得分:0)
Sub Test1()
Dim Sh1 As Worksheet, Sh3 As Worksheet, Sh2 As Worksheet
' This provides IntelliSense
Set Sh1 = Sheets("Sheet1")
Set Sh2 = Sheets("Sheet2")
Set Sh3 = Sheets("Sheet3")
Sh3.UsedRange.Clear
Sh1.Range("A1:P100").Copy Sh3.Cells(1, 1)
Sh3.Range("A1:P100").Font.Color = vbBlue
Sh2.Range("A1:P100").Copy Sh3.Cells(101, 1)
Sh3.Range("A101:P201").Font.Color = vbGreen
Sh3.UsedRange.Sort Key1:=Sh3.Cells(1, 3), Order1:=xlAscending, Header:=xlGuess
End Sub
以上代码有效。问题解决了