vba code to reference letter and number in excel formula - excel

This is my piece of code:
For loopi = maxlastrow + 3 To lastrowrangetotal
issuerformula = Cells(loopi, 4).Address(rowabsolute:=False, columnabsolute:=False)
Worksheets("Dashboard").Cells(loopi, 4).Formula = "=BDP(""&issuerformula.value&"",""ISSUER"")"
Next loopi
this part assign a variable to a cell expressed in "letter / number", let's say D35 in this case.
This piece of code manage to transform a cell expressed as cell(2,2) for example into B2 :
issuerformula = Cells(loopi, 4).Address(rowabsolute:=False, columnabsolute:=False)
I would like to use this variable to have in my cell the formula "=BDP(B2,"ISSUER")"
However, the way it is coded now returns that : =BDP("&issuerformula.value&","ISSUER")
I tried several combinations, removing ".value", doubling or tripling """,... no chance for me.
What would be the correct combination to do that in this formula Worksheets("Dashboard").Cells(loopi, 4).Formula = "=BDP(""&issuerformula.value&"",""ISSUER"")"

it's ok I found the answer myself, it was a matter of blank
so, this works...
For loopi = maxlastrow + 3 To lastrowrangetotal
issuerformula = Cells(loopi, 4).Address(rowabsolute:=False, columnabsolute:=False)
Worksheets("Dashboard").Cells(loopi, 4).Formula = "=BDP(" & issuerformula & ",""ISSUER"")"
Next loopi

Related

Dynamic Column Index Name Error in Formula

I am trying to calculate the component q'ty from Sheet Plan to Sheet Result by using Vlookup to fill in column,having:
Count_col: is the total column in sheet Plan
Last_col: is the total column in sheet Result (before add column Demand)
Sheet Results
Material
Component
Demand W1
Demand W2
ABCD1000
nc200
#NAME?
#NAME?
Sheet Plan
Material
Demand W1
Demand W2
ABCD1000
1000
200
For i = 1 To count_col
Cells(1, i + last_col).Value = "=Plan!RC[-2]"
Cells(1, i + last_col).Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,Plan!C1:C15,i+1,0)"
Next i
But the result is name error, I checked the spelling of function is correct and dont know how to fix. Why there is "#" in my formula?
Detail of error
=VLOOKUP($A2,Plan!$A:$O,#i+1,0)
i+1 inside "" behaves like a string rather than a variable. Try this.
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,Plan!C1:C15," & i + 1 & ",0)"
Also avoid the use of .Select. Your code can be written as
Cells(1, i + last_col).Offset(1, 0).FormulaR1C1 = _
"=VLOOKUP(RC1,Plan!C1:C15," & i + 1 & ",0)"
Recommended Reading: How to avoid using Select in Excel VBA
Also you are mixing R1C1 and A1 style of referencing. I would recommend using one of them. A simple Google search R1C1 vs A1 style will explain what they are.
In R1C1, Plan!C1:C15 needs to be written as Plan!R1C3:R15C3. So your final code would be
Cells(1, i + last_col).Offset(1, 0).FormulaR1C1 = _
"=VLOOKUP(RC1,Plan!R1C3:R15C3," & i + 1 & ",0)"

Concatenating a formula with a variable inside

I want to concatenate a variable in side a formula, but I cannot get it to work:
For char_index = 1 To 48
Cells(char_index, 2).Formula = "=MID('mysheet'!$B$8;" & CStr(char_index) & ";1)"
Next char_index
The idea is that the formula is added to column B from row 1 to 48 (or B1:B48).
cells(char_index, 2).Formula = "=MID('mysheet'!$B$8," & CStr(char_index) & ",1)"
Even if for your localization the list/formula separator is ";", in VBA you have to use "," and it will write the formula in the appropriate way...

R1C1 Formula result gives twice the given variable

I want to normalize some data and want this normalized data to change dynamically when the data changes, that's why I want to put Formula in Cells that reference different Cells. But that's not really relevant to the problem because even the simplest of Formula (shown below) doesn't work.
Public Sub Test()
a = 15
Cells(a, 8).FormulaR1C1Local = "=Z(" & a & ")S7"
End Sub
The Formula in the Cell(15,8) is =$G30.
I expected the following output: =$G15.
In many forums where people ask how to put variables in a R1C1Formula the answer is: [" & a & "], but this gives a runtime error 1004, so I tried () instead and it doesn't give me any warnings, but the result is not what I expected.
You need understand the R1C1 rules in terms of relative and absolute references.
R15C7 is absolute: Row 15 and Column 7 = $G$15.
R[15]C7 is row relative and means: The row where the formula is in plus 15 rows and Column 7. If formula is in row 15, then = $G30.
RC7 means: The row where the formula is in and Column 7. If formula is in row 15, then = $G15.
Public Sub Test()
a = 15
Cells(a, 8).FormulaR1C1 = "=R[" & a & "]C7" '$G30
Cells(a, 8).FormulaR1C1 = "=R" & a & "C7" '$G$15
Cells(a, 8).FormulaR1C1 = "=RC7" '$G15
End Sub
Translated to your locale Zeile/Spalte Z1S1:
Public Sub Test()
a = 15
Cells(a, 8).FormulaR1C1Local = "=Z(" & a & ")S7" '$G30
Cells(a, 8).FormulaR1C1Local = "=Z" & a & "S7" '$G$15
Cells(a, 8).FormulaR1C1Local = "=ZS7" '$G15
End Sub

Excel VBA Write formula to a cell, refering to a number dependent on I and a sheet also dependent on I

Could someone give me some pointers on how to write this formula right?
Sub WriteFormulaTextAndNumbersDependentOnI()
Dim xNumber As Integer
xNumber = InputBox ("Choose I")
For I = 1 To xNumber
Worksheets("Sheet1").Cells(1, 1 + I).Formula = "= & 2,10 + 0,01 * (I - 1)'" & Sheets(I + 1).Name & "'!B12"
Next I
End Sub
The point here is to write a name in different cells, with a calculated number (2.10 + 0.01*(I - 1)) and text from another sheet:
Lets say we have in Sheet2:
B12 = Hello World
Lets say we have in Sheet3:
B12 = You are cool!
We should then get in Sheet1:
I = 1 would give a value/text in cell B2, with the value/text: "2.10+0.01*(1-1) B12" = "2.10 Hello World"
I = 2 would give a value/text in cell C2, with the value/text: "2.10+0.01*(2-1) B12" = "2.11 You are cool!"
An so on.
Any suggestions?
Appreciate any suggestions
//GingerBoy
use:
Concatenate() worksheet function to mix numbers and text in the same string
Text() worksheet function to format a number to a string
as follows:
Worksheets("Sheet1").Cells(2, 1 + I).Formula = "=concatenate(TEXT(2.10 + 0.01*(" & I & "-1),""0.00""), "" "", " & Sheets(I + 1).Name & "!B12)"
see that:
I used Cells(2, 1 + I) to write in in B2, C2 ... as per your narrative
I used dots (.) as decimal separator: you may want to change all dots into commas as per your decimal separator conventions

get the differences between 2 Integer columns into third column

I have to calculate the difference between the values of 2 columns(firstCol and lastCol) and set those differences in a different column(NextColumn). The row count keeps changing, so I have to calculate the rowCount before calculating the difference. I'm trying to write a loop using Range so that the difference can be calculated but it doesn't seem to work.
For i = 3 To lastRow
Range(Cells(3, NextColumn), Cells(lastRow, NextColumn)).FormulaR1C1 = "=Range(Cells(i, firstCol),Cells(i,firstCol)).Value - Range(Cells(i, lastCol),Cells(i,lastCol)).Value"
Next i
Any help would be greatly appreciated!
Thank you
Nick
For any input of "i" into the formula, you need to use " & i & " when using using a formula="", such as:
"=A" & i & "+B" & i
When you change to just a formula that doesn't input a formula to the cell (math happens in VBA), you can ignore the Excel formatting and "" blocking, such as:
= Cells(i,"A").Value + Cells(i,"B").Value
Make sure to use your loop variable where appropriate, so that you would have an outcome in a loop like:
Dim i as Long
For i = 1 to 10
Cells(i,"C").Formula = Cells(i,"A").Value + Cells(i,"B").Value
Next i
Why looping ? Unless I misunderstood the question, something like this should do the trick:
Range("X3:X"& lastrow).Formula = "=C3-D3"
The formula will adjust.
vba needs to be outside the quotes and you do not need the loop:
Range(Cells(3, NextColumn), Cells(lastRow, NextColumn)).Formula = "=" & Cells(3, firstCol).Address(0, 0) & "-" & Cells(3, lastCol).Address(0, 0)
No loop needed. Excel will change the relative references as needed.

Resources