I have a worksheet with two tabs and wanted to have a VBA to run automatically and pop up a window message saying "NPV negative! Please enter future Savings!" anytime cell J27 (in both tabs) calculates negative value.
I have the below code in the Module and when I test and click run it does what I wanted to do but in reality when I change information and cell J27 is a negative number nothing happens. I'm very new with VBA so any help is appreciated.
Private Sub Worksheet_Calculate()
For Each cell In Range("J27")
If cell.Value < 0 Then MsgBox ("NPV negative! Please enter future Savings!"), , "Invalid Entry"
Next cell
End Sub
Thank you!
The Worksheet_Calculate subroutine is only going to be triggered when the worksheet that you created it in is recalculated. Range("J27") is only looking at the cell in that worksheet, so your loop only iterates one time.
To get the value in cell J27 from both worksheets, you need to specify the sheet and the range for each one.
Private Sub Worksheet_Calculate()
If Sheets("This Sheet").Range("J27").Value < 0 And _
Sheets("That Sheet").Range("J27").Value < 0 Then
MsgBox ("NPV negative! Please enter future Savings!"), , "Invalid Entry"
End If
End Sub
Put that in both worksheets to get it to run when either worksheet is recalculated. (Or, better yet, save it in a separate Module and call it from both Worksheet_Calculate subroutines.)
Related
I want to put up a validation that should not allow percentage sign in excel sheet. I tried the data validation option to select only decimals, but still when I put the percent sign at the end of the number, excel considers this. Please help
try this in the Data-Validation as Formula:
=LEFT(CELL("format",A1),1)<>"P"
Best regards chris
Maybe this VBA-solution can help:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
For Each cell In Target
If InStr(cell.Text, "%") > 0 Then
cell.Value = ""
MsgBox "It's not allowed to insert a %-sign", vbInformation
End If
Next
End Sub
This procedure will validate evry input and if a %-sign is inside, it will throw an message box with an error message.
If you are not familiar with VBA: You have to copy this procedute into the code-area of the sheet. Therefore open the Visual Basic Editor. On the left side you will find a list with your sheets. Double click on the sheet where you want to validate for a %-sign and copy the procedure into the code-area.
Function bd()
Sheets("sheet1").Range("A1").value=1
End Function
This is my function.
Why is it that when i enter =bd() into any cell in sheet1, the data in A1 does not change to 1? I do not want to use the button to change the value.
Why it does not work:
If you're calling your function from an Excel formula, your function becomes a User-Defined-Function (=UDF).
UDF have to follow special rules - the main one being that it cannot by any means change anything in Excel - it shall only return a value! Your function clearly violates this rules.
(For reference: the other main restriction is that it shall also not access any data outside the parameters that were passed to it when calling the function. Thus, you cannot use MyUDF=Range("A1").Value - but only something like MyUDF=rngParam1.Value*2!
Alternative approach:
If you want to change the worksheet but don't want to use a button, you need to think of some kind of trigger event. Look at the possible Worksheet and workbook events - you'll find a list of events here - and detailed instruction how to use them here.
For instance, you could use the Activate of Sheet1. For this place this code in the Sheet1 code module:
Private Sub Worksheet_Activate()
Range("A1").Value = 1
End Sub
Now every time that sheet1 gets activated, A1 will be reset!
Try This
Copy this code on your 'Thisworkbook' on vba
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If ActiveSheet.Range("A1").Value = "bd()" Then
Sheets("sheet1").Range("A1").Value = 1
End If
End Sub
Now if you enter 'bd()' (without Quotes) on cell A1 on Any Sheet and press enter the cell A1 of Sheet one will Change to 1
OR
Copy this code on your 'Thisworkbook' on vba
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Sheets("sheet1").Range("A1").Value = "bd()" Then
Sheets("sheet1").Range("A1").Value = 1
End If
End Sub
Now if you enter 'bd()' (without Quotes) on cell A1 on Sheet one and press enter the cell A1 of Sheet one will Auto Change to 1
Hope this works
I want to be able to run macros based on the outcome of the cell, for example.
If A1 is 1111100 then run X macro If its 1000000 then run this macro etc. I have had a look at "Case Select" but my lack of knowledge in this matter makes me thing that might not be what I want.
Any ideas? :/
Thank you in advanced.
JB
you can combine the two types, and yes, a Case Select is the easiest to read and maintain.
Here's example code that runs different routines depending on what is in A1:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("A1"), Target) Is Nothing Then
Application.EnableEvents = False
Select Case Target.Value
Case "A"
FunctionWhenA
Case 1
ThasIsAnotherFunction
End Select
Application.EnableEvents = True
End If
End Sub
note that I also disable/enable events so this isn't triggered every time a cell is changed
There are two main types of Excel macros
1- Those that are written to perform actions, change data, etc.
2- And those that are written to perform calculations and return values to certain cells
(used as customized formulas that are not built in to excel)
The first type can only be triggered to start execution by clicking a button on a form, invoking the Macros window within Excel and selecting the name of the macro to be run
The second type can be run as soon as a value of a certain cell changes (that cell must be an input for the said macro function to work with, calculate and return a certain output), and thus the returned value will be stored on another cell.
In the second type, Excel will ignore any code that tries to modify the content of other cells, perform actions on the worksheet, workbook, or any other action that is not limited to the cell contanining the formula for the custom macro.
If you intend to run a macro of the first type, and want it to be executed right after a certain value changes, then that is not possible.
If you want to write a macro of the second type, then that is possible but code will only be limited to a single cell only.
Yes would need to loop through them.
You could replace the "cas" subs in following with your implementation of VBA for that case.
Function Strange(myVal)
Select Case myVal
Case 1
Cas1
Case 2
Cas2
Case 3
Cas3
End Select
End Function
Sub Cas1()
MsgBox ("hi")
End Sub
Sub Cas2()
MsgBox ("ha")
End Sub
Sub Cas3()
MsgBox ("ho")
End Sub
Sub LoopThem()
Do While ActiveCell.Value <> ""
Strange (ActiveCell.Value)
ActiveCell.Offset(1, 0).Activate
Loop
End Sub
So cell A1 to A3 with values 1,2,3 would consecutively pop up msgboxes "hi" "ha" "ho".
Fixed it.
Done this via using Intergers to stop when I have no further cells with data to stop the macro, and detect on many '1's are within the code and copy the data as neededs using "For" commands.
I'm trying to imlpement a code that displays a message when a certain condition is met. In this case, it should happen when Sheet2's A1's value is equal or bigger than 1000. This value, on the other hand, is defined by a formula located in Sheet1. I tried implementing a solution based on this thread: How can I run a VBA code each time a cell get is value changed by a formula?
So I got this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim updatedCell As Range
Set updatedCell = Range("A1")
If Not Application.Intersect(updatedCell, Range("A:A")) Is Nothing Then
If updatedCell.Value >= 1000 Then
MsgBox "Something requires attention"
End If
End If
End Sub
When I change the value of A1 through something from Sheet2, it works, but if for example I define it as =Sheet1!A7, and change Sheet1's A7, nothing happens.
How could I make it work?
Well, the linked thread deals with the problem that you want to find out the cell that is recalculated by the current change. (Anyway, the Dependents method only works for formula on the active sheet so this would not work across sheets).
In your case, you already know that you only want to monitor one specific (formula) cell.
So, I'd simply go with this:
Put this code in sheet 1 if you know that Sheet2!A1 only depends on values on sheet1.
Just catch all changes and look at your cell each time:
Private Sub Worksheet_Change(ByVal Target As Range)
If Worksheets("Table2").Range("A1").Value >= 1000 Then
MsgBox "Something requires attention"
End If
End Sub
Make sure that you use Worksheets(...).Range - a blank Range can be the source of sleepless nights of error-hunting, it refers to the worksheet where the code is located if the code is in a worksheet module and to the active sheet if it is in another code module.
Hello: I have a set of cells on a worksheet called "Docs". The cell range is (B13:C23).
When users get taken to this page, they are meant to fill out each of these cells with a value from 0 through 6. My Question: Is there some code that I can attach to this sheet where, if a user does not fill in a cell with anything (ie. leaves it blank) and tries to leave the sheet or close the workbook, they are somehow reminded to fill it in? Or is there a way to not let them leave the sheet until it's completed? Thanks.. Allan
You can attach a macro to the change event of the form. Excel comes with built in validation but it does not work that well. For instance if someone pastes a value into the cell it does not validate what is pasted.
Start by creating a range by selecting the range of cells to be validated, right click and select "Name a Range". Note that I am testing this with Excel 2007. Say you call your range "InputRange".
Then open the VBA editor and create a procedure for the change event.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim vrange As Range, cell As Range
Set vrange = Range("InputRange")
If Intersect(vrange, Target) Is Nothing Then Exit Sub
For Each cell In Intersect(vrange, Target)
If cell.Value < 1 Or cell.Value > 6 Then
MsgBox "Invalid Entry", vbCritical
Application.EnableEvents = False
cell.ClearContents
cell.Activate
Application.EnableEvents = True
End If
Next cell
End Sub
Note you can attach to any event that suits you.
You could give these cells conditional formatting, making them red if empty.
Try writing a vba macro. Alt + F11 opens the VB Editor. Check out this SO post for VBA tutorials.
There are worksheet and workbook events that you can use. For example, Workbook_BeforeClose or Workbook_SheetChange. If you create methods for those events you can put code inside that checks that the required cells are filled.