Forcing Cells to recalculate in Excel using Macros - excel

I am currently working on an excel spreadsheet called Calendar.xlsm
I have Excel2010.
My workbook has the following worksheets:
Cover Sheet
Calendar
Colour Detection
Printable
The "Colour Detection" sheet uses a User Defined Function to detect the colours of cells on the "Calendar" worksheet and based on that colour returns a number for that particular cell; For example if Calendar!C3 has a fill colour of "light green" then Colour Detection!C3 = 43
Just for completeness the UDF is called ColorIndex and was written by Bob Phillips.
I then use conditional formating to colour cells in my "Printable" worksheet based on the number stored in "Colour Detection".
My problems start when there is a colour change on the "Calendar" sheet, usually when this happens "Colour Detection" remains unchanged unless I select the cells manually and press return.
My knowledge of how Excel works with Visual Basic in this area is very limited but growing.
My first idea for a solution was to record a macro where I selected each cell and hit return one by one. I then attached the macro to a button called Calculate on the "Cover Sheet". Immediately this didn't work because there are approximately 800 cells that use this UDF.
While trying this I thought I stumbled on the solution, I noticed that if I applied my solution to only a few cells, the whole worksheet seemed to update. I then applied this and tested it a few times and it worked. I then presented my work to a friend as the masterpiece I was sure it was, and while demonstrating it to them it all fell over and only the cells I selected in my macro updated.
Next I tried adding the line
Application.Volatile
to my UDF but this doesn't to make a difference.
My question is how do I easily force every single cell in my worksheet to update by doing something simple like pushing a recalculate button?

Try Ctrl-Alt-F9 this forces every formula in the workbook to be calculated. Or you could set workbook.forcefullcalculation to True in your workbook_Open event.

Related

Excel cell is magically populated with formula after another cell gets data entered

Some mysterious behavior within a simple worksheet (Excel 2013 - Win7, but also continues if workbook is opened on Excel 2019 - Win10):
Cells in column "A" are getting populated with a formula after I enter a number in cell of column "C".
For example:
When I enter the number 123456 in C23, the following is automatically entered into a previously blank A23: =HYPERLINK("http://gigapan.com/gigapans/"&C23,C23) which is what I want, but how is it happening automatically? There are NO conditional formattings (that I can find), NO Worksheet_Change VBA subs running (NO VBA code at all for this workbook). The result is what I want, but it is driving me crazy not knowing how it got set up this way!!! Took the xlsx file to another computer with no Personal.xlsb to interfere and same behavior. Suggestions?
Excel example screenshot...
This is normal behaviour, controlled by the option Advanced > Extend data range formats and formulas. Described at https://support.microsoft.com/en-us/office/advanced-options-33244b32-fe79-4579-91a6-48b3be0377c4

Is there a way to make Conditional Formatting ignore the FollowedHyperlink flag?

I have a table of contents in Excel 365. In the table, column 2 contains the names of worksheets and column 3 contains the names of tables and/or named ranges in the associated worksheet. I used the HYPERLINK function to link each cell to its specific component: hyperlinks for the worksheet column go to cell A1 on the associated worksheet; hyperlinks in the table/named range column go to that specific table or named range.
Since this results in several repeats of worksheet names, I used conditional formatting to change the text to white when the worksheet name is a repeat of the one directly above it. This makes it look more like an outline. It works like a charm up until the point that I click on the link in a repeated cell, making it a FollowedHyperlink. When I return to the table, this is no longer whited out. Is there a way to make these stay white, even after they've been followed?
Here's the formula I used for the conditional formatting rule applied it throughout the range =$C$6:$C$24:
=C5=C6
Here's what it looks like before I click one of the whited-out cells:
Here's what it looks like after I click one of the whited-out cells:
Urdearboy posted a link showing how to change the style of a followed link.
https://superuser.com/questions/728825/how-do-i-prevent-excel-from-changing-the-color-of-a-clicked-link/819931#819931
It is probably the best solution without using VBA.
But if you want some not control over it, if for example, you may want all links in a table if contents to be uniform color regardless if they were taken or not, but you want links in the rest of the book to change. That's as easy as a single line of VBA in the worksheet's SheetFollowHyperlink event:
Target.Range.Font.Color = vbBlue
And if you wanted to apply it to the entire workbook instead of a single sheet you would put it in the workbook's SheetFollowHyperlink event.
But if you want to be a little creative...
You could make the link disappear by blending it with the background color:
Target.Range.Font.Color = Target.Range.Interior.Color
Or you can draw attention to it, by changing the back color:
Target.Range.Interior.Color = vbYellow
Really, your imagination is the only limit. You can do just about anything you'd ever want to do ( and a lot more of things you would never want to do).

Formulas become unresponsive to changed value inputs to referenced cells

The question closest to mine did not return a satisfactory answer.
Inserted formula with cell reference does not recognize new input
In a particular macro-enabled workbook, macro-generated formulas do not change output values when data in referenced cells is changed. For instance, I can enter some numbers in referenced cells and the formula-containing cell will show the calculated number, but those numbers will not change when changing the data in the references. Manually typed formulas in the same workbook show the same behavior regardless of being in or outside range objects or in non-macro generated worksheets within this workbook. It doesn't seem to be specific to any particular formula.
I checked that the cells are formatted as numbers and that the formulas are valid. Formulas still appear in the cells after computing values.
Reloading the same workbook without macro-enabling shows the formulas working correctly.
In another macro-enabled workbook no problem is observed.
I checked property settings in the VBA editor and found no differences between them.
It seems like something is going on related to macro-enabling and not any particular code that is causing formulas to become unresponsive, but I am stumped as to what that could be.
Here is the formula in which I noticed the problem:
in "=d4:d21" the array formula "{=IF(ISNUMBER(C4:C21), C4:C21/C3, "0")}"
Even with a simple formula placed anywhere on the sheet like "=average(f4:f6)", this problem occurs.
I think creating the macro containing these lines of code is when the issue started, but they are not specific to it:
Sub CreateDataSheet()
Sheets.Add After:=ActiveSheet
Range("d4:d21").Select
Selection.FormulaArray = _
"=IF(ISNUMBER(RC[-1]:R[17]C[-1], RC[-1]:R[17]C[-1]/R[-1]C[-1], ""0"")"
End Sub
How can I get these formulas to work again?
The calculations in the workbook were set to 'manual' and I changed it to 'automatic'. The formulas work now.

How to change the tab color based on a cell in the worksheet?

I have a very large spreadsheet with lots of worksheets that I use to create invoices based on a project number. Each project doesn't get billed on a monthly basis. I have to print each invoice to PDF and to avoid having to open every tab to see if there is information to bill, I'd like to change the tab color based a cell which contains the total of the invoice.
I have used VBA very little but have found several macros that are trying to do the same thing but because they rely on something different, I can't get what I need. I found one that just turns all the tabs a color. I found one that changes the color based on a true/false formula. I don't have the knowledge around this code to change it so that if cell H22 is >0, it turns the tab a color.
The invoices created before I took my role aren't consistent so not all totals will be in cell H22 -- it could be H14. I'm even willing to put a button on each tab that I could press to apply the macro as I do my preliminary work on each tab. I just don't have the knowledge good enough to know how to write the code. Thanks!
The easiest way would be to find an cell that none of the worksheets use (say AZ99) and do the equivalent of the H22>0 example you described on each sheet.
e.g sheet1 might have AZ99 as X10>0, sheet2 might be Y11>0 or whatever.
Use the true/false VBA code to look at cell AZ99 or whatever the cell you choose is.

How can I make my excel formulas calculate after values have been added programatically

I have a .net program that updates values in an excel sheet programmatically using an OLEDB connection.
my excel sheet has some simple SUM formulas that sum the values that are programmatically updated. The problem is that since the values are updated while the excel sheet is closed my formulas do not calculate when the sheet is opened. When I press calculate sheet button the formulas are still not executed (because excel does not recognize that the new values have been added, possibly?). The only way I can get them to execute is if I click on the cell holding the formula (As if to modify the formula) then press enter(making excel reevaluate the cells). I have calculation option set to automatic and my data types are correct (general for the formulas and number for the number values). Is there any way I can make the spreadsheet calculate the formulas when I open it?
Try pressing ctrl+alt+f9 which should force a full calculation and not just a recalculation (like the calculate button does) that looks at cells that have changed.
More info on calculation material can be found here.
For vba I believe it is
Application.CalculateFull
Maybe this will work
Private Sub Workbook_Open()
Worksheets(1).Calculate
End Sub
Just in case someone runs into this problem: To do a full recalculation in vba you can use Application.CalculateFullRebuild to force a full calculation.

Resources