【问题标题】:copy paste range in first empty column in excel vba在excel vba的第一个空列中复制粘贴范围
【发布时间】:2014-06-26 14:04:16
【问题描述】:

我想复制工作表 3 的单元格范围 (C1:Z1000) 并将它们粘贴到工作表 1 的第一个空列(在第 1 行中)。下面的代码块在最后一行:source.Range("C1:Z1000").Copy destination.Cells(1, emptyColumn)

Sub CopyRange()

Dim source As Worksheet
Dim destination As Worksheet
Dim emptyColumn As Long

Set source = Sheets("Sheet3")
Set destination = Sheets("Sheet1")

'find empty Column (actually cell in Row 1)'
emptyColumn = destination.Cells(1, destination.Columns.Count).End(xlUp).Column
If emptyColumn > 1 Then
emptyColumn = emptyColumn + 1
End If

source.Range("C1:Z1000").Copy destination.Cells(1, emptyColumn)

End Sub

【问题讨论】:

  • 你的代码不合逻辑。检查 sheet3 的 LAST 列是否为空,如果不是,则取 NEXT 列。这当然永远不会存在。

标签: vba excel copy-paste


【解决方案1】:

我认为您的问题是您获得 emptyColumn 值的方式,正如其他人所建议的那样。这对我有用:

Sub CopyRange()

Dim source As Worksheet
Dim destination As Worksheet
Dim emptyColumn As Long

Set source = Sheets("Sheet3")
Set destination = Sheets("Sheet1")

'find empty Column (actually cell in Row 1)'
emptyColumn = destination.Cells(1, destination.Columns.Count).End(xlToLeft).Column
If emptyColumn > 1 Then
emptyColumn = emptyColumn + 1
End If

source.Range("C1:Z1000").Copy destination.Cells(1, emptyColumn)

End Sub

您当前拥有它的方式将拉出工作表中的最后一列,粘贴时似乎会引发错误。上述方法将拉出第一个空列。也就是说,如果 C 列为空,则 emptyColumn 的值为 3

【讨论】:

    【解决方案2】:

    您是否尝试过单步执行您的代码?

    如果这样做,您会注意到以下行将始终将 emptyColumns 变量设置为最右侧的列,而不管使用了哪些列:

    emptyColumn = destination.Cells(1, destination.Columns.Count).End(xlUp).Column
    

    通过向其添加 1 并粘贴,您可以尝试粘贴到不存在的列。这每次都会给你一个错误。

    请尝试以下方法来查找最后使用的列。它从第一行的最右列开始向左搜索(如键入 CTRL+LEFT),以便找到最后使用的列:

    emptyColumn = destination.Cells(1, destination.Columns.Count).End(xlToLeft).Column
    

    然后就可以加1粘贴了。

    【讨论】:

    • 我认为最后一列是 .End(xlToRight).column
    • 不,这是设计使然。我从最右边 (destination.Columns.Count) 开始向左走,这样我就不会卡在中间的空列上,右边有更多使用的列。 :)
    【解决方案3】:

    试试这个:

    emptyColumn = destination.Cells(1, destination.Columns.Count).End(xltoright).Column
    
    source.Range("C1:Z1000").Copy Destination:= Cells(1, emptyColumn)
    

    命名参数后跟一个冒号,等于 :=

    您的 End 代码应该是: End(xlToRight)

    另一种选择是:

    source.Range("C1:Z1000").Copy 
    with destination
        .cells(1,emptycolumn).select
        .paste
    end with
    

    希望对你有帮助

    菲利普

    【讨论】:

    • OK 你从哪里得到错误?也许试试我在下面给出的第二个选项。另外,是源对象,如工作表或工作簿,或范围
    【解决方案4】:

    我找到了这篇文章,并根据我的需要对其进行了修改。将粘贴转置

    Sub Transpose_PasteModified()
    
    Dim source As Worksheet
    Dim destination As Worksheet
    Dim emptyColumn As Long
    
    Set source = Sheets("Sheet1")
    Set destination = Sheets("Sheet2")
    'Data Source
    source.Range("A3:C3").Copy
    'Gets Empty Column
    emptyColumn = destination.Cells(3, destination.Columns.Count).End(xlToLeft).Column
    
    'In order to avoid issues of first row returning 1
    'in this case #3 is the row so we're checking A3
    'If row changes then change A3 to correct row
    
    If IsEmpty(destination.Range("A3")) Then
    destination.Cells(3, 1).PasteSpecial Transpose:=True
    Else
    emptyColumn = emptyColumn + 1
    destination.Cells(3, emptyColumn).PasteSpecial Transpose:=True
    End If
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多