Reference lookup side column values based on cell value - vba

I've got two worksheets, (a) contains rows which need to be dynamically updated based on a cell (ID), (b) contains over 10k product data by columns.
How do I achieve worksheet (a) to lookup data from worksheet (b) and based on the ID it picks up data from nearby columns. So when I change the ID to eg. 02 it will automatically populate above rows.
Worksheet A
Name
Price
Qty
ID
Worksheet B
ID | Name | Price | Qty
01 | Screw| 0.5 | 500
02 | Nail | 0.4 | 1000
03 | Cap | 0.2 | 800

Yes vlookup will work.
in each cell (except ID) you will need to put this formula:
=VLOOKUP(B1,WORKSHEETB!A1:D4,2,FALSE)
where "B1" is your ID and used to reference you table in worksheet B.
WORKSHEETB!A1:D4 is your table array (your table in worksheetB).
"2" is the column that you are referencing. Example Name is found in column 2 of that table array (doesn't matter where the table is located within the sheet name will always be column 2)
see images attached:

Assuming Name is in Cell A1, Price is in Cell A2, Qty is in Cell A3 and ID is in Cell A4, then:
Cell B2 Formula: =VLOOKUP(B4,'Worksheet B'!A:B,2,0)
Cell B3 Formula: =VLOOKUP(B4,'Worksheet B'!A:C,3,0)
Cell B4 Formula: =VLOOKUP(B4,'Worksheet B'!A:D,4,0)

Related

Replace rows from table that has specific value of multiple columns?

Let's say I have a table like this called MyTable.
| Column A | Column B || Column C | Column D |
| -------- | -------- || -------- | -------- |
| Cell 1 | Cell 2 || Cell 3 | Cell 4 |
| Cell 5 | Cell 6 || Cell 7 | Cell 8 |
And now I am inserting new row into this table that has format like this:
| Cell 1 | Cell 2 || Cell 3 | Cell Something else |.
What I want to do is replace an existing row from MyTable if the row I am inserting has the same value of the first 3 columns of MyTable (column A, column B, column C). As my real table has 250+ columns and
I want to replace rows if they have same value of 5 columns, I don't think INSERT ON CONFLICT UPDATE is good for this. In my opinion, it would be best to DELETE rows that need to be replaced and just INSERT new ones, but I don't know how to write that query.
I was thinking of INSERT ON CONFLICT UPDATE but firstly: I don't think I can specify more columns in ON CONFLICT part, and secondly: I think that I would need to specify 250 columns in UPDATE part, so that also doesn't work for me.
There is no problem specifying multiple columns in the on conflict clause, you just need a unique constraint on those columns. (see demo). As far as you having 250 columns (a highly questionable design, but another question altogether) you have no way around it you must list every column you want updated.

Extracting rows based on a cell value on dynamic data

I am trying to extract rows from an Excel sheet based on the cell value. Consider below example:
Sheet 1 columns:
Group | Question | Option1 | Option2 | Answer
Sheet 2 columns:
Question | Option1 | Option2 | Answer
As demonstrated above, I'm filtering the rows from Sheet 1 based on the Group and populating the data in a separate Excel sheet.
Issue: If data is added in Sheet 1, how can it reflect in Sheet 2 simultaneously.

ADO - how to select column from xls file where two or more columns have the same name?

I have an excel file like this:
| | A | B | C | D |
| 1 | Name 1 | Name 2 | Name 3 | Name 2 |
| 2 | Data | Data | Data | Data |
| 3 | Data | Data | Data | Data |
As you can see, headers of two columns have the same name - Name 2.
My question is, is it possible to tell the ADO engine from which column to select data?
Currently my select looks like this:
SELECT [Name 1], [Name 2] FROM [REPORT7_RAW$] WHERE [Name 1] IS NOT NULL
and ADO picks up the data from column which is listed under column B in excel. In other words it takes the first column which have the given name. Unfortunately I have two columns with the same name and I would like to pull out the data from column D. Is it possible?
I could not find any way to select column by its index rather the name.
You will need to change your connection string so that data header names are not used. The normal connection string would look something like this:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;
Extended Properties="Excel 12.0 Xml;HDR=YES";
You need to change the last bit, HDR=YES, to HDR=NO.
With that type of connection, the columns(fields) then become F1, F2, etc., where F1 = column A, F2 = column B, etc.
This is not ideal, since you are now essentially running the query based on the number of the column rather than the name, but with duplicate column names, this is the only way around that.
Per the comment from #barrowc: This format of the connection string will treat your column names as data. So depending on your query, you may need to include code to filter out the row that contains your column names.

Excel 2007: Data Validation list with filtered data

I would like to have a Validation List in cell D5 on Sheet1. The items in this list should be dynamically based on a table on Sheet2, but I only need the ones where Column2 is TRUE. Below is an example of my table on Sheet2:
Column1 | Column2
Item1 | TRUE
Item2 | TRUE
Item3 | FALSE
Item4 | TRUE
Based on the tabel above the Validation List in cell D5 on Sheet1 should only contain Item1, Item2 and Item4. Is this somehow possible (preferably without using VBA)?
I know I can create dynamic Named Ranges, but I don't know how to filter them, based on another column.
You probably want to create an index to filter out the false.
Easiest way would be to flip your columns, or make column 3 a copy of column 1, then use a formula like.
Column1 | Column2 | Column3
Item1 | TRUE | Item1
Item2 | TRUE | Item2
Item3 | FALSE | Item3
Item4 | TRUE | Item4
{=INDEX($B$1:$C$100,SMALL(IF($B$1:$C$100="TRUE",ROW($B$1:$C$100)-ROW($B$1)+1,ROW($C$100)+1),1),2)}
{=INDEX($B$1:$B$100,SMALL(IF($B$1:$C$100="TRUE",ROW($B$1:$C$100)-ROW($C$1)+1,ROW($C$100)+1),2),2)}
Those would be your first 2 fields, they create an index that gives you the 1st and 2nd result with TRUE.
The 2nd number at the end of the formula determines which item in the list.
when you enter the formula, you can't just type it in with the { }, leave those off and press ctrl+shift+enter to complete the formula and it will put them in for you, this is vital to creating an index formula.
Once you have the index created, it will update based on the true/false values. If you then use that range in a data validation dropdown, it will update with the index.

Insert a value from one cell into another cell from the same record

How can I insert a value from once cell into another cell from the same record for each record in a table, overwriting the original value from the destination?
It's a one time query. Using Sql server 2008
e.g.:
origin|destination
------|-----------
1 | A
2 | B
3 | C
to
origin|destination
------|-----------
1 | 1
2 | 2
3 | 3
update into myTable(destination)
?
Try:
update yourtable set destination = origin;
Without a where clause, this will apply to every row in the table.