I'm trying to add a formula to a specific cell that says "Enter user data then add 20" Ex: If a user enters 10 into a cell when they press enter the cell returns 30.
I've tried a formula that says, =this cell (plus this cell plus 20) but can't have a formula and user input in the same cell.
Create a named range on the cell that you are wanting to add 10 to, I called that range rngCellToAdd10To ...
Then add the below code (using the VBA editor) to the worksheet object that the cell you want to monitor changes for ...
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Intersect(Target, Range("rngCellToAdd10To")) Then
Application.EnableEvents = False
Range("rngCellToAdd10To").Value = Range("rngCellToAdd10To").Value + 10
Application.EnableEvents = True
End If
End Sub
... then add a number to the cell and watch it go.
If you're not sure how to add code into the VBA editor, I suggest Googling it.
Related
I am trying to keep track of changes (to be appeared in a chart) upon every changes made in the worksheet using this reference (codes are copied below as example reference).
The changes in the worksheet actually can be made by several List Boxes (form Control Menu) and other cells e.g. Input 3 and 4. Let's say the resulting value caused by any change appears in the cell G14, and as the example code I want to keep them in I14 (as in the example image).
The formula might be e.g. D14 + E14 + K14 + L14 where K14 and L14 are values linked to the ListBoxes.
Selections in Input 3 and 4 trigger Worksheet_Change but selections in List Box apparently don't, shall I add a macro for List Box callback in which Worksheet_Change gets triggered?
Apart from that, if there is any better saving the results method, please mention it.
Many thanks in advance!
Dim xVal As String
Private Sub Worksheet_Change(ByVal Target As Range)
Static xCount As Integer
Application.EnableEvents = False
If Target.Address = Range("G14").Address Then
Range("I14").Offset(xCount, 0).Value = xVal
xCount = xCount + 1
Else
If xVal <> Range("G14").Value Then
Range("I14").Offset(xCount, 0).Value = xVal
xCount = xCount + 1
End If
End If
Application.EnableEvents = True
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
xVal = Range("G14").Value
End Sub
As you are asking about trigger event to run a macro on selection of listbox value (form control), I believe this could be considered as answer. I am sure someone with better knowledge will improve it.
Worksheet_Change event occurs when a cell or range of cells is changed manually (not by any auto process like calculation or selection of listbox value)
There can be different scenarios
First
There is no link between calculation of cells affected by the listbox change. Say, in First ListBox, you select 10, and cell A2 is updated to 10 and cell A3 is calculated value changed to 30. Then You select in second listbox 15 and cell B2 is changed to 15 and cell B3 is calculated independently if A2 and A3 to 90.In this case these are independent listboxes and their results. So, in this case, you can assign macro to each listbox which will run every time you change value in each listbox.
Second
Dependent calculations: Say the desired result for tracking is in D3 which will be calculated only on selection of four listbox values in A2, B2, C2 and D2. In this case you will not like to run macro for every list change but only after selecting values in all listboxes and calculation of D3. So in that case, instead of assigning macros to all the listboxes you could assign it only to last listbox.
Third
By now you must have understood there are two events taking place. One is selection of listbox value and second is calculation. In the second scenario, if you want to run macro for every change in calculation, say when you select A2 and when you select B2, etc. then you can simply use calculation event instead of assigning macro to every listbox. It will run everytime when a value is changed causing worksheet to calculate.
For assigning macro to listbox (form control) --- You can directly assign a macro to the list box.. First create a macro in VBA . Then Just right click on the list box and click assign macro. then select a macro to be assigned. .. The macro will be run when you click the listbox to change the value
Also, as you want to track the result calculated with macro, you need the sheet to be calculated first. Start the macro with Worksheet.Calculate method to be safe (in case formula results are not updated for some reason).
Tahnks to #Naresh, solved the problem in the following way, any improvement editis more than welcome since I know the codes might seem inefficient!
Dim xVal As String
Public Function customRecorder(Target As String)
Static xCount As Integer
Application.EnableEvents = False
Range("I14").Offset(xCount, 0).Value = xVal
xCount = xCount + 1
Application.EnableEvents = True
End Function
Private Sub Worksheet_Calculate()
xVal = Range("G14").Value
customRecorder (Range("G14"))
End Sub
I'm running into an issue where I've set up Data Validation for characters that = 10 in a specific column in Excel. My issue is, if a user pastes over the cell, the data validation gets overwritten. Is there a way to accomplish a text limit by using VBA, that'll alert the user to only have 10 characters in a cell within a defined range? One that won't get overwritten if a user pastes over a cell?
Paste this into sheet's module:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
For Each cell In [E14:E1000]
If Len(cell) <> 10 Then
MsgBox "Text length in cell """ & cell.Address(0, 0) & """ does not equal to 10.", vbExclamation
Application.Undo
Exit For
End If
Next
End Sub
Explanation
When you paste text onto sheet, the procedure scans each cell in a range. As soon as it finds text with length more than 10, it notifies user which cell breaks the limit and undoes all the changes with Application.Undo. Then it immediately exits.
I just want to know the right syntax for this IF statement.
I just want to set a specific value to a cell.
My syntax is:
=IF(HOUR(F2)>6,F2=6)
Correct me if I miss something.
You could perhaps use the Data Validation feature of Excel. For example, in the below scenario, I'm putting this data validation on cell B2. So I select it first and go to Data > Data Validation:
Then I pick some options:
A user will be able to enter only a value between 0 and 6 inclusive.
A macro that could do that would be:
To use this macro, right click on the sheet tab and click on 'View Code'. Copy and paste the below, run (play button) and save.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$F$2" Then
If Target.Value > TimeValue("06:00") Then
Target.Value = TimeValue("06:00")
End If
End If
End Sub
Whenever a time above 6:00 am will be entered, the macro will automatically change it to 6:00 am. Formatting of the cell may be required.
I have 5,000 part numbers contained on one sheet of an Excel workbook (Part Numbers), ranging from cell A1:A5000. I want to find a way to click on any one of those part numbers and have it automatically populate into cell D7 on another sheet of the same workbook (Price Sheet). What is the easiest way to accomplish this task?
To do it that way, you will have to write VBA code to catch every SheetSelectionChange event to see if the new selection range is in your cells A1:A5000. And then if it is, execute the VBA code to fill OtherSheet!D7.
If I recall correctly, the VBA code to do this would look something like this:
Private Sub WorkSheet_SelectionChange(ByVal Target As Range)
'Check if it is in the range A1:A5000
If Target.Column = 1 And Target.Row <= 5000 Then
'get the target worksheet
Dim TargetWS As Worksheet
Set TargetWS = Sheets("OtherSheetsName")
'copy the value of the just selected cell to D7 on the target WS
TargetWS.Cells(7, 4).Value = Target.Value
End If
End Sub
(Oops, forgot about the need for "SET" in VBA.)
You can do this without VBA:
Select the partnumbers A1:A5000 and type PartNumbers in the Name Box (to the left of the formula bar) and press carriage return (OartNumbers should now be visible in the Name Box whenever you select a1:a5000
Now go to cell D7 on Price Sheet, and use Data Validation-->List and enter =PartNumbers in the Source box
Now you can select any of the 5000 part numbers from the dropdown in cell D7
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.