how show data in range by messagebox

Alaa mg

Active Member
Joined
May 29, 2021
Messages
343
Office Version
  1. 2019
hello
can anybody help me to show data are range by messagebox?
the range a1: f20 should show in message box
I try with this code but doesn't work correctly .
VBA Code:
Sub n()
Dim cell As Range
For Each cell In Range("a1:f20")
MsgBox "here is data " & cell
Next
End Sub
thanks
 

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.
Can you explain a bit more about what is not working, and what you are trying to achieve?

VBA Code:
Sub n()
    Dim cell As Range, Ans As Integer
    
    For Each cell In Range("a1:f20")
        Ans = MsgBox("Here is data in cell " & cell.Address & ": " & cell.Value, vbOKCancel, "Range: " & Range("a1:f20").Address)
        If Ans = vbCancel Then Exit Sub
    Next cell
End Sub
 
Upvote 0
Hi

I want showing all of data in message box at once . as the data show in range should show all of data in messagebox one time

not separately for each cell as my code or yours.
 
Upvote 0
Try This:
VBA Code:
Sub Show_Range()
'Modified 5/20/2022  4:40:55 PM  EDT
Application.ScreenUpdating = False
Dim r As Range
Dim ans As String
Dim i As Long
i = 0
    For Each r In Range("A1:F30")
        ans = ans & r.Value & " "
        i = i + 1
            If i = 6 Then ans = ans & vbNewLine: i = 0
    Next
MsgBox ans
Application.ScreenUpdating = True
End Sub
 
Upvote 0
A lot depend on how you want to display it. One way:

VBA Code:
Sub n()
    Dim cell As Range, rng As Range, S As String
    
    Set rng = Range("a1:f20")
    For Each cell In rng
        S = S & cell.Value & " "
    Next cell
    
    S = Application.Trim(S)
    
    MsgBox "Here is the data in range " & rng.Address & vbCr & vbCr & S, vbOKOnly
End Sub
 
Upvote 0
In my script I used:
For Each r In Range("A1:F30")
Should have been
For Each r In Range("A1:F20")
Just modify my range
 
Upvote 0
@rlv01 thanks but I want showing the same thing as in sheet
this is what i got based on your code
1.PNG
 
Upvote 0
VBA Code:
Sub n2()
    Dim cell As Range, RRng As Range
    Dim R As Range, CRng As Range, S As String
    
    Set RRng = Range("a1:f1")
    For Each cell In RRng
        S = S & cell.Value & Chr(9)
    Next cell
    S = Application.Trim(S) & vbCr
    
    For Each cell In RRng
        S = S & "------" & Chr(9)
    Next cell
    S = Application.Trim(S) & vbCr
    
    Set RRng = Range("a2:a20")
    For Each cell In RRng
        Set CRng = cell.Resize(1, 6)
        For Each R In CRng
            S = S & R.Value & Chr(9)
        Next R
        S = S & vbCr
    Next cell
    S = Application.Trim(S)
    
    MsgBox "Here is the data in range " & RRng.Address & vbCr & vbCr & S, vbOKOnly
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,843
Members
449,051
Latest member
excelquestion515

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