excel vba: insert formula with dynamic cell reference using vba? - excel

I am inserting the following hyperlink as a formula using vba:
ActiveSheet.Range("AD" & ActiveCell.Row).Formula = "=HYPERLINK(""S:\Tasks\" & Range("C" & ActiveCell.Row).Value & "\log.txt"",""View Log"")"
This works fine, however if my value in cell C was to change then my hyperlink becomes invalid and won't work because it has retained the value of the cell at the time in which the formula was entered.
I need a way of making a dynamic reference to my cell C in the event that the value should change then my link will also change.
Can someone please show me how to do this? Thanks in advance

Your code is taking the value from column C and building a string using that value that looks like this:
"S:\Tasks\FolderName\log.txt"
Instead, what you want to do is build the following string:
"S:\Tasks\" & C2 & "\log.txt"
To do that, use this VBA code:
ActiveSheet.Range("AD" & ActiveCell.Row).Formula = "=HYPERLINK(""S:\Tasks\"" & C" & ActiveCell.Row() & " & ""\log.txt"",""View Log"")"

Related

VBA code is adding unwanted ' in my formula and returning wrong values [duplicate]

I am trying to write this formula using VBA:
ActiveCell.Value = "=f(R[-1]C[0],Sheet1!" & ColumnLetter & i & ")"
Where ColumnLetter is some variable letter which my macro computes earlier, and f is some function, and i is some number.
The problem is that when I run this, the cell is given this instead: (if ColumnLetter = F, i = 16):
=f(R[-1]C[0],Sheet1!'F16')
but I want:
=f(R[-1]C[0],Sheet1!F16)
Why is VBA or Excel putting those single quotation marks around F16? It does not insert these extra quotation marks if I do not include R[-1][0] as an argument in my formula, but I need to include this.
Help much appreciated!
Its the combination of R1C1 and A1 addressing. You need to pick one method and use it for both parts.
Note that if you type =f(R[-1]C[0],Sheet1!F16) into a cell you will get an error for the same reason.
You say you need to use R1C1 style for the first address, but (assuming this is because you don't want
absolute address) you can use .Offset instead
ActiveCell.Value = "=f(" & ActiveCell.Offset(-1, 0).Address(False, False) _
& ",Sheet1!" & ColumnLetter & i & ")"
The Apostrophe means to Excel that it should interpret it as text.
Write it to ActiveCell.Formula. That way it is recognized as Formula.

Runtime Error 1004 On formula R1C1 vba

I have some trouble applying a FORMULAR1C1 to a range.
This is the code i been using:
Range("AE6:AE" & conter).FormulaR1C1 = "=VLOOKUP(RC[-28],'[" & namebook & "]" & namesheet & "'!C1:C4,4,FALSE)"
where Conter is the number of rows that will applying the formula, namebook is the name of the woorkbook (without path) and namesheet is the name of the sheet that are looking the info for the vlookup.
the reason that i used a variable is that the file update daily and change the name because the date.
When i run this i get the runtime error 1004 what can i solve this? thanks
It's better of you stick with one style of Range.
You want to use FormulaR1C1, and your first VLOOKUP parameter is RC[-28], but the second one you are using C1:C4.
Also, you have a few "extra" ' that needs to be removed.
Modify your code to the line below:
Range("AE6:AE" & conter).FormulaR1C1 = "=VLOOKUP(RC[-28],[" & namebook & "]" & namesheet & "!R1C3:R4C3,4,FALSE)"

Adding a formula in VBA

I have created a code that adds a new line to my spreadsheet with the information that users enter into a user-form. The problem I'm having is that I have formulas in two of the columns that I need to be carried into the new rows. I have tried several different ways to do this but keep receiving an error message. The formula I need carries is
=IFERROR(INDEX(Equipment!$E$3:$E$1000,MATCH($B5&"-"&$A5,INDEX(Equipment!$B$3:$B$1000&"-"&Equipment!$A$3:$A$1000,0),0)),"")
The values that change line to line are the B and A cells after the MATCH command (in this case $B5 and $A5). Can anyone help me figure out how to convert this to VBA syntax so that I can add it to my code? Thanks in advance!
I Converted this formula, and tested it in a loop.
The formula is copied to Column D from rows 11 to 20, where $B5 and $A5 change according to whatever row they are (I hope I understand what you meant).
Sub ConvertFormula()
' modify to where (worksheet) you want to copy this formula
Set eqp_sht = ActiveWorkbook.Worksheets("Equipment")
' modify this according to whatever rows and column you want this formula copied
For i = 11 To 20
eqp_sht.Cells(i, 4).Formula = "=IFERROR(INDEX(Equipment!$E$3:$E$1000,MATCH($B" & i & "&-$A" & i & ",INDEX(Equipment!$B$3:$B$1000&-Equipment!$A$3:$A$1000,0),0))," & Chr(34) & Chr(34) & ")"
Next
End Sub

Excel vlookup range issue while adding using VBA

1st VBA code: I am using vlookup formula in VBA, below is the formula I placed in VBA:
Rng.Formula = "=IF(RC" & SeseCol & " ="""","""",IF(LEFT(LOWER(RC" & SeseCol & "),4)=""none"","""",VLOOKUP(LEFT(RC" & SeseCol & ",(FIND("" "",RC" & SeseCol & ",1)-1)),'[HMO Base Rule Picker - Formula.xlsm]BaseRule'!C[-10]:C[-5],2,0)))"
1st VBA output: From the above code I get the below formula in excel which is correct:
=IF($C17 ="","",IF(LEFT(LOWER($C17),4)="none","",VLOOKUP(LEFT($C17,(FIND(" ",$C17,1)-1)),BaseRule!B:G,2,0)))
2nd VBA code: However I wanted make C[-10]:C[-5] to a constant range and tried changing to $B:$G as below VBA code:
Rng.Formula = "=IF(RC" & SeseCol & " ="""","""",IF(LEFT(LOWER(RC" & SeseCol & "),4)=""none"","""",VLOOKUP(LEFT(RC" & SeseCol & ",(FIND("" "",RC" & SeseCol & ",1)-1)),'[HMO Base Rule Picker - Formula.xlsm]BaseRule'!$B:$G,2,0)))"
2nd VBA output: Then I am getting the below formula from the above code which is not working. I am not getting the same as 1st VBA output formula, am I missing something on the 2nd code?
=IF(RC7 ="","",IF(LEFT(LOWER(RC7),4)="none","",VLOOKUP(LEFT(RC7,(FIND(" ",RC7,1)-1)),BaseRule!$B:$G,2,0)))
Use C2:C7 instead in place of C[-10]:C[-5].
Final formula would be:
Rng.FormulaR1C1 = "=IF(RC" & SeseCol & " ="""","""",IF(LEFT(LOWER(RC" & SeseCol & _
"),4)=""none"","""",VLOOKUP(LEFT(RC" & SeseCol & ",(FIND("" "",RC" & SeseCol & _
",1)-1)),'[HMO Base Rule Picker - Formula.xlsm]BaseRule'!C2:C7,2,0)))"
In R1C1 Notation [] brackets are used to indicate relative referencing.
To indicate absolute reference, you just need to indicate the actual column or row number. So for Column B that is C2 which is the 2nd column. For Column G which is the 7th column would be C7.
Also take note that I used FormulaR1C1 property above.
That is the correct property when you're using the R1C1 notation in your formula.

Referring to Range address in a VBA entered formula

Is it possible to enter a formula in VBA that refers to an address defined by a named range?
If my range is called "Table" I would reference a cell in the following way (with counters "n_row" and "n_col")
Range("Table")(n_row, n_col)
How would I do this in a Cell formula?
The best I could come up with is this, which clearly didn't work.
Range("Test").Formula = "=10 - Table(n_row, n_col)"
Use the Index Formula
Range("Test").Formula = "=10 -INDEX(Table," & n_row & "," & n_col & ")"

Resources