VBA trigger to hard code cells - excel

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.

Related

How to have excel automatically replace one word with another word?

I'm having trouble finding my answer for this elsewhere. I need to be able to type in a code (ex. CK) and have the cell show "Checking Account" when I press enter. Or SV for Savings Account, RI for Roth IRA, etc. Here is what I have tried:
Conditional Formatting>New Rule>Use Formula> =$C$25="CK"
Then the format I input was Number>Custom> "Checking Account"
No matter what I do, it stays "CK." I'm having a hard time figuring this out. Any suggestions are appreciated. Thanks!
=if(A1=“CK”,”Checking Account”)
Replace A1 with the cell range you want CK to be replaced with Checking Account.
If it’s a range input like A1:A5 or A1:E1
Easiest I could come up with as a suggestion.
What you are trying to do, is impossible with an Excel formula: an Excel formula is present in a cell and calculates the value, based mostly from information from another cell.
You are looking for a macro, and I would advise you to check for the Worksheet_Change event, something like:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Value = "CK" Then
Target.Value = "Checking Account"
End If
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).

How can I adjust the row height of merged cells using a macro in vba excel?

I made a excel sheet, which serves as a questionnaire. To allow extensive answes and adjust the design, I have merged cells. The problem I encounter now is that the row height does not adjust automatically. I have therefore written a short macro to solve this issue. However, this macro only works on single cells and does not work on the merged cells. Therefore, I would like to know how I can solve this. How can I adjust the row height of merged cells using a macro in vba excel?
Thank you for your help!
As always, it would be helpful if you posted your macro, but I am guessing you're trying to use the .entirerow.autofit command? If so, I don't believe this function evaluates a merged cell. (Someone correct me if I'm wrong!)
You might want to consider using "Center Across Range" instead of merging cells. Or you might want to create a rule to loop through cells to check character length or if the "enter key" CHR(10) was entered, and size based on that. Example:
Sub TestEachCl()
Dim CL As Range
'Make sure to include the intersect with usedrange,
'otherwise this will take much longer than necessary.
For Each CL In Intersect(Rows(20), ActiveSheet.UsedRange).Cells
If InStr(1, CL.Formula, Chr(10)) > 0 Then
'enter key exists in a cell
CL.EntireRow.Height = 30
'probably want to close the loop or have it not do
'anything else once it has reached max height,
'so it doesn't reduce the size
End If
Next CL
End Sub

Is there a way to automatically fill cells in a row when it is added in Excel

I don't really know much about excel especially VBA. But i need to write something(macro for ex.) that does the following:
if someone inserts a row and fills the first 4 cells of it, it needs to sort the rows (i have a macro for sorting called "sortingmacro") and then it needs to fill cells 7 and 8 by copying the cells above them.
for example if i insert a row to 4th line and fill A4,B4,C4 and D4 it needs to copy G3 to G4 and H3 to H4. (after sorting of course)
any help is appreciated.
edit:
i have formulas in the cells that are to be copied. the formulas for the first row(the top one according to sorting) is a different one and all the others are the same.
You can find out if a cell has been changed with the Worksheet_Change-Event.
Private Sub Worksheet_Change(ByVal Target As Range)
Debug.Print Target.Address
End Sub
Output (something like this):
$A$14
$B$17
$C$13
$C$21
$C$16
So if a matching cell is changed, look for the other 3 in the row and start your macro.
So put a button on the page, then go to the code for it (the click event). In that, use your sortingmacro to sort all the rows. Then you just do something simple like:
Rows(4).Cells(1,7).Formula = Rows(3).Cells(1,7).Formula
Rows(4).Cells(1,8).Formula = Rows(3).Cells(1,8).Formula
Of course instead of hardcoding 4 in there, you'd have a variable for the row (make sure and determine it after sorting).

Run Macro When Cell Programatically Changes

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

Resources