我有一个使用PasteSpecial复制和粘贴行的脚本以及使用xlMultiply的PasteSpecial来乘以某些行。该脚本从源文件复制列A和B,并将它们粘贴到A列和B列的目标文件中。在目标文件的C列中,粘贴B * 0.5的值。此脚本重复运行,以便在每次运行时粘贴新行。如下所示,每次执行脚本时,列D的所有行的值(而不仅仅是新行)都会相乘。如何将D列的最后一个预先粘贴的行值作为脚本中的变量传递?感谢帮助。
objXLSWorkSheet.Range("Z99") = 0.5
With objXLSWorkSheet
'get the last row of column D
DlRow = .Range("D" & .Rows.Count).End(Excel.XlDirection.XlUp).Row
'declare the next usable row in column D, desired pasting location
DnRow = DlRow + 1
LNG = .Cells(.Rows.Count).End(Excel.XlDirection.XlUp).Row
'maybe the same as DlRow
'RNGE = .Range(DnRow & LNG)
'LINE ABOVE DOES NOT PASTE MULTIPLIED RESULT AS DESIRED
RNGE = .Range("D2:D" & LNG) 'multiplies everything from D2, want "DnRow:D"
.Range("Z99").Copy()
RNGE.PasteSpecial(Excel.XlPasteType.xlPasteAll, Excel.XlPasteSpecialOperation.xlPasteSpecialOPerationMultiply)
End With
答案 0 :(得分:0)
我发现的一个解决方法是获取前一行计数并设置从上一行计数到下一行计数的范围,这似乎有效。由于行总是匹配,所以这似乎有效。
'Row stuff for Column B
With objXLSWorkSheet
lRow = .Range("B" & .Rows.Count).End(Excel.XlDirection.XlUp).Row
nRow = lRow + 1
oldlRow = lRow
oldnRow = nRow
End With
'multiplication stuff
objXLSWorkSheet.Range("Z99") = 0.5
With objXLSWorkSheet
'get the last row of column D
DlRow = .Range("D" & .Rows.Count).End(Excel.XlDirection.XlUp).Row
'declare the next usable row in column D, desired pasting location
DnRow = DlRow + 1
RNGE = .Range("D" & oldnRow, "D" & DlRow) 'multiplies everything from D2, want "DnRow:D"
.Range("Z99").Copy()
RNGE.PasteSpecial(Excel.XlPasteType.xlPasteAll, Excel.XlPasteSpecialOperation.xlPasteSpecialOPerationMultiply)
End With