Replacing a value in an Excel column, based on its value in another sheet - sql

I have a exported Excel sheet from SQL with records for insurance policies held by vendors that visit our office. There are two separate tables that identify the Insurer and the Vendor by a code. The table with the records of all the policies just has the codes for both of them.
Is there a formula that can look at another sheet for a value, and pull in another column from that sheet? Basically there is a key code for each vendor, and I need to replace that code with their actual name from another excel sheet.
Or if there is a simpler way to do this in SQL I could try that too.

In SQL you can JOIN the tables together:
SELECT i.col1,i.col2,v.col1
FROM Insurer i
JOIN Vendor v
ON i.key_code = v.key_code
Adjusting which columns you return as needed, without sample data it's unclear if there needs to be additional criteria in the JOIN.
In Excel you can use VLOOUKUP(), but I prefer INDEX() and MATCH(), below would try to match the value in B1 to a value on Sheet2 in B1:B20, and return the value from the corresponding row from Sheet2 A1:A20:
=INDEX(Sheet2!$A$1:$A$20,MATCH(B1,Sheet2!$B$1:$B$20,0))
There are numerous illustrated examples of either excel method that will be easy to follow.

Related

VBA macro to loop through filter, Copy reference cell, and then paste in separate sheet

I have a table with source data in columns Sheet1!A6:R553.
In Sheet1!A3:R3, I have formulas that pull specific information out of my data table in Sheet1!A6:R553 that I want copied to a different sheet.
Column A acts as my project name column, while column B holds order numbers. In my case, there are multiple order numbers per project.
I am looking for a script to filter and loop through all the unique order numbers in Column B one by one, then copy cells A3:R3 to Sheet2!A12:R12 for as many rows as there are unique order numbers.
Example: Assume there are 5 projects in my data sheet. I will filter the data using another macro to select Project_1. I would then like a command button to active a macro that will filter to the first order number in Column B, copy Sheet1!A3:R3 to Sheet2!A12:R12, then filter to the second order number in Project_1, and repeat the process. This should go on until all unique order numbers have been filtered and looped through.
Thank you for any help you're able to provide.

Table of data want to group, transpose matching values same line

I have a table of data in Excel. Column A contains Names, Column B contains their interest. Each interest has a separate row. I want to take the data from this table and have a single row with the name of the customer and a column for each of their interests. IE RAW Data:
I am looking to take the 4000 row table and grouping by the name. I am unsure how many times each name appears in the list (Once or Fifty times) but I want the interests placed on a single row with each interest in a separate column EG Desired Data:
I have tried the standard transpose....html table....and pivot tables but it will put the interests all in a row along the top regardless if the customer is interested or not and using a record count T/F that means the data sheet in harder to understand then if I leave it as one block and sort by name
Sure I am not alone with this but all searches for the past 2 hrs keep returning pivot/transpose or duplicate items. Any is appreciated
If you don't want to use VBA, you could first add a column, for instance in column C, with the title "InterestNum."
In C2, just put 1.
In C3, put =COUNTIF($A$2:$A2, $A3) + 1. This will find the number interest it is for the person.
Make a lookup column, for instance in column D. In D2, put =A2&C2
Then, make a list of all the people. I assume that you put this list starting in cell A2 of a new sheet. Then put headers starting in B1 so that B1 contains the title "1" and C1 contains the title "2" standing for the interest number and as many columns as you wish.
Then in Cell B2, put the formula =IF(ISNA(MATCH($A2&B$1,data!$D$2:$D$5,0)),"",INDEX(data!$B$2:$B$5,MATCH($A2&B$1,data!$D$2:$D$5,0)))
This assumes that your original data is in the data tab. I only tested with 4 rows, so you would need to change $D$2:$D$5 to have as many rows as you do. This works by looking up a combination of the name and interest number. It first checks to see if that combination exists in the data. If not, it leaves that interest blank. If so, it finds the actual interest by going to the same row of the lookup.
First remove duplicates using standard excel functionality to prevent having the same interest twice for a person.
Now, you could of course use VBA and perform exactly what you need.
However, I suggest that you use the pivot table.
If your data looks something like this...
... just use "Insert | Pivot table" and insert a pivot table to a new worksheet.
Then, configure the columns as follows:
Et voilĂ , there you have all your interests listed only once and if a person shares an interest, there is a "1".
If you would rather use VBA, just comment and I will edit my answer.

Combining different tables in excel by looking up values

Okay, so let's say I have two tables in Excel. I want to create a third table that looks up data from the other two and combines them. I'll explain what I mean
Here is my first table:
It has a number, which is like a primary key, and a corresponding name for each entry.
I also have a second table:
This one just contains some random info, and there's numerous entries. The Client Name is not listed here, but it does have the key for the client that each entry corresponds to.
I want to create a third table that shows me all the Client Info data, but replaces the key with the actual client name. So basically, wherever it sees a key of "1", it'll look that up from the first table and find "Comet" instead.
The desired table would look like this:
What kind of formula would I need to pull data from one table based on a value? Note that all my tables are in different worksheets.
Assuming that the three tables are in Sheet1, Sheet2, Sheet3, this is the formula you need in Sheet3, cell A2
=vlookup(Sheet2!A2,Sheet1!$A2$b$6,2,false)
Copy down as many rows as there are rows in Sheet2
If you need the client info from Sheet2, use this in Sheet3, cell B2 and copy down
=sheet2!B2
As you put an "SQL" tag, I assume that you are also looking for an SQL based answer. So in addition to #teylyn suggestion, you can use the following SQL query if you are working with SQL databases:
SELECT table1.Client_Name, table2.Client_Info
FROM table1
RIGHT JOIN table2 ON table1.ID_Number = table2.ID_Number
Here is what this query does:
The RIGHT JOIN will return all rows from the right table (here table2), with the matching rows in the left table (here table1) according to the condition specified by the ON clause (here, if the ID Number is the same). Then, the SELECT clause will return a table with ONLY the columns specified after the keyword SELECT.

Compare column names in excel sheet with column names in a table in SQL

I have an excel sheet a large number of columns (~500) and a DB table with similar number of columns. I can run SQL queries on the database. What I want to achieve is to find the columns that are in my DB table but not in the spreadsheet. Checking it manually seems really inefficient.
Have you thought of using ODBC functionality within excel? You should be able to open up the table in excel then use count formulas to ensure that the numbers match.
You may try next semi-manual approach:
select top 1 * from [your table]
In SSMS right click to query results and select "Copy with headers"
Paste special->Transpose into Excel (pivot rows to columns)
Select headers in the existing excel, copy them, paste special-> transpose beside previous
Sort and compare columns

Copy information from one spreadsheet to another based on a single factor

A B C
1. c First Last
2. d First Last
3. e First Last
4. c First Last
5. c First Last
I'm trying copy the information from columns B & C over to another worksheet (tab in the same document) based on the letter in column A. I only want first and last names to be transferred if the letter in Column A is a "c". Is there a formula or macro I can use to do this?
I think the easiest answer is to create a pivot table based on on the data on your first worksheet. You can format the pivot table so that it looks similar to your original spreadsheet and then filter your column A to select only the letter "C".
One thing to be careful of, is that when you select your data range for the pivot table, be sure to select the entire columns. that way you can add more names and the pivot table will pick it up.
A couple of notes of concern in the pivot table options:
turn off the totals and subtotals to make the pivot table look more
like your original spreadsheet
set it to update automatically, so that you don't have to be constantly refreshing
Hope this helps!