Auto-update (calculate) the Excel sheet when a cell is changed - vba

I am using Excel 2010 and I want that the Excel sheet will be updated (calculated) automatically when the value of a cell (Range("B7")) is changed (The Range("B7") is a drop-down list). I am trying to use the intersect-target method like:
Private Sub Worksheet_Change(ByVal target As Range)
If Not Intersect(target, Range("B7")) Is Nothing Then
ActiveSheet.Calculate
End If
End Sub
I have two questions about this:
Does it matter if I put this Private Sub under any module? Or am I supposed to write it in the exact sheet under the Microsoft Excel -object menu in VBA?
The second question is how I can make this work? The sheet doesn't update (calculate) automatically when I change the Range("B7"). It only updates when I save the Excel file.

The function should be in the sheet which B7 belongs to, e.g. if the B7 you are updating is in "Sheet1" then the code for the Worksheet_Change event should be under Sheet1 too. You can right click the tab for the sheet, and select show code to get to the right place in VBE.
Excel should be aware that any cell using B7 needs to be recalculated when changed, so that should be handled automatically.

Your code will work if you place it in the sheets event not in Modlue please find the below image for your reference.
Select the respective sheet in VB window and double click on it, it will open the worksheet event window (code window) where you need to copy paste the codes. Now try changing the validation cell it should work :)..

Related

Disable a cell in Excel using VBA

I am trying to do few calculations and update the result of the same in a cell upon clicking the Calculate button (Active X button).
When the Excel sheet is opened, I want few of the cells to be disabled and greyed out. To implement the same, I have written the below lines of code under the Workbook_Open() Sub. All the conditions written under the sub are working fine, except, the statement that I have written to disable the cell (written the locked function to disable the cell).
After some surfing, I came to understand that sheet needs to be protected if a cell needs to be disabled/locked. So, I added an extra line in the code to protect the sheet. However, this stopped the result to be updated in a cell.
Upon clicking the calculate button, excel states that the "Sheet is protected". So, is there anyway to enable/ disable the cell using VB Script and without protecting the sheet please?
Private Sub Workbook_Open()
Dim b1 As Variant
Set b1 = Sheets("Calculation Tool").CommandButton22
b1.Enabled = False
Range("B4:C4") = ""
Range("E4:O4") = ""
Range("E9:F9") = ""
'This is the code written to disable the cells N4 and O4
Sheets("Calculation Tool").Range("N4:O4").Locked = True
Sheets("Calculation Tool").Protect
End Sub
You cannot "disable" a worksheet cell per-se; it's not like a textbox control.
You can, however:
set the cell's fill color to gray (Ctrl+1 to enter Format Cells > Fill tab)
lock the cell to prevent changes (Ctrl+1 to enter Format Cells > Protection tab)
protect the worksheet (Review tab > Protect Sheet) so the cell locking takes effect.
If you need to change the locked cell you can programmatically unprotect the worksheet, make the change, and then re-protect it. Also note that there are several options available when protecting the worksheet.
Alternatively, you could instead use text boxes, and then disable/lock it like you would other controls.
See Also: Lock or unlock specific areas of a protected worksheet
(Based on your "greyed-out" description, I believe you were thinking of textboxes on Microsoft Access Forms.)
try this code for a range:
Range("A1:B2").Locked = True

Activate a worksheet to a specific section

I would like to activate a worksheet so that it always open's looking at specific cells.
The worksheet I have has some other bits out of view that the user doesn't need to see, so I wanted to make sure when the sheet activated it shows the area that the user needs to interact with.
If you need anymore calrification please ask.Thank you
Creating an event within the sheet in question would cause some code to run each time the sheet is activated like so:
Private Sub Worksheet_Activate()
Me.Cells(4, 6).Activate
End Sub
In this case, the cell F4 (4th row, 6th column) would be activated - but you can adjust this as you need.

How do I hotkey current sheet's VBA code?

I've had the issue of Worksheet_SelectionChange(ByVal Target As Range) triggering every time I click on something, which I don't want, as it means I have to wait every time I click in the actual sheet. I changed the code to Worksheet_Open, but that means I have to click the Run button whenever I want the code to run.
So I'm looking for a way of only running the current sheet's VBA code when I want to while I'm looking at the sheet (not while I'm in the VBA Editor). Ideally I'd be able to assign it to a hotkey using Application.OnKey, but I can't figure out how to refer to the sheet's code itself. The only solution I've found refers to an external macro, which isn't helpful, since I'm only wanting to use the code that's already in the sheet, as I'm using different macros for each sheet in the workbook.
Create a macro in a module for which you set the hot key, this macro will call the one you need:
CallByName ActiveSheet, ActiveSheet.Name & "Macro", VbMethod.
So you need to name the macros in the format of SheetNameMacro in each sheet.
More explanation: Trying to call a Sub with a String - VBA
Update
If your sheet names contains spaces then use this:
CallByName ActiveSheet, Replace(ActiveSheet.Name," ","_") & "Macro", VbMethod
and of course also replace spaces in your sheet (e.g. if your sheet called "list to delete" than macro name should be list_to_deleteMacro.

Is it possible to have a cell that has a formula and accepts entry at same time in excel?

Example:
A B
1 =vlookup(XX)
2
3
in cell A1 there is a Vlookup formula, Is it possible to enable user entry in this cell and override the formula then later restore the formula automatically when sheet is open again?
Even through VBA
Short, boring answer: nope.
A cell only ever has a keyed-in value, or a calculated formula. Can't have both.
Longer answer: maybe.
Shift everything 1 row down, and use row 1 to store your "original" formula - then hide that row (and pray the user isn't going to mess with it).
When the sheet is opened again sounds like you're confusing "workbook" and "worksheet" - you need to handle Workbook_Open if you want to run code when a workbook opens. Workbooks contain worksheets - it's the workbook that opens, not the sheets (sheets activate, but I doubt you would want to put that logic in there).
So, in the handler for Workbook_Open, write code that takes the formula in the hidden row and overwrites whatever is under it.
Another solution can be to hard-code the formula in the VBA code.
One possibility would be to store your Workbook as a template. Normally when a user opens the workbook by double-clicking, it will open whole new workbook based on the template, and they can modify it to their heart's content, save it, mail it to Grandma, etc.
The next person who comes along will double-click the template file and get the formula again, just as you designed it.
Short answer: Kind of, sort of
Long answer:
Save your workbook as a template. Every time someone will use it you'll see the orignal with formula, then if someone write over the formula, when using save your original will be kept intact.
What You need to do is:
press Alt + F11
select ThisWorkbook and paste this code:
Private Sub Workbook_Open()
Worksheets("Sheet1").Range("A11").Value = "asdf"
End Sub
Every time the workbook is opened, this script will run.
Instead of "Sheet1" you can write the name of the sheet you want to apply the script.
Inside the Range, You can define the cells you want to modify, You can use even multiple cells. Check this for more information about this.
After Value You can write what You want to be written inside the cell. You can write "=vlookup(XX)" and it will work.

How to highlight a cell when formula result from another sheet changes?

This is one that's been killing me and I've tried almost every solution on the Internet.
Here's background. I have an HR model that has each department broken out on separate tabs. I want to run an extract from our payroll system each payroll run and send highlight any updates individually. If someone's title or salary or status changes, I want to have that called out by highlighting the cell.
Each tab uses an INDEX/MATCH lookup to the extract tab to pull in the current information. What I want is if any value changes or is new(new hire, for example), highlight the cells.
I've played with Worksheet_Calculate and Worksheet_Change to no avail. Worksheet_Change doesn't fire because I'm not making the change directly on the sheet and Worksheet_Calculate doesn't have the Target object for to reference. I've tried the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim updatedCell As Range
Set updatedCell = Range(Target.Dependents.Address)
If Not Intersect(updatedCell, Range("A:A")) Is Nothing Then
updatedCell.Interior.ColorIndex = 3
End If
End Sub
The range I actually need evaluated is A7:R104 but I've been trying to get anything to work when linked to another sheet.
This works fine if formula of target cell is pointing to another cell on same sheet. The moment you point to one on another sheet it doesn't work. I've tried most of the solutions on here with no success. I've even tried putting the Worksheet_Change on the extract sheet and see if I can trigger it that way with no luck.
Is there a recommended solution to triggering a change to a cell for a formula linked to another sheet?
so I just saw this post, I don't know if you've found the solution or are still looking, but:
if you select a cell in sheet 3, you can then go to the home tab, go to "conditional formatting" -highlight cell rules - more rules (at the bottom) - and "use formulas to determine which cells to format" and then put your cursor in the formula box. now, select a cell in sheet 1 (click the sheet1 tab, and click a cell) and you'll notice it should populate the address for sheet1, and the cell u selected. now type <> after that cells address, then select sheet2 and a cell. then click format, and choose a fill color. then ok. if you go to conditional formatting and manage rules it will show there the rule / formula and which cells it applies to.
doing this i was able to select cell D10 in sheet 3, and make it an ugly green if cells in sheet1 and 2 didnt match (I picked which cells) you can also select a range of cells.
thusly, you can apply this rule to whatever dells you want, and if you record a macro of you setting this conditional formatting, you can manitpulate that macro to apply it to a bunch of different cells, and change the ranges. (using loops / variables)