Excel VBA将静态范围复制到不同工作表的动态范围 [英] Excel VBA copying a static range into a dynamic range on different sheet

查看:135
本文介绍了Excel VBA将静态范围复制到不同工作表的动态范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个问题,因为我是VBA的新人,但我相信它有一个非常简单的解决方案。



我是基本上希望自动将新数据添加到工作表中。



工作表:INB BASKET单元格:A2:I76包含到另一个工作表的实时链接。
基本上,我想将这些值复制到IND INDEX TOTAL下方按钮上的最新条目按(不继续更新)。



我创建了一个excel中的动态命名范围(PasteRange)可以选择我要粘贴到最后一个条目的75行:

  = OFFSET ('IND TOTAL'!$ A $ 1,COUNTA('IND TOTAL'!$ A:$ A),0,75,9)

然后,我创建了一个包含以下内容的模块:

  Sub CopyRange()
Dim CopyFrom As Range

Set CopyFrom = Sheets(IND BASKET)。Range(A2,[I76])
et PasteArea = Sheets(IND TOTAL)。范围(PasteRange)

CopyFrom.Copy
PasteArea.PasteSpecial xlPasteValues
End Sub

但是至今没有成功,请指教。

解决方案

有意粘贴这些值,那么直接的值传递就更有效率,而且不涉及剪贴板/ p>

  with Sheets(IND BASKET)。范围(A2:I76)
表格(IND TOTAL ).Range(A& Rows.Count).End(xlUp).Offset(1,0).Resize(.Rows.Count,.Columns.Count)= .Value
end with
/ pre>

使用您自己的副本,粘贴特殊xlPasteValues,您只需要指定目标左上角的单元格,并允许复制的区域定义大小和形状。

 表格(IND BASKET)。范围(A2:I76)。复制
表格(IND TOTAL)。Range(A& Rows.Count).End(xlUp).Offset(1,0).PasteSpecial xlPasteValues

您的命名范围的定义可能更好,没有易失性的OFFSET功能,如

 '或仅为第一个单元格
= INDEX('IND TOTAL'!$ A:$ A,MATCH (zzz,IND TOTAL!$ A:$ A)+1)

该列A包含我使用COUNTA和...推断的文本不COUNT。如果列A包含数字,请将1e99交换为zzz。


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 to a sheet.

Sheet: INB BASKET Cells: A2:I76 contains live links to another worksheet. Basically I want to copy these as Values to Sheet IND TOTAL below the latest entry on a button press (not continually updating).

I've created a dynamic named range (PasteRange) in excel that selects the 75 rows below the last entry that I want to paste into:

=OFFSET('IND TOTAL'!$A$1,COUNTA('IND TOTAL'!$A:$A),0,75,9)

I've then created a module with the following:

Sub CopyRange()
Dim CopyFrom As Range

Set CopyFrom = Sheets("IND BASKET").Range("A2", [I76])
et PasteArea = Sheets("IND TOTAL").Range("PasteRange")

CopyFrom.Copy
PasteArea.PasteSpecial xlPasteValues
End Sub

But to no success as of yet, please advise.

解决方案

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".

这篇关于Excel VBA将静态范围复制到不同工作表的动态范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆