VBA vlookup not changing values - vba

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?

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))"

Combining two formulas in one cell

I have two cells in my excel workbook that contain vba to perform a formula. What I'm trying to do is put two formulas in one cell instead of two separate ones. I have provided the code below for what I have so far. The outcome I want in cell C1 is to say "AMTS:(count) Cleaners:(count)" both together not separate. I tried just putting the & between the formulas and it kept returning a false value lol.
Thanks!
ws.Range("$C$1").Formula = "=""AMTs:("" & COUNTIF(E:E,""AMT"")&"")"""
ws.Range("$D$1").Formula = "=""Cleaners:("" & COUNTIF(E:E,""Cleaner"")&"")"""
ws.Range("$C$1").Value = "=""AMTs:("" & COUNTIF(E:E,""AMT"")&"")""" & "="" Cleaners:("" & COUNTIF(E:E,""Cleaner"")&"")"""
This is the output from the macro.
or
ws.Range("$C$1").Formula = "=""AMTs:("" & COUNTIF(E:E,""AMT"")&"") Cleaners:("" & COUNTIF(E:E,""Cleaner"")&"")"""

Excel Referencing cell entries for use in formula

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.

Convert Excel array formula VBA

I'm trying to do "vlookup" with 2 different criteria(Column A and G values) using "Index" and "Match" functions.
and here is the line i used for the Excel command.
=INDEX(Database!A:KG,MATCH(1,(Database!A:A='TempSheet'!A2)*(Database!G:G='TempSheet'!G2),0),10)
How would I do it with VBA? It's keep giving me the error message "Compile error: Expected end of statement".
Selection= _
"=Index(DB.Range("A:KG"), Match(1, (DB.Range("A" = Temp.Range("A" & i).Value)) * (DB.Range("G" = Temp.Range("G" & i).Value)), 0), 10)"
Thanks
Every time you use a spreadsheet formula inside a VBA code, you need to precede it with <Excel.WorksheetFunction.> or <Application.WorksheetFunction.>.
For example:
Application.WorksheetFunction.Match
instead of Match only.
I have had little luck getting array formulas to work correctly via VBA, and use the 'IFERROR' as a workaround like so:
=IFERROR(VLOOKUP(A1, Database!A:Z,1,FALSE),VLOOKUP(B1, Database!A:Z,1,FALSE))
This function will attempt to match A1, and in case of a #VALUE error, it will match B1.
To get this kind of formula populated on a sheet in VBA, you can loop down your sheet using the '.formula' approach.
' get length of source data
Dim RowCount As Long
RowCount = ThisWorkbook.Sheets("Database").Cells(Rows.Count, 1).End(xlUp).Row
' now starting from row 2 to preserve headings
For i = 2 To RowCount
ThisWorkbook.Sheets("Summary").Cells(i, 1).Formula = "=IF(ISERROR(SEARCH(""ISO"",V" & i & ")),""Order type not supported"",""Transit"")"
ThisWorkbook.Sheets("Summary").Cells(i, 6).Formula = "=IFERROR(TEXT(VLOOKUP(B" & i & ",Database!A:N,7,FALSE),""dd-mmm-yyyy HH:MM AM/PM""),"""")"
Next
Not exactly what you were after, but hope it helps!

How to find the last value with specific conditions?

Sheet 1 column A has the following values (it has around 3000 records. I’ve given the below sample values). I need to find the last value of a specific text.
RVT-01
RVT-02
RVT-03
RVT-04
RVT-05
RVT-06
RHT-01
RHT-02
RHT-03
RHT-04
RHT-05
ROI-01
ROI-02
ROI-03
SWO-01
SWO-02
SWO-03
SOR-01
SOR-02
SOR-03
SOR-04
SOR-05
SOR-06
SOR-07
Using VBA code
If enter short tex in sheet1.cells(2,2) = SWO , I need the last value in sheet1.cells(2,4)=SWO-03
If I enter sheet1.cells(2,2) = RHT , I need the last value in sheet1.cells(2,4)=RHT-05
If I enter sheet1.cells(2,2) = RVT , I need the last value in sheet1.cells(2,4)=RVT-06
If I enter sheet1.cells(2,2) = SOR , I need the last value in sheet1.cells(2,4)=SOR-07
What would be the VBA code for the above process?
As Skip Intro suggested, there is no need for VBA: in Column B, put a formula like this:
=IF(IF(LEFT(A1,3)=LEFT(A2,3),1,0)=0,RIGHT(TRIM(A:A),2),"") (to get the just the max number):
or
=IF(IF(LEFT(A1,3)=LEFT(A2,3),1,0)=0,A:A,"") (to get the complete contents of the cell)
Both will show you the highest values. Then you could AutoFilter that column, hiding the blanks and voila :)
Or
=IF(IF(LEFT($A1,3)=LEFT($A2,3),1,0)=0,NA(),"")
will enable you to use SpecialCells in VBA to get a range that you can interrogate for the maximum values in each group, as below:
Sub test()
Dim rng As Range
Dim cell
Range("B1:B" & Range("A65536").End(xlUp).Row).Formula = "=IF(IF(LEFT($A1,3)=LEFT($A2,3),1,0)=0,NA(),"""")"
Set rng = Range(Range("B1:B" & Range("A65536").End(xlUp).Row).SpecialCells(xlCellTypeFormulas, xlErrors).Offset(0, -1).Address)
For Each cell In rng
Debug.Print cell.Address & " =" & cell.Value
MsgBox cell.Address & " =" & cell.Value
Next
End Sub
For more information on the SpecialCells magic tricks, see How to delete multiple rows without a loop in Excel VBA.