从具有命名范围的外部书籍获取数据到命名范围

时间:2015-08-14 10:59:02

标签: excel vba named-ranges

早上好,

我的vba体验相当有限,但通常可以通过googled'代码,以满足我的大多数基本需求。 但是,我目前遇到以下代码的问题。目的是从一张表中的表中获取数据,并将其复制到另一张表上的同等大小的表(都称为命名范围 - MainList)中。

它适用于单个细胞,但不想做整个范围。有人可以帮忙吗?

Dim path As String
path = ActiveSheet.Range("A1").Value

Dim currentWb As Workbook
Set currentWb = ThisWorkbook

Dim openWb As Workbook
Set openWb = Workbooks.Open(path)

Dim openWs As Worksheet
Set openWs = openWb.Sheets("Job Details")

currentWb.Sheets("Main List").Range("MainList") = openWs.Range("MainList")

openWb.Close (False)

1 个答案:

答案 0 :(得分:0)

使用复制和粘贴

Dim openWb As Workbook
Dim openWs As Worksheet
Dim openRange As Range
Dim destRange As Range

Set openWb = Workbooks.Open("E:\DOCS\Book1.xlsm")
Set openWs = openWb.Sheets("Sheet1")
Set openRange = openWs.Range("$A$1:$H$16")
Set destRange = ThisWorkbook.Sheets("Sheet1").Range("$A$1:$H$16")

'magic happens here
openRange.Copy
destRange.PasteSpecial

openWb.Close False

Set openWs = Nothing
Set openWb = Nothing