Drop Array Values Across Cells

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,658
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this code which drops the values of an array into a column. How can I change this so that the values, instead of a column are placed in a row (row 2) starting at AA2?

VBA Code:
Sub ExtractNamesToColumn(grp)
    Dim names() As String
    Dim j As Long

    ' Split the string into an array using the comma as a delimiter
    names = Split(grp, ",")
    ' Loop through the array and write the names to the target column
    For j = LBound(names) To UBound(names)
        ' Trim spaces and write each name in column AC starting at AC2
        ws_dump.Cells(j + 3, "AA").Value = Trim(names(j))
    Next j
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Without testing, I feel this would work?
VBA Code:
ws_dump.Cells(2, j + 26).Value = Trim(names(j))
 
Upvote 0
Untested, but try on a copy of your workbook:

VBA Code:
names = Split(grp, ",")
Range("AA2").Resize(1, UBound(names) + 1).Value = names
 
Upvote 0
Solution
There should be no need to loop to put the values into a range.
Your original code though allowed for the clean-up of any leading/consecutive internal/trailing spaces with each name. To keep that, I would make this alteration

VBA Code:
ws_dump.Range("AA2").Resize(1, UBound(names) + 1).Value = Application.Trim(names)
 
Upvote 0

Forum statistics

Threads
1,225,969
Messages
6,188,111
Members
453,460
Latest member
Cjohnson3

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