VBA display message box with values from sheet range

kramart

New Member
Joined
Apr 3, 2014
Messages
39
Hi,

Sheet1 has a button that when clicked shows a message box. I would like to show in the message box values from sheet2 withing range A1:A4. This should be in a format of list with lines ...
line 1 = cell A1
line 2 = cell A2
.
.

This range has dynamic values and sometimes some cells are empty, depening on values from different cells so for example sometimes there are walues in four cells A1,A2,A3,A4 but sometimes there are values in two cells eg A1,A4.
If there are values in A1 and A4 i would like to display them in the message box without empty spaces/lines eg.
line 1 = cell A1
line 2 = cell A4
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Don't know if your values are numeric or text.
But the code below works for my example.
You can adjust it to your liking.

Code:
Sub Temp()


Dim sh1 As Worksheet
Set sh1 = ThisWorkbook.Sheets("Blad1")


Dim Message As String
Message = ""


Dim Counter As Integer
Counter = 0


Dim Validation As Variant


For X = 1 To 4
    Validation = sh1.Range("A" & X).Value
    If Validation <> "" Then
        If Counter = 0 Then
            Message = Validation & " [Line " & X & "]"
            Counter = Counter + 1
        Else
            Message = Message & vbNewLine & Validation & " [Line " & X & "]"
        End If
    End If
Next X


MsgBox Message


End Sub
 
Upvote 0
Is there a way to insert a links to the displayed txt to so on click you go to another msgbox ? Links depend on the cell values ... so cell A1 has it's own link , cell B2 it's own etc . ?

Cheers A
 
Upvote 0
thank you !

One more question though :) ... is there a way to not to display msgbox if the message is empty meaning that there is nothing the specified range ?
 
Upvote 0
Yes, that is possible. Use the code below.

Code:
Sub Temp()

Dim sh1 As Worksheet
Set sh1 = ThisWorkbook.Sheets("Blad1")




Dim Message As String
Message = ""


Dim Counter As Integer
Counter = 0


Dim Validation As Variant


For X = 1 To 4
    Validation = sh1.Range("A" & X).Value
    If Validation <> "" Then
        If Counter = 0 Then
            Message = Validation & " [Line " & X & "]"
            Counter = Counter + 1
        Else
            Message = Message & vbNewLine & Validation & " [Line " & X & "]"
            Counter = Counter + 1
        End If
    End If
Next X


If Counter = 0 Then Exit Sub


MsgBox Message


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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