【发布时间】:2019-09-05 14:23:36
【问题描述】:
我试图将数据粘贴到工作表中最后填充行的末尾,但在循环的第一次迭代期间,我的语法覆盖了第 1 行中的标题。
我正在运行一个循环代码,该代码将多个电子表格中的变量复制并转置在一张工作表上。变量在源文件中的两列中,但在目标文件中的一行中。我使用“下一行”函数将第一列中的数据放入第一个空行。然后我想使用“最后一行”函数将第二列中的数据附加到同一行。但是,在第一次操作期间,我的代码不知道第一行数据和第一行变量名称之间的区别,因此第二列数据最终从数据的前半部分向上移动了一行。
Dim lastrow As Long
Dim nextrow As Long
Set sht = ThisWorkbook.Worksheets("Sheet1")
lastrow = sht.Cells(sht.Rows.Count, 1).End(xlUp).Row
nextrow = sht.Cells(sht.Rows.Count, 1).End(xlUp).Row + 1
'assigns objects for the last filled row and the next unfilled row
wb1.Worksheets("Database").Range("B1:B578").Copy
Workbooks("zzmaster.xlsx").Worksheets("Sheet1").Cells(nextrow, 1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
DoEvents
Application.CutCopyMode = False
'copies the relevant data from Column B in Sheet 3 of the source file...
'then transposes and pastes into the next available row of the destination file
wb1.Worksheets("Database").Range("C32:C578").Copy
Workbooks("zzmaster.xlsx").Worksheets("Sheet1").Cells(lastrow, 579).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
DoEvents
Application.CutCopyMode = False
'copies the relevant data from Column C in Sheet 3 of the source file...
'then tranposes and pastes into the same row as the previous function, beginning with...
'the next cell after the last data point
有没有一种好方法可以调整语法,使数据不会粘贴到电子表格的第一行?目标是将源电子表格中的两列粘贴到同一行,并将循环中的每个电子表格粘贴到下一个可用的空行。我很感激任何建议。
【问题讨论】: