VBA

LBee

New Member
Joined
Dec 25, 2021
Messages
20
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a small VBA script that, when I press button, checks if values in different cells are valid.
If not, it displays a messagebox telling which cell is not correct, otherwise is displays a messagebox saying "Everything looks fine"
It does work, but it checks one cell at a time - if 3 cells are wrong I have to press the button, fix first cell, press it again, fix second cell ...

What I like it to do, was to check all cells, and then display only one messagebox saying e.g. :

A1 is empty
A7 does not contain 5 digits
D2 does not contain 7 digits

Is that possible?

This is the script I'm currently using
VBA Code:
Sub Button1_Click()
    
    If IsEmpty(Range("A1")) = True Then
        MsgBox "A1 is empty"
    ElseIf IsNumeric(Range("B4")) = False Then
        MsgBox "B4 is not a number"
    ElseIf Len(Range("A7")) <> 5 Then
        MsgBox "A7 does not contain 5 digits"
    ElseIf Len(Range("D2")) <> 7 Then
        MsgBox "D2 does not contain 7 digits"
    Else
        MsgBox "Everything looks fine "
    End If

End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Try this
VBA Code:
Sub Button1_Click()
    Dim tmp As String
    If IsEmpty(Range("A1")) = True Then tmp = "A1 is empty"        
    If IsNumeric(Range("B4")) = False Or Range("B4") = "" Then tmp = tmp & vbNewLine & "B4 is not a number"      
    If Len(Range("A7")) <> 5 Then tmp = tmp & vbNewLine & "A7 does not contain 5 digits"
    If Len(Range("D2")) <> 7 Then tmp = tmp & vbNewLine & "D2 does not contain 7 digits"      
    If Len(tmp) > 0 Then
        If AscW(tmp) = 13 Then tmp = Mid(tmp, 3)
        MsgBox tmp
    Else
        MsgBox "Everything looks fine "
    End If

End Sub
 
Upvote 0
Solution
Slightly different way:

VBA Code:
Option Explicit
Sub Macro1()

    Dim strMsg As String

    If IsEmpty(Range("A1")) = True Then
        strMsg = "A1 is empty"
    End If
    If IsNumeric(Range("B4")) = False Then
        strMsg = IIf(Len(strMsg) = 0, "B4 is not a number", strMsg & vbNewLine & "B4 is not a number")
    End If
    If Len(Range("A7")) <> 5 Then
        strMsg = IIf(Len(strMsg) = 0, "A7 does not contain 5 digits", strMsg & vbNewLine & "A7 does not contain 5 digits")
    End If
    If Len(Range("D2")) <> 7 Then
        strMsg = IIf(Len(strMsg) = 0, "D2 does not contain 7 digits", strMsg & vbNewLine & "D2 does not contain 7 digits")
    End If
    If Len(strMsg) = 0 Then
        strMsg = "Everything looks fine"
    End If
    
    MsgBox strMsg

End Sub
 
Upvote 0
Both of you, thank you very much - and sorry about the thread title, I was so focused on the description, that I didn't notice something was missing in the title

I tested both suggestion, and both are working (y)
Regarding the solution Trebor76 suggested - I had to remove "Option Explicit" to make it work, what should this line do?
 
Upvote 0
@Phuoc I'm trying to understand what I'm doing, but I cant figure out this part "If AscW(tmp) = 13 Then tmp = Mid(tmp, 3)"
Could you explain that part?
 
Upvote 0
if string was " " (space vbNewline on VBA or Alt-Enter in worksheet input), AscW(string) = 13
If the first 3 IFs failed, it release 3 spaces, like this: " D7 does not..."
Since trim function does not work for that kind of space, using MID(string,3) to get partial string counted from 3th position.
 
Upvote 0
I had to remove "Option Explicit" to make it work, what should this line do?

Option Explicit forces you to declare all your variables. If you don't use it any undeclared variables will be defined as variants - the most expensive type of variable which for large procedures can have an impact.

You must have at least one undeclared variable in your code. It is good practice to always use Option Explicit.

Regards,

Robert
 
Upvote 0
Thanks for the explanation @Trebor76 and @bebo021999
I think I have to read more about this Option Explicit to fully understand it, its a "little" beyond my level, but now I know what to look for.

Happy new year

Regards
/L
 
Upvote 0

Forum statistics

Threads
1,215,217
Messages
6,123,673
Members
449,116
Latest member
HypnoFant

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