Msgbox

John Davis

Well-known Member
Joined
Sep 11, 2007
Messages
3,457
Hello all:

I have the following code snipet:

Code:
m = Array(Range("X1:X" & lr2).Value)

MsgBox ("" & m)

I'm trying to get the values contained in X1:X & lr2 to show in a msgbox. How would I code that? Any help appreciated.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
MJaybe like this

Code:
m = Range("X1:X" & lr2).Value
For i = LBound(m, 1) To UBound(m, 1)
    msg = msg & vbNewLine & m(i,1)
Next i
MsgBox msg
 
Upvote 0
Test on a blank sheet:
Code:
Sub t()
  Dim m As Variant, lr2 As Long
  lr2 = 10
  Range("X1:X10").Formula = "=Row() & "", "" & Column()"
  m = WorksheetFunction.Transpose(Range("X1:X" & lr2).Value)
  MsgBox Join(m, vbCrLf)
End Sub
 
Upvote 0
Same idea as Mr. Hobson (but for an unknown row of the last cell with data.)
Code:
Sub MsgBoxDemo()
Dim lr2 As Long, ArryRng As Variant
lr2 = Range("X" & Rows.Count).End(xlUp).Row
ArryRng = Range("X1:X" & lr2)
ArryRng = Application.WorksheetFunction.Transpose(ArryRng)
MsgBox Join(ArryRng, ", ")
End Sub
Hope it helps.
 
Upvote 0
This one-liner should do what you asked...

MsgBox Join(WorksheetFunction.Transpose(Range("X1:X" & lr2)), vbLf)
 
Upvote 0
This one-liner should do what you asked...

MsgBox Join(WorksheetFunction.Transpose(Range("X1:X" & lr2)), vbLf)
This would work if you specify your variable (lr2) first. Like so:
Code:
lr2 = Range("X" & Rows.Count).End(xlUp).Row
MsgBox Join(WorksheetFunction.Transpose(Range("X1:X" & lr2)), vbLf)
Or, if you really want it to be a one liner, you can do this:
Code:
MsgBox Join(WorksheetFunction.Transpose(Range("X1:X" & Range("X" & Rows.Count).End(xlUp).Row)), vbLf)

Hope it helps.
 
Upvote 0
This one-liner should do what you asked...

MsgBox Join(WorksheetFunction.Transpose(Range("X1:X" & lr2)), vbLf)
This would work if you specify your variable (lr2) first. Like so:
Code:
lr2 = Range("X" & Rows.Count).End(xlUp).Row
MsgBox Join(WorksheetFunction.Transpose(Range("X1:X" & lr2)), vbLf)
The OP started his message with... "I have the following code snipet", so I made the assumption that, since his code snippet referred to lr2, he must have set lr2 in the code prior to that which he posted. My reference to "one-liner" was to handle the code snippet he was after, not his entire code module (of which we know nothing about).
 
Upvote 0
A fair assumption indeed. That's the drawback of not seeing the OP's original code. Assuming that snippet was the entire routine, (which I did), then your single line solution was (in my opinion) the best.

I only went with the one I suggested as it seemed to be close enough to the original snippet as to be intuitive to the OP. I would rather show a little longer way to do something that the OP would likely recognize & understand than to do it in a way that you and I might understand, but isn't intuitive to the OP.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,730
Members
452,939
Latest member
WCrawford

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