Excel VBA:将2D数组写入另一个工作表

时间:2015-04-14 13:29:05

标签: arrays excel vba excel-vba

我有一个VBA函数可以创建一个2D数组res。我想将数组数据写入另一个工作表,而不是执行该函数的当前工作表。这应该是非常简单的,但我是VBA的新手。

代码在最后一行结束,没有错误消息或写入任何数据。

Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Output")

Dim out As Range
Set out = ws.Range("A1")
out.Resize(UBound(res, 1), UBound(res, 2)).Value = res

1 个答案:

答案 0 :(得分:1)

您现有的代码应该有效。

Res实际上是否包含数据?

下面的示例填充数组,检查它是否是多个单元格,然后转储回工作表。

Dim wb As Workbook
Dim ws As Worksheet
Dim ws2 As Worksheet

Dim rng1 As Range
Dim res

Set wb = ThisWorkbook
Set ws = wb.Worksheets("Input")
Set ws1 = wb.Worksheets("Output")

Set rng1 = ws.Range("A1:B10")

If rng1.Cells.Count > 1 Then
    res = rng1.Value2
    ws1.[a1].Resize(UBound(res, 1), UBound(res, 2)).Value2 = res
End If