Can I print range in a MsgBox as well as text?

72 Views Asked by At

I'm writing a macro on excel.

Every time a value is typed in a specific range, if the calculated result is too low, I need a message box to appear saying ""Sample volume too low! Please increase total volume." as well as printing the range in which it was typed.

The below code works for just printing the message, but I need to know if I can also print the range.

Thank you

Private Sub Worksheet_Change(ByVal Target As Range)


Dim r1 As Range, r2 As Range, MultipleRange As Range
Set r1 = Range("I17:I20")
Set r2 = Range("Q17:Q20")
Set MultipleRange = Union(r1, r2)

If Not Intersect(Target, MultipleRange) Is Nothing Then

 If Range("G17") < 5 Then
        MsgBox "Sample volume too low!Please increase total volume."
    End If
If Range("G18") < 5 Then
        MsgBox "Sample volume too low! Please increase total volume."
    End If
If Range("G19") < 5 Then
        MsgBox "Sample volume too low! Please increase total volume."
    End If
If Range("G20") < 5 Then
        MsgBox "Sample volume too low! Please increase total volume."
If Range("O17") < 5 Then
        MsgBox "Sample volume too low! Please increase total volume."
    End If
If Range("O18") < 5 Then
        MsgBox "Sample volume too low! Please increase total volume."
    End If
If Range("O19") < 5 Then
        MsgBox "Sample volume too low! Please increase total volume."
    End If
If Range("O20") < 5 Then
        MsgBox "Sample volume too low! Please increase total volume."
    End If

End If

End Sub

1

There are 1 best solutions below

3
CDP1802 On
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim r1 As Range, r2 As Range, MultipleRange As Range
    Dim msg As String, c, r as long
    
    Set r1 = Range("I17:I20")
    Set r2 = Range("Q17:Q20")
    Set MultipleRange = Union(r1, r2)
    
    If Intersect(Target, MultipleRange) Is Nothing Then Exit Sub
    
    For Each c In Array("G", "O")
        For r = 17 To 20
            If Cells(r, c) < 5 Then
                msg = msg & vbLf & "Cell " & c & r
            End If
        Next
    Next
    
    If Len(msg) > 0 Then
        MsgBox "Sample volume too low! Please increase total volume in cells " & msg, vbExclamation
    End If

End Sub