Excel:将标题行从一个电子表格复制到另一个电子表格

时间:2018-06-30 13:06:11

标签: excel vba excel-vba

目前我有以下内容:

Sub CopyHeaders()

Dim rngSource As Range
Dim rngDestination As Range

Sheets("BGC").Select
Set rngSource = Range(Cells(1, 1), Cells(1, Range("A1").End(xlToRight).Column))
rngSource.Copy

Sheets("Promoter").Select
Set rngDestination = Range("A1").End(xlToRight).Offset(0, 1)

rngDestination.Select
ActiveSheet.Paste
End Sub

这将从工作表 BGC 中提取标题行,并将其复制到工作表发起人

但是,让我们坐在BGC的第一行中有此数据(C丢失了,即一个空白单元格):

A   B       D

促销员有:

1   2       4

运行此操作可能会导致:

1   2   A   B

但是我想要:

1   2       4    A  B       D

有什么建议吗?

谢谢

1 个答案:

答案 0 :(得分:2)

请勿使用select命令。 看到这个。

Sub CopyHeaders()

Dim Ws As Worksheet, toWs As Worksheet
Dim rngSource As Range
Dim rngDestination As Range
Dim Head As Variant


Set Ws = Sheets("BGC")
With Ws
     Head = .Range(.Cells(1, 1), .Cells(1, Columns.Count).End(xlToLeft))'<~~ edited
End With

Set toWs = Sheets("Promoter")
Set rngDestination = toWs.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)

rngDestination.Resize(1, UBound(Head, 2)) = Head


End Sub