Read multiple rows as one string in vba

Nothnless

Board Regular
Joined
Apr 28, 2016
Messages
142
Hi, how can I combine multiple rows of text into a single string without concatenating them on the sheet?

row 1 text
row 2 text
row 3 text
row 4 text

I'm trying to do something like this:

VBA Code:
Sub Macro1()
Dim myWorkbook As Workbook
Dim mySheet As Worksheet
Dim lngRows As Long
Dim rngRows As Range
Dim mCode As String

Set myWorkbook = ThisWorkbook
Set mySheet = myWorkbook.Sheets("The Sheet Name")
 lngRows = mySheet.Range("B1").Value  'Contains dynamic number of rows
Set rngRows = mySheet.Rows(1 & ":" & lngRows).Cells
 mCode = rngRows.Value

myWorkbook.Queries.Item("Table Name").Formula = mCode

End Sub
 
I agree, but it seemed odd to me to concatenate bottom upward... I thought maybe I missed something in the original question that led you to assume that. If we concatenate "normally" (downward)...
VBA Code:
Sub ConcatenateColumnRange()

  Dim ConcatenatedText As String

  ConcatenatedText = Join(Application.Transpose(Range("A1", Cells(Rows.Count, "A").End(xlUp))))

  MsgBox ConcatenatedText

End Sub

Hi, how can I add in a comma after ever concatenated text using this code? Sorry, just discovering I need them.
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Try
VBA Code:
ConcatenatedText = Join(Application.Transpose(Range("A1", Cells(Rows.Count, "A").End(xlUp))),",")
 
Upvote 0

Forum statistics

Threads
1,214,817
Messages
6,121,720
Members
449,050
Latest member
MiguekHeka

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