VBscript:将数据从一个工作簿复制/粘贴到另一个工作簿的下一个空行(特定单元格)

时间:2014-04-07 13:42:20

标签: excel-vba vbscript vba excel

我只是VBscript(和代码)中的新手。我尝试使用" Excel宏 - VBA"将数据从一个工作簿复制/粘贴到另一个工作簿的下一个空行时出现问题。

例如:我想将工作簿1的A2到A4(或更多)的范围复制到工作簿2的A4(空白行)。并且将工作簿1的C2到C4(或更多)的复制范围粘贴到E4 (空行)工作簿2.我使用" Do..Loop"方法

执行新的答案代码后,这是我的问题:

----------甲-----------------ë

1 ------标题-------------标题

2 ------(BLANK)------------ BBB1

3 ------ aaa2 ------------- PasteC1 < - (BLANK)此处的单元格已被替换

4 ------ PasteA1 --------- PasteC2

5 ------ PasteA2 --------- PasteC3

6 ------ PasteA2 ---------

7

我希望它是:

----------甲-----------------ë

1 ------标题-------------标题

2 ------(BLANK)------------ bbb1

3 ------ aaa2 ------------ (BLANK)

4 ------ PasteA1 -------- PasteC1

5 ------ PasteA2 -------- PasteC2

6 ------ PasteA2 -------- PasteC3

7

它正确地粘贴了范围A中的数据,但是来自" B"数据,代码将数据粘贴到下一行与" A"数据而不是粘贴到" A"旁边的单元格。数据。我希望代码正确粘贴到单元格。我想" IF"或" FOR"声明,但我对此没有任何想法。

以下是我编辑的代码,但Dan的答案仍然存在:

'Copy "A" data
With srcWorksheet
    LastSrcRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
With destWorksheet
    LastDestRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
Set srcRange = srcWorksheet.Range(srcWorksheet.Cells(10, 1), srcWorksheet.Cells(LastSrcRow, 1))
srcRange.Copy
destWorksheet.Range(destWorksheet.Cells(LastDestRow + 1, 1), destWorksheet.Cells(LastDestRow + LastSrcRow - 1, 1)).Select
destWorksheet.Paste


'Copy "B" data
With srcWorksheet
    LastSrcRow = .Range("B" & .Rows.Count).End(xlUp).Row
End With
With destWorksheet
    LastDestRow = .Range("B" & .Rows.Count).End(xlUp).Row
End With
Set srcRange = srcWorksheet.Range(srcWorksheet.Cells(10, 2), srcWorksheet.Cells(LastSrcRow, 2))
srcRange.Copy
destWorksheet.Range(destWorksheet.Cells(LastDestRow + 1, 2), destWorksheet.Cells(LastDestRow + LastSrcRow - 1, 2)).Select
destWorksheet.Paste

我已经在很多网站上进行了研究,但还没有找到解决方案。

请帮帮我。

感谢并原谅我的英语不好。

1 个答案:

答案 0 :(得分:0)

当您运行以下代码时,您是否仍然获得交错的Excel粘贴行为?

Sub copy2excel()

Set sourceExcel = CreateObject("Excel.Application")
sourceExcel.Visible = True
Set destExcel = CreateObject("Excel.Application")
destExcel.Visible = True

'Open the source and the destination files
Set srcWorkBook = sourceExcel.Workbooks.Open("C:\Your\Filepath\Here.xls")
Set destWorkbook = destExcel.Workbooks.Open("C:\Your\OtherFilepath\Here.xls")

'Open the 1st sheet of source file
Set srcWorksheet = srcWorkBook.Worksheets(1)
'Open the 1st sheet of destination file
Set destWorksheet = destWorkbook.Worksheets(1)

'copy all data
LastSrcRow = srcWorksheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastDestRow = destWorksheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Set srcRange = Range(srcWorksheet.Cells(2, 1), srcWorksheet.Cells(LastSrcRow, 2))
Set destRange = Range(destWorksheet.Cells(LastDestRow + 1, 1), destWorksheet.Cells(LastDestRow + LastSrcRow - 1, 2))
srcRange.Copy destRange

End Sub

- 结束修订 -

以下是对代码的轻微调整,放弃循环/激活/选择/复制/粘贴,转而使用range.copy

'Copy "A" data
With srcWorksheet
    LastSrcRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
With destWorksheet
    LastDestRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
Set srcRange = Range(srcWorksheet.Cells(2, 1), srcWorksheet.Cells(LastSrcRow, 1))
Set destRange = Range(destWorksheet.Cells(LastDestRow + 1, 1), destWorksheet.Cells(LastDestRow + LastSrcRow - 1, 1))
srcRange.Copy destRange

'copy "B" data
With srcWorksheet
    LastSrcRow = .Range("B" & .Rows.Count).End(xlUp).Row
End With
With destWorksheet
    LastDestRow = .Range("B" & .Rows.Count).End(xlUp).Row
End With
Set srcRange = Range(srcWorksheet.Cells(2, 2), srcWorksheet.Cells(LastSrcRow, 2))
Set destRange = Range(destWorksheet.Cells(LastDestRow + 1, 2), destWorksheet.Cells(LastDestRow + LastSrcRow - 1, 2))
srcRange.Copy destRange

此外,如果您确定将A和B列始终填充到源文件中的同一行,则可以一次完成整个range.copy

'copy all data
LastSrcRow = srcWorksheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastDestRow = destWorksheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Set srcRange = Range(srcWorksheet.Cells(2, 1), srcWorksheet.Cells(LastSrcRow, 2))
Set destRange = Range(destWorksheet.Cells(LastDestRow + 1, 1), destWorksheet.Cells(LastDestRow + LastSrcRow - 1, 2))
srcRange.Copy destRange