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.
Related
So I have built a formula that has Absolute Cell References, and wanted to repeat the same formula down 3000 cells with each one referencing increment cells. (1st formula referring to Cell $A$1, 2nd formula referring to $A$2) I know that I could easily do this without referencing exact cells and the Fill Handle and this is currently how it's set up, however there's a very large number of people who work in this spreadsheet that have bad Excel manners, and regularly delete rows and cells or copy and paste, which breaks the formulas.
Rather than manually editing the same formula in each cell to change the references from relative to absolute, I was wanting to run a Macro to automatically run the formula for 3000 cells.
I had at first built a Macro that fills 20 cells with the formula, but it didn't adjust the formula based on the active cell. (Always entered with range $A$1:$A$20, and not $A$21:$a$40 when started further down) I changed the Macro to loop, but it looks with all formulas referencing $A$1 rather than updating.
The Macro set up to loop is as follows:
Sub HDDatesRef()
ActiveCell.Select
ActiveCell.FormulaR1C1 = "=IF(AND(HD!R1C1>0,ISBLANK(HD!R1C4)),HD!R1C1,""n/a"")"
ActiveCell.Offset(1, 0).Range("A1").Select
Loop Until ActiveCell.Value = ""
End Sub
Any and all help with figuring this out would help immensely. Right now I also have access to Liknedin Learning, so if there's any suggestions for courses on there I should look into so I can understand what I need to do will help with this.
The Excel application object has a function called ConvertFormula which you can use to change a formula between reference styles (A1 or R1C1) and to specify whether the rows and columns should be relative references or absolute references.
If you start off by creating the formula in each row as a relative reference then you can use ConvertFormula to turn it into an absolute reference. The only restriction is that the formula cannot be longer than 255 characters.
Adapting your code and following the advice in How to avoid using Select in Excel VBA gives us:
Option Explicit
Sub HDDatesRef()
Dim r As Range
' If we know the cell address we want to start in then we could use that directly
' e.g. Set r = Worksheets("HD").Range("E1")
Set r = ActiveCell
Do
' The With block just saves us typing r.FormulaR1C1 multiple times
With r
' Don't know what your relative formula would be. I've assumed that we are
' working in column E but adjust as appropriate
.FormulaR1C1 = "=IF(AND(HD!RC[-4]>0,ISBLANK(HD!RC[-1])),HD!RC[-4],""n/a"")"
' Take the formula we already have which is in R1C1 format, keep it in R1C1 format,
' change it from a relative reference based on cell r to an absolute reference
' and make that the new formula for this cell
.FormulaR1C1 = Application.ConvertFormula(.FormulaR1C1, xlR1C1, xlR1C1, xlAbsolute, r)
End With
' Move down one row
Set r = r.Offset(1, 0)
Loop Until r.Value = ""
End Sub
In case you aren't familiar with them. here are the references for Option Explicit and With...End With
You can do this without looping, Excel is smart enough to know you want incremental.
As an example do run this on a fresh sheet:
Sub ShowIncremental()
Range("A1:A10").Formula = "=Row(A1)"
Range("B1:B10").Formula = "=A1*2"
Range("C1:C10").Formula = "=sum(B$1:B1)"
End Sub
Notice the formulas created in A1:C10. Notice Excel incremented them even though the code didn't say to except in the case where we absoluted B$1.
I recommend you do something similar with your code to avoid looping, this will be much much faster.
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]"
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
I need to add a piece of code to my macro that copies and pastes a range of data.
The problem that I am having is that the data can start and finish on any row.
Is it possible to do this?
e.g. Currently the data starts in cell C10 and ends in cell I20. But next month it could start in C5 and end in I40.
The column range will always remain the same i.e C:I, but the row will change each time.
How to a add code to search for the first instance of data in Column C and copy all the data that follows?
Thanks
Here are some code snippets that might help
Get the first cell in C
If Range("C1").Value <> "" Then
Set firstcell = Range("C1")
Else
Set firstcell = Range("C1").End(xlDown)
End If
I test C1 because it will go all the way to the bottom if that is the only cell in C with values.
After that, to get the bottom cell after that
Set lastcell = Cells(Rows.Count, "I").End(xlUp)
Another way might be to use a named range in the spreadsheet to define the range you want to copy.
=OFFSET(C1,MATCH(TRUE,INDEX(NOT(ISBLANK(C:C)),0),0)-1,0,COUNTA(C:C),7)
That formula will select any number of data rows starting anywhere in column C. It doesn't account for header rows or any other content. If your data would always start in at least, say, row 5, then you could adjust the C1 value and use C5:C1000 instead of C:C. Then, in the VBA you can just use Range("namedRange"). Copy instead of having to work out the start and end points.
If you highlight the required cells before running the macro, then you can use
Selection
to refer to the selected range
ie, instead of
Range("C10:i20").Clear
you can write
Selection.Clear
I have two cells A1 and A2. What I want is for A1 to take the current date mm/dd/yyyy if A2 has a value bigger than zero (0). For this I used the following formula in A1:
=IF(E2>0;(TODAY()))
So if a cell in A2 is for example 60 then A1 will be 11/03/2015.
If I save this and reopen it the next day the values in A1 will automatically change into the new date. This is something that I don't like as I want to use this method to keep track of my expenses each day. So if I type an amount in A2 I want A1 to give me the current date and keep it.
So how can I get the current date without worrying that every time I reopen the file they will change?
What you're looking for is a macro, not a formula. I am assuming you're performing this operation on COLUMNS not ROWS. EG: A1 and B1, NOT A1 and A2.
What is the difference?
Macro: Can be triggered one time on an event.
Formula: Given a input, it constantly re-evaluates the result.
Step 1) Press Alt+F11, this will open up the code editor in VBA.
Step 2) Copy and paste the code below into your 'sheet' that your formula resides in.
Step 3) Save your workbook as a macro-enabled spreadsheet. (xlsm).
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
If (Target.Columns.Count = 1 And Target.Column = 2 And Target.Rows.Count = 1) Then
'Check every row, currently this will get executed once
For Each cell In Target.Cells
'Make sure the value is a number
If (IsNumeric(cell.Value)) Then
'Make sure the value is greater than 0
If (cell.Value > 0) Then
'Check to make sure the date column is empty, so it only ever gets written once
'Note: You can remove the if statement below, if you want it to get re-evaluated every time you make a change
If (Target.Worksheet.Cells(cell.row, 1).Value = "") Then
'Using the current modified row, set column 1 to Today
'Note: Now() is the same as Today in VBA
Target.Worksheet.Cells(cell.row, 1).Value = Now()
End If
End If
End If
Next cell
End If
End Sub
Note: This macro only gets executed if you modify one row at a time, you can change it to work on multiple cells by removing the 'Target.Rows.Count = 1' part of the FIRST 'if' statement.
Note2: The last nested 'if' statement can be removed if you want the date to be re-evaluated any time a change is made to column 2. (aka Column B, in VBA columns are referenced via number, not letters).
Note3: If you really need it to work via rows, try to understand this macro by stepping through it with the debugger, and reconstruct it. Keying off of Rows instead of Columns.