我对vba比较陌生。我在从特定单元格复制行时遇到问题。就是说,如果我的单元格位置在D5,那么它应该将整个行从该单元格位置复制到工作表2中的单个单元格中。我需要复制的总单元格数。
提前致谢。
假设activecell是感兴趣的单元格,要复制该行中的所有单元格,可以使用:
StartCol = ActiveCell.End(xlToLeft).Column
EndCol = ActiveCell.End(xlToRight).Column
mRow = ActiveCell.Row
Set mRange = Range(Cells(mRow, StartCol), Cells(mRow, EndCol))
mRange.Copy Destination:=Range("A20") 'Insert your destination instead of "A20"
如果需要插入特定的单元格值,则可以使用Range("your address")
或Cells(RowNumber, ColumnNuber)
代替ActiveCell。
您还可以使用一个字符串变量将一个地址包含在Range中,因此,例如:
mAddress = "D5"
StartCol = Range(mAddress).End(xlToLeft).Column
EndCol = Range(mAddress).End(xlToRight).Column
mRow = Range(mAddress).Row
Set mRange = Range(Cells(mRow, StartCol), Cells(mRow, EndCol))
mRange.Copy Destination:=Range("A20") 'Insert your destination instead of "A20"
更改mAddress的值,您将拥有所需的内容
编辑:这是从D4复制到行尾
mAddress = "D4"
StartCol = Range(mAddress).Column
EndCol = Range(mAddress).End(xlToRight).Column
mRow = Range(mAddress).Row
Set mRange = Range(Cells(mRow, StartCol), Cells(mRow, EndCol))
mRange.Copy Destination:=Range("A20")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句