Excel - using a flag to control cell level calculation - excel

I'm trying to figure out if there is a way to control calculation at a cell level. Here is what I'm basically trying to do in an If statement... I don't know how to "UseTheLastValue" of "DoThisBigLongCalc" though....
Cell A1 =IF(CalculateFlag = True, DoThisBigLongCalc, UseTheLastValueOfBigCalc)
Any help would be appreciated! The reason I'm doing this is I have a lot of formulas that I do want to calculate every time I hit F9, but also a whole lot that I don't because of their long calc time..... Thanks

This approach might help you,
1) Whatever cells you want to do a recalculate with F9, enter the formulas in the excel sheet directly. Even if you set the formula calculate method to manual, the data will be refreshed and recalculated when you press F9
2) For the cells which you do not want to do a recalculate, enter the formulas for those cells through VBA, so that whenever the code is executed, the formulas will be calculated. If not, the old value would still stay there on the cell (UseTheLastValueOfBigCalc)
This code could be triggered through a worksheet_change event which can detect a True/False on a particular cell and execute accordingly. If its true, just recalculate the formulas, if false just ignore.
Below is a code snippet which detects True in cell A1 and calculates accordingly,
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" And Range("A1") = True Then
Range("B3") = Application.WorksheetFunction.Sum(Range("B1:B2"))
End If
End Sub
This code sums the values in B1 and B2 and prints in B3 only if True is entered in A1
Even if you change values in cells B1 or B2 the sum will not be recalculated, which means the old value can still be used unless A1 is changes to true.
Once True is entered, the formulas are calculated,
Hope this helps.

Related

Auto-populate cell, based on another cell's value, without formulae

I've seen a couple of spreadsheets over the years that had a blank, unpopulated, non-formula cell, that would populate when another cell was populated correctly. I am wondering if there is a way to do this without using add-ons, or VBA.
Scenario:
User is asked to enter a value in cell A1.
If the value is X, cell B1 populates with a value.
If the value is Y, cell B1 remains blank.
I know that this can be done with a formula such as =IF(A1="","",IF(A1=1234,"Hello 1234","")).
However, I am wondering if it is possible to do this without a formula in cell B1, but still have cell B1 populated?
From your description, it sounds like this might be what you witnessed. Macros can be set to trigger automatically given a certain event & criteria met. In this instance, the macro will fire when you make a Worksheet_Change in cell A1.
Note that the change to A1 must be manual to fire macro - a change due to formula will not suffice to trigger macro
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1")) Is Nothing Then
If Target = "X" Then
Range("B1") = "X Result"
ElseIf Target = "Y" Then
Range("B1") = "Y Result"
End If
End If
End Sub

Excel - Force recalculation when different cell is selected

A bit of context:
I recently discovered that the following formula returns the address of the cell that is currently selected (or if a range is selected, returns the address of the upper-left most cell in the range):
= CELL("address")
At first, I thought this formula would be useful for conditional formatting, since it could be used as part of a condition to only format the cell that is selected (e.g. the conditional formatting rule could be something like = CELL("address")=ADDRESS(ROW(),COLUMN())), but I am facing an obstacle.
The formula is volatile, but volatile functions only update either when:
A cell in the worksheet is changed
F9 is pressed on the keyboard
All that said, my question is: Is there a way to have a cell automatically recalculate whenever a different cell is selected with a mouse click? Even volatile cells won't update from this condition, because selecting a different cell, in itself, won't cause any data in the cells to change.
Of course, it could be updated manually by pressing F9 after selecting a different cell, but I am wondering if there is a way to automate this.
You can use the Worksheet_SelectionChange() event in VBA.
Open your VBE (Alt+F11), find your workbook in the VBAProject pane (upper left) and double click your worksheet.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Force this cell to recalculate any time any cell is selected/highlighted
Range("A1").Calculate
End Sub
Now anytime moves around on the worksheet Cell A1 will recalculate.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Range("A1:D4"), Target) Is Nothing Then
Range("A1:D4").Interior.Color = xlNone
Target.Interior.ColorIndex = 6
End If
End Sub
This will now highlight the cell chosen only if the cell chosen is in A1:D4

Getting adjacent cell values around the current cell in Excel VBA

I want to get the adjacent cell values for calculation in VBA.
It is easy to get the left cell of the current cell(e.g.H19) usually like this:
=H18
If I copy this cell to other cells, each of them is changed to an appropriate cell number.
But in VBA code, I am not sure if I am right to get the value of the left cell.
Public Function LeftCell()
LeftCell = ActiveCell.Offset(0, -1).Value
End Function
I am not sure this is correct, I tested copying this cell to other cells but each result is not changed automatically.
I clicked all kinds of Calcuation buttons on the Menu, changed Calculation as Automatic, but there is no calculation occur.
The only way I can do is to manually select each cell and press enter.
Is there any way to calculate all cell values?
Otherwise, "The Active Cell" means "The Selected Cell by Cursor"?
Thanks for your help in advance.
Adding a formula as #Chris Harper suggests would work, but then you may as well just write the formula in the cell.
Rather than the ActiveCell you want the cell that called the formula.
Public Function LeftCell()
LeftCell = Application.Caller.Offset(, -1).Value
End Function
Edit: If you want the cell to update whenever you change the value add Application.Volatile True as your first line in the function.
https://msdn.microsoft.com/en-us/library/office/ff193687.aspx
Calculate method in Excel VBA do all kind of calculations. You can even define a range to calculate only a range of specific cells like Worksheets("Sheet1").Calculate.
Yes, ActiveCell is always the Selected Cell.
As an alternative to setting value by Offset, you can use ActiveCell.FormulaR1C1 = "=RC[-1]"

Copy value of cell to another cell to record vehicle mileage

I'm working on a spreadsheet to record vehicle mileage. I'd like to manually enter starting mileage in F6 and ending Mileage in G6. I'd like the value of G6 to then automatically be copied to F7 and I'd enter the ending mileage in G7- and so on. The problem I have is dealing with weekends and holidays when 2 or 3 blank cells may occur. I've tried using something like =if(D9=""," ", G6), but this doesn't work.
I've also tried this function in VBA: =IF(D9=""," ",LastNonBlankCell(G8:G39)) [D9 is a datefield] and only ended up with 0's.
Function LastNonBlankCell(Range As Excel.Range) As Variant
Application.Volatile
LastNonBlankCell = Range.End(xlDown).Value
End Function
Do you really need the blank rows for weekends or holidays? If not, set D8 to the formula =workday.intl(D7,1) then copy it down as far as needed. If you need to allow for holidays, see the Excel documentation for WORKDAY.INTL. The list of dates will now contain only workdays, so the ending mileage for one row can be copied down to the next with no special handling.
This makes weekends and holidays harder to see at a glance. That can be addressed using conditional formatting. Setup formatting for the range D8:D<whatever>, use the formula =D8>(D7+1), and set the formatting to whatever stands out for you.
The following VBA macro will achieve your end goal in a slightly different way than what you asked about.
After you have installed the macro, when you double-click on a cell in column G, the macro will copy the last entered ending mileage to the cell to the left in column F.
For example, if the last ending mileage entry was in cell G6 and you double-click in cell G9, that entry will be copied to cell F9. You can then enter the new ending mileage in cell G9.
Private Sub WorkSheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Application.Intersect(Target, Range("G:G")) Is Nothing Then
Selection.Offset(0, -1).Value = Selection.End(xlUp).Value
Else
Exit Sub
End If
End Sub
To install the macro, right-click on the worksheet's name tab, select "View Code", and paste in the macro code. It won't work if you put the code into a regular module.

Excel formula not executing on run time with selecting and double click on the particular cell

My scenario is , I have one Excel sheet. On the same sheet in cell A2 I am inserting the data through code and cell B2 I already put the data manually before inserting the data in cell A2, in cell C2 I written the formula =EXACT(A2,B2).
Now when I run the code, the code put the value in cell A2,and the Status should be change according to the condition given and the data on both cells. But it doesn't change the status according to the formula in cell C2 and the status gets changed when I double click on that particular cell and enter then the condition work. Please tell me how it will be possible without double click on the particular cell?
This works (I've tested it) if automatic calculation is switched on (Excel Options->Formulas->Calculation Options).
Here's the code (I'm using A1, B1, C1):
Private Sub CommandButton1_Click()
Cells(1, 1).Value = 2
End Sub

Resources