Link to home
Start Free TrialLog in
Avatar of Euro5
Euro5Flag for United States of America

asked on

VBA if statement to show msgbox and exit

I need to check if cell A2 is empty, and if so, show msgbox, show Sheet ("Detail") and exit all code.
The entire code if farther below.
I am getting errorUser generated image

If A2 = "" Then
MsgBox = "No data meets minimum deficit weight."
Detail.Activate
End
End If

Open in new window




Sub FORMATREPORT()

Dim rng As Range
Dim LastRow As Long

If A2 = "" Then
MsgBox = "No data meets minimum deficit weight."
Detail.Activate
End
End If



Application.ScreenUpdating = False
        Cells.Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
        
    Range("A1:L1").Select
    Selection.Font.Bold = True
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
Set rng = Application.Intersect(ActiveSheet.UsedRange, Range("A:L"))
rng.SpecialCells(xlCellTypeVisible).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    Columns("J:J").Select
    Selection.Style = "Comma"
    Selection.NumberFormat = "#,##0"
    Columns("K:K").Select
    Selection.Style = "Currency"
    Columns("L:L").Select
    Selection.Style = "Currency"
    Columns("I:I").Select
    Selection.Style = "Comma"
    Selection.NumberFormat = "_(* #,##0.0_);_(* (#,##0.0);_(* ""-""??_);_(@_)"
    Selection.NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
    Columns("A:A").Select
    Selection.NumberFormat = "mm-dd-yyyy"

Columns("A:L").Select
Selection.EntireColumn.AutoFit

LastRow = Cells(Cells.Rows.Count, "J").End(xlUp).Row + 1
Range("K" & LastRow).Formula = "=SUM(K2:K" & LastRow - 1 & ")"
Range("L" & LastRow).Formula = "=SUM(L2:L" & LastRow - 1 & ")"





Application.ScreenUpdating = True
End Sub

Open in new window

Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

The message box call is done as:
MsgBox("Your message here")

Further details can be found here: https://msdn.microsoft.com/en-us/library/aa445082(v=vs.60).aspx
ASKER CERTIFIED SOLUTION
Avatar of Russ Suter
Russ Suter

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Euro5

ASKER

Thanks!