Message box to flag up negative figure

Billdub417

New Member
Joined
Nov 5, 2019
Messages
45
Hello,

Some help with a code please...

I have set up the below sheet showing quantities of different items.

There is a button (offscreen) that exports this list - I would like to add a message box that, when exporting this list, alerts for any negative figures in the period listed (i.e. in this case, period 9 = September, so the message box would say something like:

Error: Negative Values:
Pears: -1
Bananas: -4
Would you like to continue? Yes / NO

is this possible? VV Many thanks!

Sample.xlsx
ABCDEFGHIJKLM
1
2
331/01/202128/02/202131/03/202130/04/202131/05/202130/06/202131/07/202131/08/202130/09/202131/10/202130/11/202131/12/2021
4Apples02-3-45-2351-5-1-3
5Pears6146-26-12-13-34
6Bananas0103-2-526-4126
7
8
9
10MonthPeriod 9
Sheet1
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Can you post your current code?
 
Upvote 0
VBA Code:
Sub Export()
Dim LResult As String
LResult = Trim(Right(Range("b10"), 2)) + 1

If Range(Cells(LResult, 4)) < 0 Then
   MsgBox Range("a4") & "=" & Range(LResult + 1, 4)
   End If

If Range(Cells(LResult, 5)) < 0 Then
    MsgBox Range("a5") & "=" & Range(LResult + 1, 5)
    End If

If Range(Cells(LResult, 6)) < 0 Then
    MsgBox Range("a6") & "=" & Range(LResult + 1, 5)
    End If

End Sub

thanks,
 
Upvote 0
I have corrected it to this... which works, but brings up the errors one by one, how would you get these onto 1 message box?

VBA Code:
Sub Export()

Dim LResult As String
LResult = Trim(Right(Range("b10"), 2))

If Range(Cells(4, LResult + 1), Cells(4, LResult + 1)) < 0 Then
   MsgBox Range("a4") & "=" & Cells(4, LResult + 1)
   End If
   
If Range(Cells(5, LResult + 1), Cells(5, LResult + 1)) < 0 Then
   MsgBox Range("a5") & "=" & Cells(5, LResult + 1)
   End If
   
If Range(Cells(6, LResult + 1), Cells(6, LResult + 1)) < 0 Then
   MsgBox Range("a6") & "=" & Cells(6, LResult + 1)
   End If

End Sub
 
Upvote 0
Perhaps something like this.
VBA Code:
Sub Export()
Dim rng As Range
Dim cl As Range
Dim LResult As String
Dim arrMessage As Variant
Dim cnt As Long
Dim Res As VbMsgBoxResult

    LResult = Trim(Right(Range("b10"), 2))

    ReDim arrMessage(1 To 3)
    
    Set rng = Cells(4, LResult + 1).Resize(3)
    
    For Each cl In rng.Cells
        If cl.Value < 0 Then
            cnt = cnt + 1
            arrMessage(cnt) = Range("A" & cl.Row).Value & "=" & cl.Value
        End If
    Next cl

    If cnt > 1 Then
        ReDim Preserve arrMessage(1 To cnt)
        
        Res = MsgBox(Join(arrMessage, vbCrLf) & vbCrLf & vbCrLf & "Would you like to continue?", _
                     vbYesNo, "Error: Negative Values")
        If Res = vbNo Then
            MsgBox "Export cancelled!", vbInformation, "Cancelled"
            Exit Sub
        End If
    End If

    ' ccde for export
    
End Sub
 
Upvote 0
Solution
Just one follow up point.... ive just noticed this only works for periods 5, 6, 9 & 11? would you know why ??
 
Upvote 0
Oops, slight typo.:eek:

The If statement at the end should read like this.
VBA Code:
    If cnt > 0 Then
        ReDim Preserve arrMessage(1 To cnt)
      
        Res = MsgBox(Join(arrMessage, vbCrLf) & vbCrLf & vbCrLf & "Would you like to continue?", _
                     vbYesNo, "Error: Negative Values")
        If Res = vbNo Then
            MsgBox "Export cancelled!", vbInformation, "Cancelled"
            Exit Sub
        End If
    End If
 
Upvote 0

Forum statistics

Threads
1,223,948
Messages
6,175,570
Members
452,652
Latest member
eduedu

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