I have a problem that I hope someone can help with. I have a sheet that gives a total using a sumif and I want to popup a message box that contains the values of the cells that were called by the sumif.
I have the code that will list all the cells in the range but not just the ones called by the sumif.
I have attached a screenshot of the sheet and the VBA code is below.
I have looked at precedents but in my real spreadsheet the sumif criteria key is on another sheet.
In the picture attached A8 contains the value SUM=(A1:A7) but should contain =SUMIF(B1:B7,D1,A1:A7) and the msgbox should contain a list of the values picked up.
Private Function RangeToString(ByRef rngDisplay As Range, ByVal strSeparator As String) As String
'The string to separate elements on the message box,
'if the range size is more than one cell
Dim strMessage As String
Dim astrMessage() As String
Dim avarRange() As Variant
Dim varElement As Variant
Dim i As Long
'If the range is only one cell, we will return that that
If rngDisplay.Cells.Count = 1 Then
strMessage = rngDisplay.Value
'Else the range is multiple cells, so we need to concatenate their values
Else
'Assign range to a variant array
avarRange = rngDisplay
'Loop through each element to build a one-dimensional array of the range
For Each varElement In avarRange
ReDim Preserve astrMessage(i)
astrMessage(i) = CStr(varElement)
i = 1 + i
Next varElement
'Build the string to return
strMessage = Join(astrMessage, strSeparator)
End If
RangeToString = strMessage
End Function
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Target.Address(True, True, xlA1) = "$A$8" Then
MsgBox RangeToString(Sheet2.Range("A1:A7"), vbCrLf), , "Items"
End If
End Sub
Bookmarks