VBA: Message Box Display, Loop on Condition - vba

I have created VBA code that displays a message box when a certain condition is met. This if statement loops through a determined range. However, the message box is displayed for every cell that meets my condition. Is there a way to ensure the message box is only displayed once?
Here is my code:
Private Sub Worksheet_Change(ByVal Target As Range)
For Each cell In Range("S194:BZ194")
If cell.Value < 0 Then
MsgBox "Unrestricted cash cannot be less than zero (Row 194). Please lower the loan growth rate."
End If
Next
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
For Each cell In Range("S194:BZ194")
If cell.Value < 0 Then
MsgBox "Unrestricted cash cannot be less than zero (Row 194). Please lower the loan growth rate."
Exit For
End If
Next
End Sub

Related

select value of active cell from a range

I have a range 'Bills' (A10:B50) containing dates of my bills, some are paid on the same date so there are duplicates and blanks at the end. I have a range 'Consolidated' (D10:E30) which consolidates the dates and bill amounts into single entries. When I click on the date in 'Consolidated' it highlights the individual entries in the Bills range using conditional formatting.
I use the following to get the date I am clicking on:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ActiveSheet.Range("C40").Formula = "=" & Target.Address
End Sub
However, I use C40 elsewhere, it is formatted as a date, the vba causes havoc when the active cell is not a date. How do I get my vba to work ONLY if the active cell is within the Consolidate range?
Thanks
Not the cleanest solution, but try something like this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Row < Range("$D$10").Row Then Exit Sub
If Target.Column < Range("$D$10").Column Then Exit Sub
If Target.Row > Range("$D$30").Row Then Exit Sub
If Target.Column > Range("$D$30").Column Then Exit Sub
ActiveSheet.Range("C40").Formula = "=" & Target.Address
End Sub
To explain, this checks the target row and columns and makes sure they are not below or above the range of your choice. If you click anywhere outside of range D10 through D30, then nothing will happen with cell C40.
I have tested this and it works. When clicking within D10 through D30, it changes the contents of C40. Hopefully it works for you.

How to pop up a message in Excel depending on the value of a cell range?

I have variable values in the cells of column A starting with cell A1. I want to write a VB code to popup different message boxes if the value in the targeted cells become equal to 12, and equal to 13, and so. But less than 12 no need a massage box to popup. The cells in the column A has formula which is relevant to the cells in same sheet.
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("A24:A300")) = 13 Then
MsgBox "add 1 into the existing Forfeited PH value?"
End If
If Intersect(Target, Range("A24:A300")) = 14 Then
MsgBox "add 2 into the existing Forfeited PH value?"
End If
End Sub
Ok, So I think I understand your question to be stating that if the cell is changed to anything greater than 12 you need the message box to fire. This would be the code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("A1:A300")) > 12 Then
MsgBox "add " & Target.value - 12 & " into the existing Forfeited PH value?"
End If
End Sub
That is based on the existing message you have in your message box... I'm assuming based on the message that increasing the ph lowers the number so maybe the number is the acidity? Regardless. That should get the result you are looking for. In fact, if you don't want to limit it to 300 rows, you can just put your range as "A:A" and you can select the whole column as your field.
Ok after review of your spreadsheet the issue is you are not actually changing the target so all the target code is not useful ... please use the code below which should apply relative to the sheet you sent me in email. and be sure you of course save the file as an .xlsm file.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo eSub
If (Worksheets("2015").Range(Cells(ActiveCell.Row, 5), Cells(ActiveCell.Row, 5)) - 12 - Worksheets("2015").Range(Cells(ActiveCell.Row, 6), Cells(ActiveCell.Row, 6))) > 0 Then
MsgBox "add " & Worksheets(1).Range(Cells(ActiveCell.Row, 5), Cells(ActiveCell.Row, 5)) - 12 & " into the Forfeited PH value?"
End If
eSub:
End Sub

Using Excel VBA to automatically move to cell if value changes

I have put values in columns from C to F, with G being the calculation from these cells (C to F).
Is there a way to automatically move to cell C in next row when I put the number in cell F at the end?
I have tried using macro plus VBA but it seems there is a problem.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$H$6" Then
Call Macro1
End If
End Sub
MACRO
Sub Macro1()
'
' Macro1 Macro
'
ActiveCell.Offset(1, -3).Range("A1").Select
End Sub
In H6, I used the SUM of the F cell to trigger it but it didn't work.
You are checking only for the cell H6. It will only move if you type something in H6 other cells will have no effect.
So, test for the F column (number 6):
if Target.Column = 6 then
....
endif
Since the acive cell is probably below the one edited (if user pressed enter) or anywhere else (if user just clicked another place), using ActiveCell will be a bad thing, use the changed row instead:
if Target.Column = 6 then
call Macro1(Target.row)
end if
Sub Macro1(row as integer)
Worksheets("TheNameOfTheSheet").Cells(row,3).Select
end sub
This code will only fire if the user changes the value in cell H6. It doesn't fire if that value is changed due to a calculation (ie because the user changes the value in another cell). This is mentioned in the MSDN documentation for the Worksheet Change event at https://msdn.microsoft.com/en-us/library/office/ff839775.aspx.
Also, I would suggest not using ActiveCell.Offset as you don't know which cell will be active when macro1 is run. If the user types a number in F6 and presses enter then F7 becomes the Activecell, but if the user types a number and presses the right arrow key, it will be cell G6.
Instead of using ActiveCell, you can use Target from the Change event, as we know that is the cell that was updated:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$F$6" Then
Macro1 LastCell:=Target
End If
End Sub
Sub Macro1(LastCell As Range)
LastCell.Offset(1, -3).Select
End Sub
If you have more than one row of data and want to repeat the macro for a number of cells in F, you may want to test for a specific Colum (F), and a range of Row numbers, instead of one cell address. There's also no real need to call a separate sub unless you're doing something more complex. You might want to try something like the following:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 6 Then
If Target.Row > 1 And Target.Row < 10 Then
Target.Offset(1, -3).Select
End If
End If
End Sub
(You'd need to adjust the Row numbers according to your needs).

Message Box if cell exceeds another cell

What would be the best way to write a VBA code to have a message box pop up if the value in one cell is less than or greater than another cell - and then display the difference?
Column N contains total appts (manual input)
Column R contains total results (formula generated)
If the cell in column R after calculated is less than or greater than the cell in column N the message box would pop up and say Total results is less than appts by # or Total results is greater than appts by #.
Add the following routine to your required sheet in the VBA project (e.g. Sheet1)
Private Sub Worksheet_Change(ByVal Target As Range)
If Target = Range("N1") Or Target = Range("R1") Then 'Only attempt to run the below code if target is a range you care about
If Range("R1").Value2 <> Range("N1").Value2 Then
MsgBox "Values differ"
End If
End If
End Sub
On the assumption that you want to compare two cells with one another (as opposed to a whole column of cells):
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("N1") > Range("R1") Then
MsgBox "Oops. Results less than Input by " & Abs(Range("N1") - Range("R1"))
End If
If Range("N1") < Range("R1") Then
MsgBox "Oops. Results greater than Input by " & Abs(Range("N1") - Range("R1"))
End If
End Sub
That should achieve the following:
Compare two cells with one another whenever the sheet changes (regardless whether it be the formula generated value for R1, the manual input for N1, or anything else on the sheet)
Identify which is greater
Pop up an appropriate message

Check a range is not the entire sheet?

I have this function which is trying to detect when a particular cell value changes. The problem is, if the user selects the whole spreadsheet and presses delete, I get an overflow in my check that the range is only a single cell:
Public Sub Worksheet_Change(ByVal Target As Range)
'Overflow can occur here if range = whole spreadsheet
If Not IsError(Int(Target.Cells.Count)) Then
If Target.Cells.Count = 1 Then
If Target.Cells.Row = 4 And Target.Cells.Column = 1 Then
Sheets("X").Cells(5, 1).Calculate
End If
End If
End If
End Sub
Is there a more elegant way that I can get this code to only run, when a single particular cell value is changed? (with no overflows, problems when I clear the whole worksheet etc)?
I'm assuming you are on Excel 2007+ since the number of rows and columns increased dramatically in those versions. You might have better luck checking to make sure both the row and column count = 1, since those maxes will be much lower than the product of the two (ie, the cell count):
If Target.Rows.Count = 1 And Target.Columns.Count = 1 Then
Use CountLarge instead of Count
Private Sub Worksheet_SelectionChange(ByVal target As Range)
If target.Cells.CountLarge > 1 Then Exit Sub
'Code...
End Sub
See: MSDN Range.CountLarge Property (Excel)