Message box for any True

Nothnless

Board Regular
Joined
Apr 28, 2016
Messages
142
Hi, how can I get a message box to display only the values that are true?

VBA Code:
    If DirFileSize1 < 10000 Then
        DirFile1 = "True"
        Else: DirFile1 = "False"
    End If
    If DirFileSize2 < 10000 Then
        DirFile2 = "True"
        Else: DirFile2 = "False"
    End If
    If DirFileSize3 < 10000 Then
        DirFile3 = "True"
        Else: DirFile3 = "False"
    End If
    If DirFileSize4 < 10000 Then
        DirFile4 = "True"
        Else: DirFile4 = "False"
    End If
    If DirFileSize5 < 10000 Then
        DirFile5 = "True"
        Else: DirFile5 = "False"
    End If
    If DirFileSize6 < 10000 Then
        DirFile6 = "True"
        Else: DirFile6 = "False"
    End If

So if only DirFileSize2 and DirFileSize3 are true then the message box should look like this:

VBA Code:
MsgBox DirFile2 & DirFile3, vbCritical, "Failed"
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I would go about this a different way.
Build a dynamic string to return in the Message Box, i.e.
VBA Code:
Dim str as String

If DirFileSize1 < 10000 Then
    str = "File 1 is true, "
End If

If DirFileSize2 < 10000 Then
    str = str & "File 2 is true, "
End If

If DirFileSize3 < 10000 Then
    str = str & "File 3 is true, "
End If

If Len(str)>0 Then
    MsgBox Left(str,Len(str)-2)
End If
 
Upvote 0
Solution
I would go about this a different way.
Build a dynamic string to return in the Message Box, i.e.
VBA Code:
Dim str as String

If DirFileSize1 < 10000 Then
    str = "File 1 is true, "
End If

If DirFileSize2 < 10000 Then
    str = str & "File 2 is true, "
End If

If DirFileSize3 < 10000 Then
    str = str & "File 3 is true, "
End If

If Len(str)>0 Then
    MsgBox Left(str,Len(str)-2)
End If

Wow! thanks so much, can you explain a bit how this part "Left(str,Len(str)-2)" is working?
 
Upvote 0
Wow! thanks so much, can you explain a bit how this part "Left(str,Len(str)-2)" is working?
Yes. If you notice, at the end of every string, we have a comma and space (", "), to prepare for the next string to be added on.
So, when we are done, if our string is not empty, we want to remove these last two characters from the very end.
LEN is the length function that returns the length of the string. So, if we use the LEFT function on our ending string, telling it to take all the characters except for our last 2 (length - 2), then it will drop those.

Does that make sense?
 
Upvote 0

Forum statistics

Threads
1,215,001
Messages
6,122,648
Members
449,092
Latest member
peppernaut

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