this is simple demo of what i want to do. I want to set a formula to a range of cells(eg. C1 to C10).
Range("C1").Formula = "=A1+B1"
but how to make formula use dynamic cells like this:
Range("C1:C10").Formula = "=Ax+Bx"
so in reality it is like,
C1 = A1 + B1
C2 = A2 + B2
C3 = A3 + B3
C4 = A4 + B4
C5 = A5 + B5
...
C10 = A10 + B10
how to change RHS of this formula to make above working: Range("C1:C10").Formula = "=Ax+Bx"
Range("C1:C10").Formula = "=A1+B1"
Simple as that.
It autofills (FillDown) the range with the formula.
I think this is the simplest answer possible: 2 lines and very comprehensible. It emulates the functionality of dragging a formula written in a cell across a range of cells.
Range("C1").Formula = "=A1+B1"
Range("C1:C10").FillDown
I would update the formula in C1. Then copy the formula from C1 and paste it till C10...
Not sure about a more elegant solution
Range("C1").Formula = "=A1+B1"
Range("C1").Copy
Range("C1:C10").Pastespecial(XlPasteall)
Use FormulaR1C1:
Cells((1,3),(10,3)).FormulaR1C1 = "=RC[-2]+RC[-1]"
Unlike Formula, FormulaR1C1 has relative referencing.
Use this
Sub calc()
Range("C1:C10").FormulaR1C1 = "=(R10C1+R10C2)"
End Sub
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am trying to fill cells from A3 to A100 with numbers, as shown. Every three cells, the cell value increases of 1 digit. More than having the VBA snippet as an answer, which would be great, I would really appreciate if someone could be kind enough to make a step by step explanation of the logic behind building the code. I need to understand to racionalize on this in order to apply this knowledge in future and more complex endeavours.
A3 ---> 1
A4 ---> 1
A5 ---> 1
A6 ---> 2
A7 ---> 2
A8 ---> 2
A9 ---> 3
A10 --> 3
A11 --> 3
A12 --> 4
A13 --> 4
A14 --> 4
( . . . )
Manually, in A3 enter:
=ROUNDUP(ROWS($1:1)/3,0)
and copy down.
So with VBA:
Sub qwerty()
Range("A3:A100").Formula = "=ROUNDUP(ROWS($1:1)/3,0)"
End Sub
If you did not want formulas in the final result:
Sub poiuyt()
With Range("A3:A100")
.Formula = "=ROUNDUP(ROWS($1:1)/3,0)"
.Value = .Value
End With
End Sub
VBA approach via datafield array and flexible row settings
Just to show another possible approach using a datafield array and allowing to define even individual start and end rows based on #CalumDa 's modified formula (assuming that you start always with 1, 1, 1 in the first three rows):
Code
Option Explicit
Sub FlexibleRange()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("MySheet") ' << change to your sheet name
Dim start As Long, last As Long, i As Long ' declare variables
start = 3: last = 100 ' << change to your individual rows
Dim v: ReDim v(start To last, 1 To 1) ' variant datafield array, counter
For i = start To last: v(i, 1) = Int((i - start) / 3) + 1: Next i ' calculate values
ws.Range("A" & start & ":A" & last) = v ' write numbers to column A
End Sub
Put in
A3 = 1
A4 = 1
A5 = 1
A6 = A3 + 1
A7 = A4 + 1
A8 = A5 + 1
Then copy A6 to A8 down to A100
Why do you need to use VBA here? You can use the following formula on the worksheet. It doesn't depend on cells above so it'll give you the right answer whichever row you place it on:
=INT((ROW()-3)/3)+1
If you really want VBA there's not much to explain about this. It just places the same formula in the cells you require:
Public Sub FillColumnA()
ThisWorkbook.Worksheets("Sheet1").Range("A3:A100").Formula = "=INT((ROW()-3)/3)+1"
End Sub
I have an Excel file with defined macros that are opening specific PPT files.
I'm looking for a VBA code that will run a specific macro based on if certain cells are empty and other are not.
For example:
If cells B1 and B2 are empty but cell B3 isn't --> then run Macro1
If cells B1, B2 & B3 are empty but cell B4 isn't --> then run Macro2
Is it possible?
Many thanks!
Thanks for the quick answer! most chances I missed something there... Report 3 should be activated when d8 & d9 ARE NOT EMPTY and d10 is empty (sorry it was the other way)
Sub Choose_Macro()
Dim v As Variant
With Worksheets("Reports")
v = Application.Match(Chr(42), .Range("d8:d11"), 0)
If Not IsError(v) Then
Select Case CLng(v)
Case 3
Report_3
Case 4
Report_4
Case Else
'do nothing
End Select
End If
End With
End Sub
So I have two columns of data and I want to add a date next to data that has two sets of values. So if A3 = Trucks and B3 = 2008 appear in the two columns I want C3 to have a date value of 11/1/2016. If these values A3 = Trucks and B3 = 2008 appear anywhere else in the data I want the date value to increase by 1 to 11/2/2016 and have that run until the data is fully queried.
This is what i understand from your question
if A3 = Trucks, B3 = 2008, C3 = 11/1/2016. then
if A10 = Trucks, B10 = 2008, then C10 = C3 +1 i.e. 11/2/2016
if my understanding is right, the below formula should work. Here is a google spreadsheet.
IF(MAX(--($A$1:A8=A9)*--($B$1:B8=B9)*ROW($A$1:A8))>0,OFFSET($C$1,MAX(--($A$1:A8=A9)*--($B$1:B8=B9)*ROW($A$1:A8))-1,0)+1,"New Date")
Please note the formula is an array formula i.e. press Ctrl+Shift+Enter after typing in cell. Also I have added an if clause, in case the item is appearing for the first time in the list, it will show "New Date".
So I have something like this:
A B C
1 11 12 13
2 10 20 15
3 1 -8 -2
So A3, B3, and C3 is generated by subtracting A1 to A2 and so on.
If you look at the top of the sheet there is a long bar that shows the formula of a cell when you click on one. If you fill in the sheet manually, you can type in that bar something like = A1 - A2 and it will fill A3 for you.
Right now, because my formula is actually in the code, when I click on A3 for example, the bar only shows 1 instead of = A1 - A2.
How do I get the formula to be displayed in the bar?
Use .formula for your requirement
Worksheets("Sheet1").Range("A3").Formula = "=A1-A2"
My guess is that right now, you're filling your cell along the lines of:
Range("A3").Value = Range("A1").Value - Range("A2").Value
So solve your Problem, use this:
Range("A3").FormulaLocal = "=SUM(A1;A2)"
I need a function that will do what Excel does automatically when you dreag a formula: change the referneces automatically.
For example:
In A1 I have "= A2 + A3"
If i copy this to say C3 it will have: "= C4 + C5"
I need to WRITE a formula in C3 that will produce this.
Any ideas? VBA solution is also welcome
CLARIFICATION:
In need this to be as general as possible.
Meaning A1 can contain ANY formula of any type, containing references to other cells.
for example: "= A2 + A3" or "= VLOOKUP(A2, $C$1:$E$7, 2, True)"
In need to move have this formula, whatever it is, copied to another cell (say C3), w/o the built in copy/paste, and have the references (that aren't set with $) change relatively.
I thought there might be a function to write in the destination file to do this.
I have tried writing an Eval function, and i managed to copy the formula from A1 and have it evaluated in C3, but the references would not change
This question lacks a bit of clarity, but I think this might be what you're after:
=SUM(OFFSET(C3,1,0,2))
This will sum the two cells directly below the given cell (in this case, cell C3). That is, it offsets C3 by 1 row, 0 columns, and grabs a height of 2 cells and then passes the result to the SUM function.
This VBA code would do what you are looking by setting the formula in the active cell:
ActiveCell.FormulaR1C1 = "=R[+1]C+R[+2]C"
You can use the Indirect() function using relative reference style.
For example, if you were in A1 and wanted to sum B1 & C1, it would look as follows:
A1: =INDIRECT("RC[1]",0)+INDIRECT("RC[2]",0)
That will change as you move the cell around to always sum the 2 cells to the left of the cell.
For your specific example (A1 = A2 + A3 || C3 = C4 + C5), it would be as follows:
=INDIRECT("R[1]C",0)+INDIRECT("R[2]C",0)
Hope that does the trick!!