如何通过vba选择和复制数据范围

时间:2022-02-04 02:40:59

I am getting stuck on a simple line of code. For this section of my procedure I simply want to merge data from one tab to another. Below is the code i have to copy the information from "Page1-2" and paste in at the next blank row of "Page1-1":

我陷入了一个简单的代码行。对于我的过程的这一部分,我只想将数据从一个选项卡合并到另一个选项卡。下面是我必须从“Page1-2”复制信息并粘贴到“Page1-1”的下一个空白行的代码:

Sheets("Page1-2").Activate
ActiveSheet.Range("A5:AJ66000").Copy
Sheets("Page1-1").Activate
Columns("A").Find("", Cells(Rows.Count, "A")).Select
ActiveCell.PasteSpecial

Any help on this issue would be greatly appreciated.

对此问题的任何帮助将不胜感激。

Thanks!

1 个解决方案

#1


0  

I would create a variable to hold the last row in your Page1-1 sheet.

我会创建一个变量来保存Page1-1表格中的最后一行。

Sub test()
Dim k   As Integer

Sheets("Page1-2").Range("A5:AJ66000").Copy
With Sheets("Page1-1")
    k = .Cells(1048576, 1).End(xlUp).Row
    If k > 1 Then k = k + 1  ' this is so we don't overwrite the last row on previously copied data
    .Cells(k, 1).PasteSpecial
End With
End Sub
    End Sub

Note: It's a good idea to learn how to avoid using .Select, you can see in my code above how to combine those two lines into one.

注意:学习如何避免使用是个好主意。选择,您可以在上面的代码中看到如何将这两行合并为一个。

#1


0  

I would create a variable to hold the last row in your Page1-1 sheet.

我会创建一个变量来保存Page1-1表格中的最后一行。

Sub test()
Dim k   As Integer

Sheets("Page1-2").Range("A5:AJ66000").Copy
With Sheets("Page1-1")
    k = .Cells(1048576, 1).End(xlUp).Row
    If k > 1 Then k = k + 1  ' this is so we don't overwrite the last row on previously copied data
    .Cells(k, 1).PasteSpecial
End With
End Sub
    End Sub

Note: It's a good idea to learn how to avoid using .Select, you can see in my code above how to combine those two lines into one.

注意:学习如何避免使用是个好主意。选择,您可以在上面的代码中看到如何将这两行合并为一个。