Writing an array back to the sheet.

JeffGrant

Well-known Member
Joined
Apr 7, 2021
Messages
558
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I am just learning about arrays.

I have this code that reads the initial array in and joins several columns.
VBA Code:
Sub AddColumns()
Dim arr As Variant, rng As Range
arr = Sheet61.Range("A1").CurrentRegion

Dim i As Long

    Set rng = Sheet61.Range("L1")

    With rng.Borders
        rng.Value = "UniqueID"
        rng.Font.Bold = True
        rng.BorderAround xlContinuous
        rng.Borders(xlInsideHorizontal).LineStyle = xlContinuous
        rng.Borders(xlInsideVertical).LineStyle = xlContinuous
    End With

For i = LBound(arr, 1) + 1 To UBound(arr, 1)
    arr(i, 12) = arr(i, 3) & arr(i, 7) & arr(i, 9)
Next i

End Sub

where i need help is how to write the array out again with the extra column. I can do it if i put the write statement inside the For Loop, but that does give me any speed advantage.

I think it has something to do with resizing the initial array, but I am not sure how to go about that.

Thanks for your help.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi All,

I am just learning about arrays.

I have this code that reads the initial array in and joins several columns.
VBA Code:
Sub AddColumns()
Dim arr As Variant, rng As Range
arr = Sheet61.Range("A1").CurrentRegion

Dim i As Long

    Set rng = Sheet61.Range("L1")

    With rng.Borders
        rng.Value = "UniqueID"
        rng.Font.Bold = True
        rng.BorderAround xlContinuous
        rng.Borders(xlInsideHorizontal).LineStyle = xlContinuous
        rng.Borders(xlInsideVertical).LineStyle = xlContinuous
    End With

For i = LBound(arr, 1) + 1 To UBound(arr, 1)
    arr(i, 12) = arr(i, 3) & arr(i, 7) & arr(i, 9)
Next i

End Sub

where i need help is how to write the array out again with the extra column. I can do it if i put the write statement inside the For Loop, but that does give me any speed advantage.

I think it has something to do with resizing the initial array, but I am not sure how to go about that.

Thanks for your help.
Can you please post the appropriate data using XL2BB so that we can see the size of the initial array?
 
Upvote 0
Unfortunately I cant. I have always had trouble with the XL2BB add-in. It just messes up my machine and I have never been able to solve the issue. Sorry
 
Upvote 0
Figured it out. Instead of writing out an extra column to the initial array, I wrote the whole array out to another location and everything worked.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top