Run Macro When Cell Programatically Changes - excel

I filter data into excel from an external financial software. It operates in real time and I want it when say cell B4 changes programatically by the real time financial software that Macro 1 will run. A few issues to be aware of are, the real time data may change in milliseconds.
If this is impossible then as a back up I would like to be able to copy and paste that Cell B4 every time it changes to say B10 then once it changes again put the new value in B10 and have the old value in B10 shift down to B11, then when B4 changes again have that value copied into B10 and shift down the previous two values so that the newest data is always in B10.

Setup an onWorksheet_Change event, this should run everytime the worksheet changes.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$4" Then
'Place appropriate code here
End If
End Sub
I don't know what would occur if another update occurred while this was running though.

If your cell value change every milisecond, you may have performance issues (or loops) with a vba event macro like Craig T showed.
You should probably add some checks on the update frequency and if you need to disable vba event, use :
Application.EnableEvents = False
' Coding to skip these events
Application.EnableEvents = True
Regards,
Max

Related

Excel formula not calculating automatically unless press enter for some cells

I have over 3000 cells and about 300 cells where the formula is not calculating (some cells have less than 100 character, most have more than 500 characters that are not calculating).
Can someone help me with this one of explain if it is not possible?
Thank you so much!
I am creating a file to capture the local language translation of each sentences from another sheet. I use the Xlookup as some sentences contains more than 500 characters. The problem is there are some cells that are not capturing the translation/ the formula for that cell is not automatically calculating unless i click the cell and press enter.
I already read some possible issue like the automatic calculation should be ticked, highlight all and replace "=" with "=" to recalculate the cells, used VBA code to auto/force calculate, update to text to columns to update the formatting of cells to general and not as "text". All of these are not working as more than 2k of the cells are working but 300 of them needs to be manually recalculated.
As you mention having a macro running, you might consider stopping the calculations at the beginning of the macro, and restarting it once your macro has finished, something like:
Sub macro()
Application.Calculation = xlManual ' Turn Automatic Calculations Off
...
Application.Calculation = xlAutomatic ' Turn Automatic Calculations Back On
End Sub

All Excel Formulas Updating at Once

I am trying to use to use the following formula in an excel document: =IF(C2="","",NOW()). I'm just trynig to capture the time that a cell is populated. The problem I am running into is that everytime one formula updates, they all update.
For example, if I have the two formuals:
=IF(C2="","",NOW())
=IF(C3="","",NOW())
If I update C3, the formula that relates to C2 also upates. So my times are always identical to the last updated cell. I can't get them to update when only there dependent cells update.
One other thing of note, this spreadsheet is being used in MS Teams, so macros are not an option.
Is there a setting I have incorrect potentially? Or a work around?
Thanks in advance!
Have you looked into Office Scripts? They work a lot like macros in VBA, but they are for Excel Online.
https://learn.microsoft.com/en-us/office/dev/scripts/overview/excel
The behavior is expected since =NEW() always returns the current time, and you have no control of when a sheet might recalculate.
The best way to handle this situation is to have a macro that runs when the worksheet updates and it manually writes a value into the desired cell from the current time.
For example if the values to be tracked are in column "C" (the 3rd column) then the code below will write the current time of the adjacent cell (one column over) every time that value changes.
Place the code below under the worksheet
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 3 Then
If IsEmpty(Target.Offset(0, 1)) Then
Target.Offset(0, 1).Value = Now()
End If
End If
End Sub
The code above checks to see if a timestamp is already filled before setting it.
Try this:
=IF(CELL("row")=ROW(C2), NOW(), C2)
Drag down your C column as needed.
NOTE: You'll need to enable iterative calculations for formulas (Options -> Formulas).

Excel named range formula, for cell sequence

I'm trying to set up a sheet that will input data in a specific cell order. Basically I need the first enter to go to the next column, and then the second enter to go to the previous column but one row down.
I linked a picture to elaborate. I thought it would be possible with just defining a named range and selecting each cell individually, but I need for this to work for 500+ rows.
It's my understanding you can use a formula to define a named range, but I'm having difficulty figuring out how to do that.
edit:
More elaboration on the input method. In this case I can only use the enter key due to the input device. This sheet also has to be shared with people, so setup instructions are not ideal.
Based on the question as it stands (and without further qualification), the following code will do what you want:
Option Explicit
Private Sub Worksheet_Change(ByVal rgTarget As Range)
With rgTarget
Select Case .Column
Case 1: .Offset(0, 1).Select
Case 2: .Offset(1, -1).Select
End Select
End With
End Sub
The above goes into the VB of the Sheet where the data entry will occur.
If you don't know what that means, let me know and I'll provide details on how to implement.
Excel recognises data entry patterns when data is entered manually.
enter something into A1 and press Tab to confirm and go to B1
enter something into B1 and press Enter.
now the active cell is A2
enter into A2 and press Tab to go to B2
enter into B2 and press Enter to go to A3
etc.
No need to set up anything special or create named ranges for that.

reference a cell but keep formatting of text from source cell

I have a cell with some text in it.
Say cell A1.
This text is formatted in a certain way - First few words are bold, line breaks, varying font size, etc
When I reference this cell, say in cell B1:
=A1
In B1 I just get a long string of text that has none of the formatting that is present on A1
Is there a way to reference and keep the formatting?
I can use format painter and it will recognise the line breaks within the cell, but aspects like the partially bold writing are still not recognised.
As per my comment:
Private changing As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Target.Address = [A1].Address Or changing Then Exit Sub
changing = True
[A1].Copy [B1]
changing = False
End Sub
The code above means that whenever any change is made to cell A1 (specifically A1 - that way the code doesn't execute every time a change is made on your sheet - doing that would slow everything down), whatever's in A1 is copied to B2
Usage
To use, simply
Right click on the name of your sheet (a tab along the bottom), and select "view code"
Paste the code in
Change any cell references to the ones you want (ie replace[A1] with [A3] or [A1:A4] or [blah] (i.e. a named cell/range) depending on what you need)
Close the window. To save the workbook you'll have to save as .xlsm or .xlb if you want to keep the macro
Notes
[A1] is shorthand for ThisWorkbook.ActiveSheet.Range ("A1"). Typically I would advise against using it as ActiveSheet means that if the code ran on any sheet in ThisWorkbook, it would copy and paste over the A1 and B1 of that sheet, whereas you probably only want the specific cells on a specific sheet.
However in this case, the code only applies to 1 sheet so that's not a problem.
All the changing stuff is necessary because copy/paste triggers a change event; i.e. the macro triggers itself over and over until Excel stops it - not ideal! The changing variable which I declare simply acts as a signal to stop the program executing itself.

VBA trigger to hard code cells

I have setup a control worksheet. In Column J I have a drop down list - If "Y" is select then it triggers a UDF in column I to apply a signature (sign off). I'm looking for a trigger to hard code to the whole row after the UDF runs. So if "Y" in selected in any cell in column J then hard code protect/lock that entire row.
Looking for tips on how to handle/go about setting up the trigger - I think I should be able to setup the locking/protecting. Plus I presume a time delay will allow the UDF to run before locking the row?
All advice welcome,
Thank you,
Ciaran
Create a worksheet change event. Any time a change in column J is detected, the code will run.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 10 Then
'insert your code here
End If
End Sub
Use Target.Address for the exact cell reference. You could even incorporate your UDF code in here instead of running a separate process.

Resources