简体   繁体   中英

Excel VBA: Write 2D array to another worksheet

I have a VBA function that creates a 2D array res . I'd like to write the array data to another worksheet, not the current worksheet the function is executed from. This should be pretty straightforwards but i'm new to VBA.

The code ends on the last line without an error message or writing any data.

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

Your existing code should work.

Does Res actually contain data?

Sample below that populates an array, checks if it is more than one cell, then dumps back to the worksheet.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM