Get relative cell value using R1C1 notation - vba

I'm beginner in excel VBA. Sorry it should be something very basic. My question is I need to get the value of a cell relative to the active or selected cell. So what I tried is as follows
ActiveSheet.Range("P2").Select
Dim s As String
s = "=RC[-11]"
What my plan was to get the value at cell, which is 11 columns left to the active cell. By s="=RC[-11]" I know it will print as it is and its not gonna do any operation. But I tried to express that was my final end value. So how can I achieve that.

Something like:
myRange.FormulaR1C1 = "=RC[-11]"
Where myRange is a variable covering the range of cells you want to assign the formula to in column e.g. from start row to end row e.g.
Dim myRange As Range
Set myRange = ActiveSheet.Range("P2:P10")
Just pay attention to the square brackets which determine if using absolute or relative cell references.

Related

VBA Excel - Update Cell value based on other cell value

I am trying to write a formula in a Range of cells based on other range cells values from another worksheet. The formula is shown below:
ActiveSheet.Range("G14:G43").Formula = "=Worksheets("1ºperíodo").Range("V14:V43").Value"
But the code doesn't work and I get a syntax error. That must be related with the strings but I don't know how to fix it.
The value at V14 must be equal to the value at G14 until the last cell, i.e., the value at G14 equals the value at V43. Besides the syntax error is the formula correct based on what I expect to have?
"=Worksheets("1ºperíodo").Range("V14:V43").Value"
is not a formula. It is a value.
If you only want the static values then just assign the values:
ActiveSheet.Range("G14:G43").Value = Worksheets("1ºperíodo").Range("V14:V43").Value
If you want a live formula you need to pull vba from the string and use the .Address function:
ActiveSheet.Range("G14:G43").Formula = "=" & Worksheets("1ºperíodo").Range("V14").Address(0,0,,1)
But the above can be simplified to:
ActiveSheet.Range("G14:G43").Formula = "='1ºperíodo'!V14"
With the formula, we only need to refer to the first cell with a relative reference and vba will make the changes to each row.

Blank cells in matrix take on average of surrounding cells in Excel

I have a matrix of numbers in excel that contains several blank cells. The numbers correspond to the clay level in the ground, as indicated by a borehole in that position. I am using the cells as a grid to represent a map. I want to replace the blank cells with an average of all of the cells which surround it. How do I do this? I keep getting circular reference errors when I try to do it.
1) Use a proxy range: Create a duplicate range and have that refer to the original. The named range here is F5:H6
For above the range on the left (F5:H6) is called namedRange and the blank cell in the duplicate range on the right uses Average(namedRange) to populate the value.
To add a named range , select your range and then add the name via the name manager .
OR
2) Use VBA
With VBA using the same named range (code in a standard module - replace activesheet with your sheetname) :
Option Explicit
Sub test()
With ActiveSheet.Range("namedRange")
.SpecialCells(xlCellTypeBlanks) = Application.WorksheetFunction.Average(.Value)
End With
End Sub

Loop and Add Range in a place holder

Hello,
First of all, thanks for everyone who's helped me with my previous concern. I have another loop issue though. What I'm trying to do is store a certain range on a place holder and use for Charts. My code loops through column A to check cells that contain Product/URL and if found, will offset to the next column and will store the range highlighted on a place holder.
What I was able to do so far is to just select the cell with content in-line with cell with Product/URL. Here's my code so far.
Dim ProdCell as Range
For Each ProdCell in [A:A].SpecialCells(xlCellTypeConstants)
If Not IsEmpty(Prodcell) and Prodcell.Cells.Value = [A1] Then
ProdCell.Offset(0,1).Cells.End(xlToRight).Select
End if
Next ProdCell
I believe that if I can find a way to highlight the range using VBA coding, I will be able to loop through it. Please help.

Using Search and Replace with VBA/Excel

Got an annoying Excel/VBA issue that I can't seem to get around - would appreciate any help.
I have a formula in a spreadsheet that says something along the lines of if the cell to its left =1, then the cell itself =on, and if not, ="off".
Is it possible to write some VBA search and replace code that turns "off" to "totally_off" when run?
At the moment it just seems to be editing the formula itself, while I really want it to delete everything in that cell and just replace it with the phrase "totally_off"
Thanks for your help.
The following code should work. Just replace the range B1:B10 with the range that the formulas you want to change are in. The procedure tests the value of the cell immediately to the left of the cells in the formula. If the value is zero, it replaces the formula with the "totally off".
Sub totally_off()
Dim rng As Range
Dim cell As Variant
Set rng = Range("B1:B10")
For Each cell In rng
If cell.Offset(0, -1).Value = 0 Then
cell.Value = "totally off"
End If
Next cell
End Sub
Here's a non-VBA, non-search and replace alternative.
Set up a filter on the formula column with Sort and Filter / Filter on the Home ribbon.
Select "off" from the drop-down menu on the column.
Type "totally off" in the first cell of the filtered column and copy it down to the bottom of the column.
Then remove the filter. The formulas evaluating to "on" will remain intact.
No need for VBA - simply use an AutoFilter:
Select the column with the on/off formula
Apply an AutoFilter (Ctrl-Shift-L
Filter for off
Again, select all values
Simply type in your replacement value totally_offinto the formula bar - but press Ctrl-Enter
Done!

I have created a macro using the if function but it doesn't seem to work. What am I misisng?

I created a macro to show the following:
If (I3<>0,I3*G3,H3*G3) and this repeats itself for cell N3, R3, V3, Z3 etc.
Option Explicit
Sub Eg()
Range("J3, N3,R3, V3,Z3,AD3,AH3,AL3,AP3,AT3,Ax3,BB3,XF3,BJ3").Formula = "=IF(RC[-1]<>0,RC[-1]*RC[-3],RC[-2]*RC[-3])"
End Sub
However this doesn't seem to work.
Let me explain a bit more how this should work:
This report needs to be downloaded from an application.
The macro needs to be attached to this report so that when I download the report the macro automatically runs this formula in the appropriate columns.
Also I'll have to populate the spreadsheet for all the rows with this formula.
The columns where the formula should sit are not blank but this needs to be catered for in the report automatically once the macro is run.
What am I missing here?
When you use Range.Formula = formulaAsString, and the range refers to multiple cells, you specify the exact formula string as required by (only) the first cell in the range, while appropriately using relative vs. absolute cell references because the assignment to multiple (succeeding) cells will occur as if you were pasting the first cell in the range into the others, exactly as if you'd done a copy & paste without VBA -- you use absolute addressing A1 vs. A$1 vs. $A$1 vs. $A1 etc... as desired to achieve the right alteration of the formula for the succeeding cells.
For example,
Range ("A1, C1, E1").Formula = "=A2+$A2"
will have the same result as
Range ( "A1" ).Formula = "=A2+$A2"
Range ( "C1" ).Formula = "=C2+$A2"
Range ( "E1" ).Formula = "=E2+$A2"
You are mixing up .Formula with .FormulaR1C1! Your string is R1C1 style, but you assign it to the A1 style formula.
Therefore, simply, change it to:
Range("J3, N3,R3, V3,Z3,AD3,AH3,AL3,AP3,AT3,Ax3,BB3,XF3,BJ3").FormulaR1C1 = _
"=IF(RC[-1]<>0,RC[-1]*RC[-3],RC[-2]*RC[-3])"
or
Range("J3, N3,R3, V3,Z3,AD3,AH3,AL3,AP3,AT3,Ax3,BB3,XF3,BJ3").Formula = _
"=IF(I3<>0,I3*G3,H3*G3)"
As Erik points out in his answer, also the later will work and adjust the formula for each cell in the same way (which is not necessary in R1C1 as the formula stays the same anyway...)