Excel VBA copying a static range into a dynamic range on different sheet

后端 未结 1 1071
耶瑟儿~
耶瑟儿~ 2021-01-21 03:56

I\'m having a few issues working this out as I\'m new to VBA but I\'m sure it has a pretty simple solution.

I\'m essentially wanting to automate the addition of new data

相关标签:
1条回答
  • 2021-01-21 04:39

    If you are only interested in pasting the values, then a direct value transfer is more efficient and does not involve the clipboard.

    with Sheets("IND BASKET").Range("A2:I76")
        Sheets("IND TOTAL").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Resize(.Rows.Count, .Columns.Count) = .Value
    end with
    

    With your own Copy, Paste Special xlPasteValues you only need to specify the cell in the top-left corner of the destination and allow the copied area to define the size and shape.

    Sheets("IND BASKET").Range("A2:I76").Copy
    Sheets("IND TOTAL").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
    

    The definition of your named range might be better without the volatile OFFSET function like,

    =INDEX('IND TOTAL'!$A:$A, MATCH("zzz",'IND TOTAL'!$A:$A )+1):INDEX('IND TOTAL'!$I:$I, MATCH("zzz",'IND TOTAL'!$A:$A ) + 75)
    'or for just the first cell
    =INDEX('IND TOTAL'!$A:$A, MATCH("zzz",'IND TOTAL'!$A:$A )+1)
    

    That assumes that column A contains text which I deduced by your use of COUNTA and not COUNT. If column A contains numbers, swap 1e99 for "zzz".

    0 讨论(0)
提交回复
热议问题