Concatenate list of Numbers using FormulaArray but the list of numbers can vary

koolaid929

New Member
Joined
Feb 17, 2020
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
Hello all,

I'm a rookie when it comes to VBA, so I apologize is this seems remedial. I'm trying to setup VBA code to that will Concatenate a list of numbers that will then be used in a SQL query. I was able to record the macro below, but the list of numbers can change to more or less and I need help updating the code so it will always concatenate all the numbers in the list.

Selection.FormulaArray = _
"=LEFT(CONCAT(""'""&RC[-3]:R[6]C[-3]&""',""),LEN(CONCAT(""'""&RC[-3]:R[6]C[-3]&""',""))-1)"

Also asked here Concatenate a List of Numbers using a FormulaArray but the List can vary
 
Last edited by a moderator:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try the below, it assumes your numbers are in Column A form 1 to lastrow
It currently writes the string to cell B1, and separates them with a comma and space, then trims the last comma and space


VBA Code:
Sub Koolaid929_Concatenate()

Dim NumList As String, i As Integer

For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row
    NumList = NumList & Cells(i, 1) & ", "
Next i

NumList = Left(NumList, Len(NumList) - 2) 'Remove last Comma and Space

Range("B2").Value = NumList
End Sub
 
Upvote 0
Solution
For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row
This works awesome, thanks Cooper. One last thing. Again I apologize as I am very new to VBA. My list of items are starting in A4 because there is information and blank cells in A1, A2, and A3. Is there a way to code it so the NumList starts at A4 and counts the rows from there so I don't have those three unnecessary entries at the beginning?

Thanks again for the help cooper645
 
Upvote 0
Try the below, it assumes your numbers are in Column A form 1 to lastrow
It currently writes the string to cell B1, and separates them with a comma and space, then trims the last comma and space


VBA Code:
Sub Koolaid929_Concatenate()

Dim NumList As String, i As Integer

For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row
    NumList = NumList & Cells(i, 1) & ", "
Next i

NumList = Left(NumList, Len(NumList) - 2) 'Remove last Comma and Space

Range("B2").Value = NumList
End Sub
I also just saw, I need to have " in front of and at the end of the item so each item should look like this "A1234", and then the last item would just be "Z9999". I know how I can get the " on the end of the item before the comma, but how would I add it to before the item number? Sorry this wasn't in my other reply I just caught this.
 
Upvote 0
Actually you know what, I was able to troubleshoot both my questions on my own. Thanks for you help in getting this solved. :)
 
Upvote 0
Hi

Another option:

VBA Code:
Sub Conc()
Dim r As Range

Set r = Range("A1", Cells(Rows.Count, "A").End(xlUp))
Range("B1").Value = Join(Application.Transpose(r.Value), ", ")
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,489
Messages
6,113,949
Members
448,534
Latest member
benefuexx

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