Getting adjacent cell values around the current cell in Excel VBA - excel

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]"

Related

using Application.WorksheetFunction.MRound and Cells.Formula

I am trying to write excel formulas using Cells.Formula and Application.WorksheetFunction.MRound
I want to leave the formulas in the cells but it only leaves the values, not the formulas.
How do I leave the formulas in the cells?
The Application.WorksheetFunctions are not meant to be placed into cells, but will perform the calculation of a worksheet function in VBA.
If you want to place a formula into a cell you need to use one of the Formula properties, for example
Range("A1").Formula ' or
Range("A1").FormulaR1C1
See this question for an example of both.
Instead of cell.value = Application.WorksheetFunction.MRound use cell.formula = "=MRound". I.E. put the formula as you'd write it in a cell into the cell as a string.

Sum only visible cells in one ROW (not column)?

I have a row and I want to sum only visible cells, i know if it's a column I can use subtotal (109,range), but this one doesn't seem to work for cells in one row. Anyone knows how to sum only visible cells in a row?
Please click here for picture
If a VBA solution is okay, this will work:
Function sumVisible(rng As Range) As Double
Dim cel As Range
For Each cel In rng
If cel.EntireColumn.Hidden = False Then
sumVisible = sumVisible + cel.Value
End If
Next cel
End Function
Pretty straightforward - just checks if a cell in your range has a hidden column, if it's visible, sum it.
=sumVisible(D2:M2) is how you'd use it.
You can check the width of the cell.
=IF(CELL("width",A1)=0,"hidden","open")
you can then sum your cells as need it using IF and CELL
=IF(CELL("width",A1)=0,0,A1)
more info below:
Ignoring a hidden column in an excel sum formula
You can do this using a single worksheet formula alone, provided that none of the columns which remain unhidden will have a width of less than or equal to 0.5 (which, in practice, would be so narrow as to be virtually hidden in any case).
Assuming a range of A1:E1
=SUMPRODUCT(0+(CELL("width",OFFSET(A1,,N(INDEX(COLUMN(A1:E1)-MIN(COLUMN(A1:E1)),,))))>0),A1:E1)
Unfortunately this formula will not update automatically as changes regarding hiding/unhiding columns within the range are made. As such, it will be necessary to 'recommit' it each time you make changes in that respect; one way to do this is via going into the formula as if to edit it and then recommitting by pressing ENTER.
Perhaps of interest is this post.
Regards

Excel - using a flag to control cell level calculation

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.

How to make a Cell in Excel Reference itself

I have a table that contains a column that has a risk score. I want to write a formula to examine each cell and determine if the cell is blank, then make the cell value 100, else leave the cells current value. The code I'm trying to use( For cell D2) is as follows:
=IF(ISBLANK(D2),100,D2)
I'm having trouble doing this due to a cell's inability to reference itself easily and I believe inserting this formula into the cell simply overwrites the cells current value, any thoughts?
Say the values you want to process are from D2 through D100. Try this small macro:
Sub OneHundred()
Dim r As Range
Set r = Range("D2:D100").Cells.SpecialCells(xlCellTypeBlanks)
r.Value = 100
End Sub
Adjust the range to meet your needs.
It can be done because I have a spreadsheet that allows this to happen. Part of my formula in cell T3 is as follows: IF(B3="","",IF(B3="Ready","",IF(T3="",TODAY(),T3))) First it checks some other things but at the end it checks itself to see if it is blank. If it is, it writes todays date. Otherwise it just writes itself.
I remember I had to change something in the settings to allow this to happen but I can't find it now.

EXCEL - dropdown and fill cells

Good night.
I'm having some troubles to get what i need done.
I have some cells in a sheet that needs to be filled every day, manually.
I have also a dropdown with all the months, and another one with the days.
Is it possible to save data in specific cells for the selected dropdown values?
Something like for each day mantain different data in the same cells.
Thanks.
I'm not exactly sure what you are requesting. What I think you are trying to do is:
Enter something into a cell
Select the Month and Day from the dropdown lists
Record the value of the dropdowns into cells near the value you entered.
Is this correct? If so, you would need to need to write a VBA macro that would take the values of the dropdown and write them into the cells where you needed them to be (presume next to your entered text). I think this would do that for you.
Sub writeDropdowns()
'This will take the values from cells B1 and C1
'and record this in the two cells next to the selected cell
Selection.Offset(0, 1).Value = ActiveSheet.Range("B1")
Selection.Offset(0, 2).Value = ActiveSheet.Range("C1")
End Sub
It assumes that the dropdown values are in cell B1 and C1 of the same sheet.You could then link this macro to a Form button. This is very simple and doesn't check for any errors, like if there is no cell selected. It should be a good starting point though.

Resources