I'm not sure how to even ask about this; hopefully this explanation is clear enough.
Using a VBA recorded macro, with the relative selection on, I have a formula that uses the countif($C$656:C656,[#Column1]) (which is actually the cell directly below the Table Header, Column1)
The problem I'm facing is the formula doesn't translate as the variable locations because I don't know how to use relative code to actually tell vba to search all the way up the Table column until you reach the cell directly below the header. It must be relative because the table is not always created on row 655 and has varying numbers of rows within the table itself so I can't tell VBA R[1]C[1]:R[367]c[1] for example.
Below is the formula entered in the cell itself (B656).
=IF(B655="Column2",[#Column1],
IF(COUNTIF($C655:C$656,[#Column1]),
(CONCATENATE(TEXT([#Date],"yyyy-m-d"),
"_",TEXT([#Date],"hhmm-s"),
"_R-"&COUNTIF($C655:C$656,[#Column1]),
IF([#[TRANSACTION FEE ID]]="","","_"&[#[TRANSACTION FEE ID]]))),
[#Column1]))
Below is the VBA translation using the relative button turned on:
'Enter formula in cell under header (INCLUDES REPEATED DATES FOR DIFFERENT SPREADS)
Range("TblAccountTradeHistory[[#Headers],[Column2]]").Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = _
"=IF(R[-1]C=""Column2"",[#Column1]," & Chr(10) & "IF(COUNTIF(R[-1]C3:R656C[1],[#Column1])," & Chr(10) & "(CONCATENATE(TEXT([#Date],""yyyy-m-d"")," & Chr(10) & """_"",TEXT([#Date],""hhmm-s"")," & Chr(10) & """_R-""&COUNTIF(R[-1]C3:R656C[1],[#Column1])," & Chr(10) & "IF([#[TRANSACTION FEE ID]]="""","""",""_""&[#[TRANSACTION FEE ID]])))," & Chr(10) & "[#Column1]))"
ActiveCell.Offset(1, 0).Range("A1").Select
'^^^^^^^^^^^^^^^NOTE: THERE'S A SEARCH ALL CELLS ABOVE THIS ROW THROUGH THE HEADER, BUT ABOVE FORMULA SAYS "R[-1]C3:R656C" WILL THAT MESS UP FOR TRANSACTIONS THAT AREN'T THAT LOW?
Anyone have thoughts of how to make this work?
Don't use cell references within tables and much of your problem goes away.
Assuming your table is named "Table1", try inserting a new column titled DataRowsAbove with a formula of =ROW()-ROW(Tables[#Headers]-1
Then insert another column titled Column1Repeated with a formula of =IF([#DataRowsAbove]=0,0,COUNTIF(OFFSET(Table1[#Headers][Column1],1,0,[#DataRowsAbove],1),[#Column1])
The offset is the secret sauce, here. It gives you a range of every Column1 value above the current line without any cell address references.
Then your Column2 formula can be
=IF(
[#Column1Repeated]=0,
[#Column1],
CONCATENATE(
TEXT([#Date],"yyyy-m-d"),
"_",
TEXT([#Date],"hhmm-s"),
"_R-",
[#Column1Repeated],
IF(
[#[TRANSACTION FEE ID]]="",
"",
"_"&[#[TRANSACTION FEE ID]]
)
)
)
If that works for you, then...
If you don't care about those two extra columns showing, you're fine!
If you can insert two new column before A:A, move those two new column into the new A and B columns, then hide those columns, then your worksheet looks the same as it did before except that it starts with column C.
If you can't do either of these, then you have to replace mentions of [#DataRowsAbove] & [#Column1Repeated] with their formulas instead of those two new columns.
I am trying to combine two cells like this:
Lets say that cell 1 is 123 and cell 2 is 321
Then I want cell 3 to look like this:
123 (321)
So if this was in excel and I wanted to do it with two cells, I would just do it like this:
=E2&"("&F2&")"
But in VBA, I'm actually trying to do it with a loop. So that the i value is the row, and the column is constant.
I've tried to do it like this:
ActiveCell.FormulaR1C1 = _
"='Nye_Virksomheder'!R & i & C2 & ""(""&'Nye_Virksomheder'!R & i & C4&"")"""
So "Nye_Virksomheder" is the sheet that i am taking the data from.
But I keep getting an error. Not sure what is wrong. Anyone that is able to figure it out?
Kind regards,
Zebraboard
You have several issues with quotes.
"='Nye_Virksomheder'!R" & i & "C2 & ""("" & 'Nye_Virksomheder'!R" & i & "C4 &"")"""
Note that you don't actually need a loop to write a formula with an increasing row. You can write the formula to the entire range in one go (keeping the row as a relative reference).
I have the following formula in excel
=MAX(INDIRECT($A2 & "!"&"B2:B5"))
A2 is a cell in the current worksheet, which is the name of a worksheet tab. However what i wish to do is drag the formula horizontally like that of standard excel formulas to reference the cells of the "other" worksheet tab
So if i dragged horizontally i would get:
=MAX(INDIRECT($A2 & "!"&"C2:C5"))
=MAX(INDIRECT($A2 & "!"&"D2:D5"))
etc
This wont work with the formulas as a fixed text..so how would i do this?
This will solve your problem. Though not sure whether this is the best solution.
=MAX(INDIRECT($A2 & "!" & CHAR(COLUMN()+64) & "2:" & CHAR(COLUMN()+64) & "5"))
Another way is
=MAX(INDIRECT($A2 & "!"&ADDRESS(ROW(),COLUMN())&":"&ADDRESS(ROW()+3,COLUMN())))
if you want the range to start in the same row and column on the second sheet and finish three rows down.
Looking to get a range from another sheet based upon the Column Letter which is obtained from the current sheet.
So say that I designate A2 on the current sheet to the letter G.
I would then ask A10 to =AVERAGE('Sheet1'!(A2)2:(A2)999)
So essentially it would be =AVERAGE('Sheet1'!G2:G999)
However, this doesn't work. Anyone have any idea how to make this worK?
You would use the Indirect() function:
=AVERAGE(INDIRECT("'Sheet1'!" & A2 & "2:" & A2 & "999"))
So I am writing some formulas for my spreadsheets and wondered if there was a placeholder for this row number.
For example say I am using the formula:
=C4+SUM(F4:N4)
I know I can autofill this, but what I really want is some stock formula like:
=C{this.row}+SUM(F{this.row}:N{this.row})
Is this possible? Thanks
The function you need is INDIRECT(CELL/RANGE as string)
The INDIRECT function takes in a string and uses it to evaluate a cell or range location. This is where you get the flexibility (aka indirection) that you need.
eg your formula would be:
=INDIRECT("C" & ROW()) + SUM(INDIRECT("F" & ROW() & ":N" & ROW()))