VBA: Using VLookUp to Set Dynamic Values in loop? - vba

New VBA user here...
I have set up a Macro that runs for each file in a folder, as a loop.
In one part of the macro, there is an equation that contains a value that differs for each file.
Here is the equation (the value that changes for each file is 0.2483, everything else stays the same):
ActiveCell.FormulaR1C1 = "=(((" & signal_array(Element, 1) & ")-
R[-83]C)/R[-85]C)*1000*0.2483"
I have set up a table that lists each file name and its corresponding value in another workbook. I have attempted to use vLookUp within the equation to find the value based on the file name - the file of which is active.
Here's what I have so far, for which I get a "Run-Time Error: 1004" on:
ActiveCell.FormulaR1C1 = "=(((" & signal_array(Element, 1) & ")-
R[-88]C)/R[-90]C)*1000*(=VLOOKUP(" & ActiveWorkbook.Name & ",'[Calibration
Curves.xlsm]Sample Weights'!A2:B10,2,FALSE))"
Suggestions on how to make this work?

Try getting rid of the = in the formula and also use consistent cell notation (i.e. don't mix A1 notation with R1C1 notation):
ActiveCell.FormulaR1C1 = "=(((" & signal_array(Element, 1) & ")-
R[-88]C)/R[-90]C)*1000*(VLOOKUP(" & ActiveWorkbook.Name & ",'[Calibration
Curves.xlsm]Sample Weights'!R2C1:R10C2,2,FALSE))"

Related

how to use cells for a Hyperlink destination/path and workbook name VBA?

I have a folder on the desktop
C:\Users\ME\Desktop\folder1\folder2\
In workbook 1 have column A which was used to create folders within folder 2.
Column N was then used to create filenames for each newly created workbook.
Every Row became a new workbook. (~3200 Workbooks, each in their own folder)
How can I either write a VBA or a Formula to quickly create the hyperlinks.
The reasoning is that the Master workbook will be used to narrow down the the search results and give immediate access to that location of the database.
the path I have is,
C:\Users\ME\Desktop\folder1\folder2\ & Column A & "/" & Column N & ".xlsm"
I tried using a formula
=HYPERLINK('C':\Users\ME\Desktop\folder1\folder2\ & [#SITE] & "/" & [#FULLNAME] & ".xlsm",[#FULLNAME])
This doesn't seam to work, the Cells I'm referencing is stated as the column headers. When I hit enter the entire column downward was then filled with "#NAME?" and a error stating "the formula contains unrecognized text"
Flip "/" into a "\"
Also look at this example for guidance:
=HYPERLINK(C10&"\"&A10&"\"&B10&".xlsx")
Perhaps use formula like this in New column and use it for the whole range:
=HYPERLINK("C:\Users\ME\Desktop\folder1\folder2\ & $A2 & "/" & $N2 & ".xlsm","PathToFile_" & $N2)

Way to set a formula using counter as cell reference in vba?

I am trying to set a cell's value to a formula, but the formula includes a counter because the column is always set, but the row of the cell varies during every initiation of the macro. How do I set this up? This is what I have, but it keeps giving me an object-defined error.
counter = activecell.row
Range("A1").FormulaR1C1 = "=VLOOKUP(AH & counter &, reference)"
The reference part is set up correctly, but I don't know how to get the first part to work.
Two things, you do not want R1C1 format and vba variables need to be outside the "" and concatenated with &
Range("A1").Formula = "=VLOOKUP(AH" & counter & ",...

Using .Formula with variable in cells object

I've tried a variety of concats and uses of "" but I cant get .formula to work in the following code. The macro runs without error but it does not populate my sheet. I feel like this: Excel VBA formula with variables is what I am going for but main_row does not appear to be getting assigned to the variable when it is inside the .formula.
main_row = main.Range("b6").Row
calc_col = main.Range("k6").Column
main.Cells(main_row, calc_col).Formula = " = j & main_row / i & main_row "
Is it possible to use .formula with cells object?
Would rather not use application.worksheetfunction because i want end user to see the formula
Anyone have a good source to explain the proper way to qualify variables within .formula?
Try the below, I believe you were nearly there, but you inserting an unnecessary space before the equals sign + the quotes were out.
main.Cells(main_row, calc_col).Formula = "= J" & main_row & " / I" & main_row

Referring Cell range contained in variable in SUM formula

I have two cell addresses stored in my two variables
saddr and eaddr (starting and ending cell addresses respectively)
I have to find the some of all cells in between the range and have the value assigned to another random cell. My code below throws an error. I am pretty sure i am not using the variable containing the cell addresses in the right format. Please help
Code:
saddr = Cells(x, 3).Address
eaddr = Cells(x, (3 + 6)).Address
Worksheets("Sheet2").Range("C2").Select
ActiveCell.Formula = "=Sum(Sheet1!:"&saddr&":"&eaddr&")"
The last line throws up this error. Can you tell me how to use the variable cell references correctly in the formula?
You probably want
ActiveCell.Formula = "=Sum(Sheet1!" & saddr & ":" & eaddr & ")"
Even if this gets you going, you better check/fix a few things.
I strongly recommend you check this.

Summing cells from another sheet using offset

I'm trying to get this code to work for summing cells:
Worksheets("Sheet2").Range("C3").Offset(i, j).Formula = "=Sum("
&Worksheets("Sheet1").Range("A3").Offset(2*i,j).Address & ":" &
Worksheets("Sheet1").Range("A7").Offset(2*i,j).Address & ")"
It keeps giving me the right cells but from the wrong sheet. So for the first iteration I get sum(A3:A7) in cell C3 of Sheet2 but the A3:A7 stays referenced to Sheet2 not Sheet1.
Thanks!
You need to specify the name of the sheet in the formula too. Your code will work if you write it like this:
Worksheets("Sheet2").Range("C3").Offset(i, j).Formula = "=Sum(Sheet1!" & _
Worksheets("Sheet1").Range("A3").Offset(2 * i, j).Address & ":" & _
Worksheets("Sheet1").Range("A7").Offset(2 * i, j).Address & ")"
Try this code - it uses the External:=True parameter of .Addressto retrieve the full address. While this also includes the workbook name, Excel will remove this automatically so you end up with Sheet1!A3. Also note that I used the range A3:A7 as source as .Address can handle multi-cell ranges and you don't need to take care of it manually:
Sheets("Sheet2").Range("C3").Offset(i, j).Formula = "=SUM(" & _
Sheets("Sheet1").Range("A3:A7").Offset(2 * i, j).Address(External:=True) & ")"
Be aware that hard coding references such as A3 can lead to bugs in the long run, as the user (or even the developer at some stage) might modify the sheet structure. It is best practice to use named ranges, i.e. create a named range for each cell/range you refer to and then access it in VBA with SheetX.Range("rngStartCell") or similar!