简体   繁体   English

VBA Excel粘贴到下一行,如果复制了空白单元格,则无信息可转移

[英]VBA Excel Paste on next row without information to shift if there is a blank cell copied

Hi everyone I am using this code to copy and paste information from one excel file to another. 大家好,我正在使用此代码将信息从一个excel文件复制并粘贴到另一个文件。 The problem is that some of the cells i copy have no data and the current code i use pastes the information on the next blank cell thus all of the information shifts. 问题是我复制的某些单元格没有数据,而我使用的当前代码将信息粘贴到下一个空白单元格上,因此所有信息都发生了移动。 Does anyone know how i can fix that problem and paste the information on the next blank row. 有谁知道我如何解决该问题并将信息粘贴到下一个空白行。

Set wb = Workbooks.Open(folderPath & filename)
   Range("F18").Copy
   emptyRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
   Application.DisplayAlerts = False
   ActiveWorkbook.Close
   ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(emptyRow, 1), Cells(emptyRow, 19))
Set wb = Workbooks.Open(folderPath & filename)
   Range("F14").Copy
   emptyRow = Sheet1.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
   Application.DisplayAlerts = False
   ActiveWorkbook.Close
   ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(emptyRow, 2), Cells(emptyRow, 19))

If you want to paste the value from each new source sheet into the next row down on the destination, whether the source cell is blank or not, use a variable to keep track of your destination row: 如果要将每个新的源工作表的值粘贴到目标位置的下一行,无论源单元格是否为空白,请使用变量来跟踪目标行:

' at the top of your code:
Dim destinationRow As Integer
destinationRow = 1

' ... start of your loop

Set wb = Workbooks.Open(folderPath & filename)
   Range("F18").Copy
   Application.DisplayAlerts = False
   ActiveWorkbook.Close
   ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(destinationRow, 1), Cells(emptyRow, 19))
   destinationRow = destinationRow + 1

' and so on
' ... end of your loop

I'm assuming that when you say you're 'looping through different files' you mean you're using some sort of loop in the VBA code that processes all the files. 我假设当您说要“遍历不同文件”时,意味着您正在VBA代码中使用某种循环来处理所有文件。 If you need to run a macro on separate occasions and always have the new data go to the end of the destination range you'll need to have your code save the value of destinationRow in a worksheet cell when it's finished, and read it in again next time it starts. 如果您需要在不同的情况下运行宏,并且始终将新数据移到目标范围的末尾,则需要让代码将完成后的destinationRow值保存在工作表单元格中,然后再次读取下次开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM