Excel Referencing cell entries for use in formula - vba

I'm not sure exactly what to call what I'm trying to do, so searching it has been tough. Basically I have a table of equations, each row has a different equations/references a different column, but all of them reference the same range of rows. i.e. Eq. A = average(A200:A400), Eq. B = sum(C200:C400), etc...
From file to file the range of rows changes, so what I want to do is be able to do is enter the start and end rows into cells and have them auto populate the equations. If anyone could tell me how to do it just for one cell and not an entire table, I could figure it out from there.
Thanks!

Sounds like the INDIRECT function would accomplish this. It allows you to enter in text to be interpreted as a cell reference.
For instance, lets say you wanted the range to cover A200:A400 for a given sheet, and you wanted that desginated in cell A1 of that sheet. In cell A1 you would just type in "A200:A400" then, in the actual equations, you would have:
=AVERAGE(INDIRECT(A1))
You can obviously split this further down, but thats the concept of it.

You could create a form with a few text boxes. Enter the start and end row. Then your code could go through and enter the formula.
Something like this.
Dim lRow as long
Dim lEnd as long
lRow = val(txtBoxStartRow.text)
lEnd = val(txtBoxEndRow.text)
ws.Range("A" & lEnd + 1).Formula = "=average(A" & lRow & ":A" & lEnd & ")"
ws.Range("C" & lEnd + 1).Formula = "=average(C" & lRow & ":C" & lEnd & ")"

This should do:
=AVERAGE(INDIRECT(ADDRESS($B$1;ROW())):INDIRECT(ADDRESS($B$2;ROW())))
In that code I'm assuming cells B1 and B2 contain the limits (you can replace these references with hard number), to use your example: B1 = 200 and B2 = 400.
If you then place this code in any row, you'd get average("rowNumber"200:"rowNumber"400).
Address() gives you the right range reference
Indirect() makes a range out of it
Then you can wrap it in whatever function you like.

Related

VBA script to insert INDEX/MATCH formula with dynamic reference/arrays

I'm looking to simply insert an INDEX/MATCH formula in the column next to an existing selection (this will be part of a larger Sub). The reference/look-up array in the formula needs to be dynamic.
So far I've managed to successfully insert the formula. However, the reference/lookup-arrays have been manually input by me as I know the current Range (which is likely to change):
Selection.Offset(0, 1) = "=INDEX($J$3:$J$31,MATCH(INDIRECT(""RC[-2]"",0),$H$3:$H$31,0))"
How can I amend the $J$3:$J$31 and the $H$3:$H$31 to update dynamically? I've had success separately with something like 'Range("J3", Range("J3").End(xlDown))', but I can't seem to work it into the code above.
You can integrate variables into the formula, it becomes a bit tricky, and I imagine there are better ways to accomplish this, but it's the method I use when I need dynamic ranges in formulas in VBA. I'll provide an example below that should help:
I'm assuming the J3 and H3 will remain the same, but the end of the range is what you expect to change. If this is not the case, let me know.
You're essentially replacing the "31" in your range reference with " & [variable] & " (quotes included).
EDIT: I use Sheet1 as an example for the row count; so just update that to whatever the applicable sheet & range would be.
Dim rCount As Long
rCount = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
Selection.Offset(0, 1) = "=INDEX($J$3:$J$" & rCount & ",MATCH(INDIRECT(""RC[-2]"",0),$H$3:$H$" & rCount & ",0))"

Excel - how to increment formula reference along a row

I have really long strings of text saved in roughly 1000 cells down Column A.
I have created the following formula (some VBA included for FindN), placed in cell B1:
=MID($A1,FindN("978",$A1,1),13)
I can copy this formula down Column B just fine. However, I also want to copy this formula across each row, so for example the formulas for the cells across the row should be as follows:
Cell C1: =MID($A1,FindN("978",$A1,2),13)
Cell D1: =MID($A1,FindN("978",$A1,3),13)
Cell E1: =MID($A1,FindN("978",$A1,4),13)
etc...
If I copy the formula in Cell B1 across the row, it will copy across =MID($A1,FindN("978",$A1,1),13) - but I need the "1" to increment by 1 each time.
I think I'd need to adjust the formula slightly, but a bit lost on how to do this...
Any help would be greatly appreciated. Please let me know if I should clarify further.
Use COLUMN() - it gives the column number of the current cell. You can offset this as required.
In this case for your incrementing number use COLUMN() - 1, so that in B you have 1, C; 2 etc.
You need use CELL formula to get current column number. Try something like this:
=MID($A1,FindN("978",$A1,CELL("column";A1)+1),13)
I dont have English Excel and im not sure first argument in CELL forumla is "column"
Try this :
Sub Fill()
With Sheets("Sheet1")
For i = 1 To .Cells(Rows.Count, "A").End(xlUp).Row
.Cells(i, 1).FormulaR1C1 = "=MID($A1,FindN(" & _
Chr(34) & "978" & Chr(34) & _
",$A1," & i - 1 & "),13)"
Next i
End With
End Sub

How to autofill formula for known number of columns but variable number of rows in excel macro

I writing a macro within which I need to autofill some rows with formulas, across multiple columns.
The number of columns is fixed, but each time the macro runs, the number of rows is variable. I use the "record macro" function and the current macro only ever fills my rows to row 16. Below is the code:
Range("D3:P3").Select
Selection.AutoFill Destination:=Range("D3:P16")
I obviously need to change the "P16" to something dynamic.
I have tried to use the following:
Dim LR As Long
LR = Range("D3:P3" & Rows.Count).End(xlUp).Row
Range("B3:P3").AutoFill Destination:=Range("B3:P" & LR)
I am unsure whether the "Dim LR as Long" has to be placed at the very beginning of my macro - or can it just be placed anywhere?
I am getting an error anyway with what i attempted above giving me an "autofill selectio error" (sorry i cant remember the exact error message.
Would someone be able to point me in the right direction?
LR can be declared anywhere before where you first use it, but it's best to do it at the beginning. Your range for LR is incorrect.
LR = Range("D3:P3" & Rows.Count).End(xlUp).Row
Should be
LR = Range("D3:P3").End(xlUp).Row
You should use xlDown if you are trying to find the end of a range BELOW D3:P3
LR = Range("D3:P3").End(xlDown).Row
Would give you the last row with data in all columns D:P in it below D3:P3
I think you're looking for this:
LR = Range("D3:P" & Rows.Count).End(xlUp).Row
but note that this finds the last row with any content in Column D - if there are later rows with content in Cols E-P but not in Col D then those rows will be ignored.
So I used the information provided to me and managed to get the following:
Dim LR As Long
LR = Range("C3:P" & Rows.Count).End(xlDown).Row
Range("D3:P3").AutoFill Destination:=Range("D3:P" & LR)
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$D$2:P" & LR), , xlYes).Name = _
"Table10"
This allowed me to count the number of rows that had already been populated in column "C", and then take the formulas that already existed in cells D3:P3 and autofill them down through the range until the last populated row of column C.
I then used that structure to make the whole range a table, in this case named "Table10".
Great stuff guys - your help allowed me to get exactly what I wanted. Thanks

VBA vlookup not changing values

Hello I am new to the form and am having trouble with creating a VBA that performs a vlookup on a user selected case in A2. In the attached example the values change in some cells (D2-F2) based on the selection, but the formula results, so I can not perform a select case later on because there is no value, only a formula. The value does appear in B2 and C2 but does not change when the user selects a different case. Thank you.
https://app.box.com/s/qrs0r5gf0vth6wpbj1w9
' Step 3 Add additional selection information
Dim LastRowNo As Long
LastRowNo = Cells(Rows.Count, "CA").End(xlUp).Row
Worksheets("annovar").Range("B2").Value = Evaluate("=VLOOKUP(A2,CA5:CB" & LastRowNo & ",2,0)")
Worksheets("annovar").Range("C2").Value = Evaluate("=VLOOKUP(A2,CA5:CC" & LastRowNo & ",3,0)")
Worksheets("annovar").Range("D2").Formula = "=IFERROR(VLookup(A2,CA5:CD" & LastRowNo & ",4,0),"""")"
Worksheets("annovar").Range("E2").Formula = "=IFERROR(VLookup(A2,CA5:CE" & LastRowNo & ",5,0),"""")"
Worksheets("annovar").Range("F2").Formula = "=IFERROR(VLookup(A2,CA5:CF" & LastRowNo & ",6,0),"""")"
Check the ranges that you're using in the VLOOKUP. I think you may be looking in the wrong column. There isn't anything in the Column("CA") so LastRowNo = 1 and Range("CA5:CB1") is empty. The information that you're looking for I think was in Range(BP8:BQ12") because of the way that you're inserting and deleting rows every time you run it.
Would it not be better to create a new sheet or clear a different sheet every time you run the macros? That way the code wont be so awkward?

Excel VBA - Sum function

I'm trying to calculate the sum of my columns (column I). From 1 to the last record in I it has. When I record a macro I get this as output, but this is gibberish to me.
ActiveCell.FormulaR1C1 = "=SUM(R[-11]C:R[-4]C)"
I found another topic and there they said
LastRow = .Range("I" & .rows.Count).End(xlUp).row
Range("I"&LastRow) = "SUM(I1:I...)"
Except in my case, I can't figure how to enter the lastrow of I in it.
All help is welcome :)
There are two ways of referencing a cell - 'R1C1' and 'A1'. The former works like co-ordinates, with a relative number of rows (R) and cells (C).
The other reference style refers to the cell name on the sheet - B6, F67 etc.
Let's say you want to put your Sum() in cell B1 and LastRow has a value of 6:
ActiveSheet.Range("B1") = "=Sum(I1:I" & LastRow & ")"
Would insert the following function in cell B1:
=SUM(I1:I6)