Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 要粘贴到其他工作表中的数据的动态范围_Vba_Excel - Fatal编程技术网

Vba 要粘贴到其他工作表中的数据的动态范围

Vba 要粘贴到其他工作表中的数据的动态范围,vba,excel,Vba,Excel,我想将表1 A列中的所有数据复制到表2 问题是我想将数据粘贴到表2中,但从单元格A13开始 示例表1 A2要粘贴到表2单元格A13中的值 这是我的代码,它将列A工作表1的值粘贴到列A工作表,但我无法更改起始单元格 Sub test1() ' test1 Macro Application.ScreenUpdating = False Dim s1 As Excel.Worksheet Dim s2 As Excel.Worksheet Dim iLas

我想将表1 A列中的所有数据复制到表2

问题是我想将数据粘贴到表2中,但从单元格A13开始

示例表1 A2要粘贴到表2单元格A13中的值

这是我的代码,它将列A工作表1的值粘贴到列A工作表,但我无法更改起始单元格

Sub test1()
    ' test1 Macro

    Application.ScreenUpdating = False

    Dim s1 As Excel.Worksheet
    Dim s2 As Excel.Worksheet
    Dim iLastCellS2 As Excel.Range
    Dim iLastRowS1 As Long

    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")

    ' get last row number of A in Sheet1
    iLastRowS1 = s1.Cells(s1.Rows.Count, "A").End(xlUp).Row

    ' get last AVAILABLE cell to past into
    Set iLastCellS2 = s2.Cells(s2.Rows.Count, "A").End(xlUp).Offset(1, 0)

    'copy&paste into sheet2
    s1.Range("A2", s1.Cells(iLastRowS1, "A")).Copy iLastCellS2

    Application.ScreenUpdating = True
End Sub

谢谢

如果要始终直接粘贴到A13(非动态),请执行以下操作:

如果您始终希望它至少启动13次:

Set iLastCellS2 = IIf(s2.Cells(s2.Rows.Count, "A").End(xlUp).Row < 13, s2.Cells(13, "A"), s2.Cells(s2.Rows.Count, "A").End(xlUp).Offset(1, 0))
s1.Range("A2", s1.Cells(iLastRowS1, "A")).Copy iLastCellS2
Set iLastCellS2=IIf(s2.单元格(s2.Rows.Count,“A”)。结束(xlUp)。行<13,s2.单元格(13,“A”),s2.单元格(s2.Rows.Count,“A”)。结束(xlUp)。偏移量(1,0))
s1.范围(“A2”,s1.单元格(iLastRowS1,“A”)。复制iLastCellS2
试试这个:

Sub CustomCopy()
    Dim lastRow As Long
    With Sheets("Sheet1")
        'first we find last row in column A (in sheet1)
        lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        'now we copy until found row number and paste it to Sheet2 starting from cell A13
        .Range("A1:A" & lastRow).Copy Sheets("Sheet2").Range("A13:A" & (lastRow + 12))
    End With
End Sub

你的代码对我有用,到底是什么没有按你的要求工作?它是从“Sheet2”中A列的第一个空单元格粘贴的
Sub CustomCopy()
    Dim lastRow As Long
    With Sheets("Sheet1")
        'first we find last row in column A (in sheet1)
        lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        'now we copy until found row number and paste it to Sheet2 starting from cell A13
        .Range("A1:A" & lastRow).Copy Sheets("Sheet2").Range("A13:A" & (lastRow + 12))
    End With
End Sub