简体   繁体   English

将范围作为值复制并粘贴到另一张纸上的下一个空列中

[英]Copy & Paste range as values into next empty column on another sheet

I am trying to copy a range (which varies based on inputs) as values into another sheet.我正在尝试将一个范围(根据输入而变化)作为值复制到另一张表中。
The code below seems to be copying on top of the last entry rather than into the next empty column.下面的代码似乎是复制到最后一个条目的顶部,而不是复制到下一个空列中。

Sub CommandButton3_Click()
    
    Dim source As Worksheet
    Dim destination As Worksheet
    Dim emptyColumn As Long
        
    Set source = Sheets("Month Template")
    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("m26:m35").Copy destination.Cells(1, emptyColumn)
    
End Sub

try with below试试下面

Sub CommandButton3_Click()
    Dim source As Worksheet
    Dim destination As Worksheet
    Dim emptyColumn As Long
    Set source = Sheets("Month Template")
    Set destination = Sheets("Sheet1")
    'find empty Column (actually cell in Row 1)'
    emptyColumn = destination.Cells(1, 1).End(xlToRight).Column
    If emptyColumn > 1 Then
        emptyColumn = emptyColumn + 1
    End If
    source.Range("m26:m35").Copy destination.Cells(1, emptyColumn)
End Sub

This is an old question that popped up.这是一个老生常谈的问题。 When reading the Op's question, it looks like they want to make sure the 1st column is used and then move over to column 2.在阅读 Op 的问题时,他们似乎想确保使用第一列,然后移至第二列。

If emptyColumn > 1 Then

When 1st using the copy&paste code emptycolumn will never be greater than 1.第一次使用复制粘贴代码时, emptycolumn永远不会大于 1。

A possible solution would be to 1st check if A1 ="" when emptycolumn=2.一个可能的解决方案是在 emptycolumn=2 时第一次检查 A1 =“”。

Private Sub CommandButton3_Click()
    Dim source As Worksheet
    Dim destination As Worksheet
    Dim emptyColumn As Long

    Set source = Sheets("Month Template")
    Set destination = Sheets("Sheet1")

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

    If destination.Cells(1, 1) = "" And emptyColumn = 2 Then emptyColumn = 1

    source.Range("m26:m35").Copy destination.Cells(1, emptyColumn)

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试从一张纸上复制一个范围并将其粘贴到另一张纸上一列中的下一个空单元格 - Trying to copy a range from one sheet and paste it to the next empty cell in a column on another sheet 复制并粘贴到下一个空行中的另一个工作表 - Copy and paste to another sheet in the next empty row VBA复制范围值并粘贴到另一张工作表中 - VBA Copy range values and paste in another sheet Excel 2007复制范围值并粘贴到另一个范围(同一张纸)的第一个空行 - Excel 2007 Copy range values and paste to another range (Same Sheet) first empty row 在下一个空行的另一张纸上复制并粘贴(转置和值)设定范围 - Copy and Paste (transposed & values) a set range in a different sheet in the next empty row 如果表格范围内的单元格值等于“新建”,则复制整行并粘贴为工作表 1 中下一个空单元格中的值 - If cell value in table range equals “New” copy entire row and paste as values in sheet 1 in the next empty cell 复制粘贴行,直到 B 列中的值更改到另一个工作表中的下一个空列 - Copy paste rows until change in value in Column B into the next empty column in another Sheet VBA:将值复制并粘贴到其他工作表的下一个空行中 - VBA: Copy and paste values in next empty row in other sheet VBA脚本复制下一张工作表中的一列并粘贴到第一张工作表中的下一个空列 - VBA script to copy a column in the next sheet and paste to the next empty column in the first sheet 使用VBA并使用“偏移”将一系列单元格值复制并粘贴到另一张工作表中 - Copy and paste a range of cell values into another sheet with VBA and using Offset
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM