Running a VBA code to run automated emails based on date. If the date equals today then the value becomes "100" which should trigger the email macro.
I only have this set up to run based off cell value which is the reason for the IF cell = today then "100".
The issue I believe is that the VBA code is looking at the formula and not the value so it is not being triggered. This only works if I manually input the number 100 and not when the formula runs its calculation.
Any help is much appreciated!
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Value < 1 Then Exit Sub
If Not Application.Intersect(Range("I36:I44"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value = 100 Then
Call Mail_small_Text_Outlook
End If
End If
End Sub
Using the Worksheet_Calculate() event procedure
Private Sub Worksheet_Calculate()
If WorksheetFunction.CountIf(Range("I36:I44"), 100) Then
Call Mail_small_Text_Outlook
End If
End Sub
as this runs whenever the calculated result of a formula on the sheet changes.
Related
I want to change the formatting of a cell if the value changes. I just need it to be shaded in.
I'm trying to use the following:
Private Sub Worksheet_Change(ByVal Target As Range)
Target.Interior.ColorIndex = 45
End Sub
However this is far too sensitive. For example if i delete a row, the entire row changes colour as technically it's a change. Even if I go into a cell, make no change and come out of it, it changes the formatting.
Is there anyway to edit the above so it only formats the cell's when the value has changed?
Thanks
I think you could use below code as a base for your further logic.
The idea is: we need to remember value before it is changed, so best to read it when some range is selected (since you are talking about single cell, I didn't consider ranges consisting of more cells). The value would be stored in global variable ValueOnEnter.
When the change is finally made, we compare values "before" and after", and if they differ, change the color.
Public ValueOnEnter As String
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Value <> ValueOnEnter Then
Target.Interior.ColorIndex = 45
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
ValueOnEnter = Target.Value
End Sub
I have a macro that displays a calendar when a cell is clicked. My problem is that I can't run the same macro when this cell is merged with other one. I tried makinkg the selection individual for each cell or making a range of cells. Neither of the two options worked.
My question is how to code merged cells in vba in order to run a macro.
My code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("K12")) Is Nothing Then
Call SHOWCALENDAR
End If
End If
End Sub
Selection.count will return a count of all the cells in the merged cell, so it will be greater than 1 for a merged cell. Remove that count condition and the code will run fine.
Merged cells cause all kinds of headaches and should be avoided. If the cells are merged across several columns, consider using the formatting setting "center across selection" instead.
Or you can simply try this...
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells(1).Address(0, 0) = "K12" Then
Call SHOWCALENDAR
End If
End Sub
I want to calculate a price based on a proposed discount % or calculate a discount based on a proposed price.
I need users to be able to use either parameter and adjust back and forth to determine their ideal price.
I have coded the formulas so that nothing is overwritten by the user but have a problem as the 'value if false' element of the "IF" formula is dependent on a zero value in one column and so resets if the user doesn't manually adjust this before inputting a figure into another. This results in the new figure being reset.
Ideally I would like the sheet to reset the value in column "Proposed Promo POR%" to zero if a value is input in "Promo Price per Unit". Is this possible?
This is the code I have at the moment.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect(Target, Range("b21:b26")) Is Nothing Then Exit Sub
Application.EnableEvents = False
ActiveSheet.Range("$i21:$i26").Formula ="=IF(Proposal!$b21>Formulae!$a$5,Proposal!$g21-(Proposal!$g21*Proposal!$b21), (Proposal!$g21-Proposal!$i21)/Proposal!$g21)"
Application.EnableEvents = True
End Sub
For clarity Formulae!$a$5 is zero and it is column $i21:$i26 that the user will enter a price in if they want to. If they do enter something here then column b should reset to zero.
This is a screenshot of the columns and headers in my sheet
The Worksheet_Change event will help you. Worksheet_SelectionChange fires each time a user changes the active cell, whereas Worksheet_Change fires anytime a user makes an edit to any cell(s).
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("$i21:$i26"")) Is Nothing _
And Target.Rows.Count = 1 and Target.Columns.Count = 1 Then
'assumes event will fire only on changing one cell at a time
Application.EnableEvents = False
Target.Offset(0,-7).Value = 0
Application.EnableEvents = True
End If
End Sub
I have been trying to work this out myself for the last few days and caught myself in a bit of a one step forward three steps back cycle. I've been reluctant to bother you thinking this would have been answered somewhere else before now.
The idea is that I have a spreadsheet that has criteria in rows with separate entries in rows; in row 6 it is the status of each column entry, which when changed to "Completed" I would like the column to be hidden.
I've been floundering around with Worksheet_Change and been able to hide specific columns, but not the active column.
Any help offered would be much appreciated and I'm sorry if this has been covered elsewhere, but I've not been able to successfully apply any examples out there.
Thanks.
Whenever you have to work with worksheet_change events, you have to consider a cycle for it, due to user may delete multiple data at the same time or do a copy paste, if you only consider "Target" It would give a debugger error.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ItemMultipleData As Range
For Each ItemMultipleData In Target 'handles multiple cells, paste, del, etc
'your code (instead of using "Target" change to ItemMultipleData. IE:
'If ItemMultipleData.Value = "Completed" Then
Next ItemMultipleData
End Sub
Here is a starting point. It only checks row # 6:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Range("6:6")
If Intersect(Target, rng) Is Nothing Then Exit Sub
If Target.Count <> 1 Then Exit Sub
If Target.Value = "Completed" Then
Application.EnableEvents = False
Target.EntireColumn.Hidden = True
Application.EnableEvents = True
End If
End Sub
EDIT#1:
This approach assumes that only one cell at a time is being changed........that makes it easy to "find the active cell"
I have 2 Sheets in a workbook, Sheet 1 contains permanent data, Sheet2 has information coming in using a DDE add-in.(esignal).
The data has Columns: Date, Time, Price
The time interval is 5 min.(Others are also possible but this is the one I am using for analysis)
The DDE gives the data from say row: 1 to 20. The value of the last row gets deleted once a new time period gets over i.e. if 7:00 PM is the top row(Row 1) "Time" then soon as time becomes :7:00:01, the 7:00PM row will become(Row2), Row1 will become 7:05 PM.
And the last row(20) will be deleted and replaced with Row 19 data.
Assume the last time in Sheet1 is 6:55 PM, I want to get the final value for time 7:00, i.e. as soon as time moves to 7:00:01, from Sheet2(where DDE is giving the values) to Sheet1 below 6:55PM.
I wonder what would be the right way to do this, is VBA the correct language to learn to do this in the future? I am new and still in the learning process.
Tried the following(changes):
Sub Activate_timer()
Application.OnTime Now + TimeValue("00:05:00"), "MoveData"
End Sub
Sub MoveData()
Set mainworkBook = ThisWorkbook
mainworkBook.Sheets("Sheet2").Rows(3).EntireRow.Copy
mainworkBook.Sheets("Sheet1").Range("A14340").Select
mainworkBook.Sheets("Sheet1").Paste
Call Activate_timer
End Sub
Location in Under Modules Folder Module1
You can use the worksheet events for this purpose. For example if you add this function to the VBA sheet, it will be called whenever the data changes on the sheet:
Private Sub Worksheet_Change(ByVal Target As Range)
' WHERE Tatget is a Range in which the change occurred
End Sub
So in this function you can handle changes of cells and find the desired change checking exactly what range on the worksheet changed. Is it right direction to help you?
For example this handler displays the modified range address. You can check whether it satisfies some conditions and if so, perform the necessary actions.
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox Target.address
If (IfSatisfySomeConditions(Target.address)) Then
'do something
End If
End Sub
Function IfSatisfySomeConditions(address As String) As Boolean
'check condition and return result'
IfSatisfySomeConditions = True
End Function
another approach is to use timer:
Sub Activate_timer()
Application.OnTime Now + TimeValue("00:05:00"), "doSomething"
End Sub
Sub doSomething()
'do something
Call Activate_timer
End Sub
this code trigger doSomething sub every 5 minutes