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

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)

Related

VBA Excel - How to know if a specific cell is affected by any macro?

I have an excel file with hundreds of macros, where which macro has extensive code.
I need to change the layout of the cells and that includes moving some cells to another location.
My only problem is that after doing that i need to change the macros that interacts with that cell. Interacts as his value/formula was changed or his value is used in another operation
I can't search in the code for range("C2") for example because the cell can be affected in diferent forms like:
Range("C2")
Cells(2, 3)
Range("B1:E5")
Range(Cells(1, 1), Cells(10, 10))
Offset
Is there any whey that i can discover which macros are changing a specific cell?
Are you looking for a way to track a specific cell, or for a way to track all cells affected by VBA code? Also, "affected" as in "value was changed" or "formula was changed", or "cell was merged with another", "range was unmerged", or "borders were changed", or "backcolor was changed"? Is adding conditional formatting "affecting" a cell? Data validation?
And then, there are more ways a cell can be "affected", too. Without tracking the code as it's running, it could be hard to tell whether this myRange variable is affecting the cell you're looking for.. especially if methods like Range.Offset and Range.Resize are used.
Then ranges can be named, so Range("Foo") might be referring to a cell you're interested in, but you can't know that without verifying whether Names("Foo").RefersToRange includes that cell.
Short of carefully reviewing the code, I'm afraid you can't.
If no macro is highlighting any cell in bright yellow, you could always make a copy of the file and then handle Workbook_SheetChange in ThisWorkbook:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Target.Interior.Color = vbYellow
End Sub
You can easily add conditional logic here to only recolor modified cells from a specific sheet, or from a specific column or row.
Now remove or comment-out any Application.EnableEvents toggling everywhere in the code, and run your macros - the cells it affected (assuming a change in value is what we're after) should all be bright yellow.
If you need to know what code caused this, you can place a breakpoint there, and inspect the call stack:

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.

vlookup not finding vba cells

I have a table that uses a bit of vba to populate column b with id numbers.
I then have column c as a vlookup that gets a name based off the id, from another sheet. The vlookup in the first cell works fine and returns the correct name, John Doe.
When I drag down, the rest of the cells in column c return the same name as the first, John Doe. The vlookup in the other cells is exactly the same, except the reference cell does change, as expected....so, c2 = vlookup(b2, $range, col, false), c3= vlookup(b3..), c4=vlookup(b4,..), etc.
The catch is, when I look at the vlookup in c3 and click on b3, the cell changes to find the correct name (no longer John Doe). So it works fine. And I have to do that for every cell in column c.
It's like vlookup isn't aware that column b changed? Is that something that happens with vba? Is there a refresh command or some other way for vlookup to register that column b has changed without having to click on each individual vlookup function?
If you've entered a formula in a cell, then copied the formula to another cell and Excel hasn't updated your results, that means that Calculation Mode is Manual.
To fix it (depending on version, this is for Excel 2010)
Click on Formulas in the Ribbon
Click on Calculation Options
Click on Automatic
If you need to have it calculate manually (valuable for making many formula changes in a large worksheet):
Click on Formulas in the Ribbon
Click on Calculate Now to calculate the entire workbook, or
Click on Calculate Sheet to calculate the current worksheet
You can skip all the clicking by pressing F9 to Calculate Now or Shift-F9 to Calculate Sheet.

VBA code to show Message Box popup if the formula in the target cell exceeds a certain value

I am trying to write a simple macro to display a pop-up (vbOKOnly) if the value in a cell exceeds a certain value.
I basically have a worksheet with products and discounts. I have a formula in one cell, say A1, that shows the discount as a percent (50% or .5) effective discount of all the entries.
What I'm looking for is code to display a message box if the value of cell A1 exceeds say 50%, because the input of another cell pushed the discount over 50%.
Thanks!
You could add the following VBA code to your sheet:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("A1") > 0.5 Then
MsgBox "Discount too high"
End If
End Sub
Every time a cell is changed on the sheet, it will check the value of cell A1.
Notes:
if A1 also depends on data located in other spreadsheets, the macro will not be called if you change that data.
the macro will be called will be called every time something changes on your sheet. If it has lots of formula (as in 1000s) it could be slow.
Widor uses a different approach (Worksheet_Calculate instead of Worksheet_Change):
Pros: his method will work if A1's value is linked to cells located in other sheets.
Cons: if you have many links on your sheet that reference other sheets, his method will run a bit slower.
Conclusion: use Worksheet_Change if A1 only depends on data located on the same sheet, use Worksheet_Calculate if not.
Essentially you want to add code to the Calculate event of the relevant Worksheet.
In the Project window of the VBA editor, double-click the sheet you want to add code to and from the drop-downs at the top of the editor window, choose 'Worksheet' and 'Calculate' on the left and right respectively.
Alternatively, copy the code below into the editor of the sheet you want to use:
Private Sub Worksheet_Calculate()
If Sheets("MySheet").Range("A1").Value > 0.5 Then
MsgBox "Over 50%!", vbOKOnly
End If
End Sub
This way, every time the worksheet recalculates it will check to see if the value is > 0.5 or 50%.
I don't think a message box is the best way to go with this as you would need the VB code running in a loop to check the cell contents, or unless you plan to run the macro manually. In this case I think it would be better to add conditional formatting to the cell to change the background to red (for example) if the value exceeds the upper limit.

Major formatting issue in Excel - VLOOKUP

I need help with a formatting issue in Excel, which is interfering with the VLOOKUP function in my Excel sheet.
I have two sheets with more than 20,000 column values as lookup, and the same number of values for reference. All the values in both cells are weirdly formatted, some with green triangles at the upper left corner of cells, some are text, etc.
Is there a way in Excel using a macro/VBA to remove or make all formatting similar in both sheets? The reason for VBA is because the person who is going to work with this file needs everything automated and is not familiar with Excel at all. I already have the VLOOKUP function in the cells, I just need to work with the formatting.
Well, I fight with partial lookups this way:
In the items array, I create new empty FIRST column and then place formula
="+"&B2
This will take the content of Cell B2 and add + in the front of it.
When I do vlookup, I add "+" to searcheable value
=VLOOKUP("+"&A6,A:O,2,FALSE)
Therefore, instead of comparing for example Strings and numbers, I compare Strings, by adding "+" in the front.
Another technique, is to kill all formatting:
Select whole column, click DATA-TEXT TO COLUMNS-DELIMITED and then DESELECT ALL DELIMITERS. Click Finish. This will clear your formatting.
===========================================================================
This is the VBA solution you asked for:
Call it from Excel
=GetLookup(G2,A:C)
Here is VBA:
Function GetLookup(LOOKFOR As String, RANGEARRAY As Range) As String
GetLookup = Application.WorksheetFunction.VLookup("+" & LOOKFOR, RANGEARRAY, 3, False)
End Function
Good luck!
I'm assuming the data type in all of the cells is the same, or you want it to all be the same. The following steps will make the cells a uniform type:
Save your workbook, in case this does not do as you require
Select all cells you wish to be of the same cell type
Press Ctrl+1, on the "Number" tab, select the type you wish these cells to take. Press OK.
Open the VBA editor using Alt+F11
Open the immediate window with Ctrl+G
Type the following: for each cell in selection : cell.formula = cell.value : next cell
Press enter (you may have to wait a few seconds).
If you take this action with the same data type (e.g. choose "Text" for both ranges in step 3) on both your ranges you should be "comparing apples with apples" and your VLOOKUP should function as required.
Hope this helps.
Edit: formatting, clarification