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
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
This assumes the text is in Col "A"
VBA Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step -1
Cells(r - 1, 1).Value = Cells(r - 1, 1) & " " & Cells(r, 1).Value
Rows(r).Delete
Next r
End Sub
 
Upvote 0
This assumes the text is in Col "A"
VBA Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step -1
Cells(r - 1, 1).Value = Cells(r - 1, 1) & " " & Cells(r, 1).Value
Rows(r).Delete
Next r
End Sub

Thanks for the effort but this concatenates the rows on the sheet. I can do that, what I'm more asking about is if VBA can read multiple rows as one string variable without having to concatenate them on the sheet.
 
Upvote 0
Ok, maybe this way then
VBA Code:
Sub MM1()
Dim lr As Long, r As Long, t As String
lr = Cells(Rows.Count, "A").End(xlUp).Row
t = ""
For r = lr To 1 Step -1
t = t & Cells(r, 1).Value
Next r
MsgBox t
End Sub
 
Upvote 0
Good Point.....but how do we know what the OP actually wants ?
We don't actually know what the strings represent !
 
Upvote 0
Does the OP want spaces between each string OR something else ?
VBA Code:
Sub MM1()
Dim lr As Long, r As Long, t As String
lr = Cells(Rows.Count, "A").End(xlUp).Row
t = ""
For r = 1 To lr
t = t  & " " & Cells(r, 1).Value
Next r
MsgBox t
End Sub
 
Upvote 0
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
 
Upvote 0
I agree Rick, but you didn't miss anything !!
 
Upvote 0
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

Awesome! Thanks so much Rick, you guys are the best.

Sorry Michael, I should have made it clearer what I was trying to actually do with the string but thanks for helping.
 
Upvote 0

Forum statistics

Threads
1,214,400
Messages
6,119,288
Members
448,885
Latest member
LokiSonic

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