Linking worksheets based on a pivot column - vba

I have a spreadsheet linking issue. For example let the data in the work books look like this
Book1
A 1 aaa
B 2 bbb
C 3 ccc
Now I link this sheet to another sheet by reference and it has same values as in here
Book 2
A 1 aaa
B 2 bbb
C 3 ccc
Now if I add another column in Book 2 like below
Book 2
A 1 aaa 10
B 2 bbb 20
C 3 ccc 30
Now if I link this new column back to Book1 it looks like below
Book 1
A 1 aaa 10
B 2 bbb 20
C 3 ccc 30
But now if I sort column 1 in book 1 it doesn't sort the column4 because that is referenced to Book 2 which looks like below
Book 1
C 3 ccc 10
B 2 bbb 20
A 1 aaa 30
But expected output is
Book 1
C 3 ccc 30
B 2 bbb 20
A 1 aaa 10
But I want the sorting to happen on the new column that is added as well. A solution I could think of is to share a single sheet. But that doesn't serve my purpose so I need a linking which is sophisticated enough to sync across the books. Is there a pivot column based linking where the column1 in two books are taken as a reference when the manipulation happens on any sheet. Any help is appreciated.

I'm not sure why this isn't working for you, but I may be misinterpreting how you've set this up.
I've set up two workbooks, each with identical data for the first three columns, the fourth column in Book1 being linked to the same column in Book2.
Here, you can see the formula view after I sorted on Column A.
This will not work properly if you had previously enabled Auto Filter Before putting in the links, since the Auto Filter will not have extended the filter range by itself. To fix this particular issue, simply disable and re enable the Auto Filter (You should see the drop-down icon on Column D after that).

I've run into this type of "missorting" problem before. My solution was always to copy and paste the formulas in a different sheet as values.
Otherwise you could make column D dynamic through VLOOKUP and not reference hard cell numbers.

Related

How to merge two Spreadsheets into one in google spreadsheets

There are two Spreadsheets namely main and secondary. Main has 3 sheets in it and Secondary has 1 sheet under it. For simplicity I'll show modified version of them both under
Main spreadsheet
Name Qrating Brating Crating
A 8 5 4
B 5 2 1
C 6 7 9
Secondary spreadsheet includes personal details of same people present in Main spreadsheet as seen below.
Name Age Rank qualified
A 23 2 yes
B 45 22 yes
C 44 23 yes
I have looked up ways to merge and combine but all seem to be ways in combining data vertically, but what i want is all the data in secondary to be added in Main file in accordance to the name. I don't seem to find the solution online where data is filled horizontally after tallying a common column between two files. if anyone can give solution or guide me to a direction that would be very helpful. Thankyou.
end result should be like this when merged
Name Qrating Brating Crating
A 8 5 4
B 5 2 1
C 6 7 9
Name Age Rank qualified
A 23 2 yes
B 45 22 yes
C 44 23 yes
Name Age Rank qualified Qrating Brating Crating
A 23 2 yes 8 5 4
B 45 22 yes 5 2 1
C 44 23 yes 6 7 9
It is very easy to do this with click on feature in Microsoft Excel, I was expecting the same in Google sheet but it seems the add-ons help for this as I didn't find any tool in sheets itself. There are many add-ons to achieve this, I am using Merge Sheets add on for this.
You can merge multiple sheets form different spreadsheets like this
=query({
IMPORTRANGE("ID_1"; "SheetX!A2:E");
IMPORTRANGE("ID_2"; "SheetX!A2:E");
IMPORTRANGE("ID_3"; "SheetX!A2:E")
};"SELECT * WHERE Col1 IS NOT NULL")
and then adapt query as you need. Wait for an example from your side. Sincerely yours.

How do i keep a row connected together(moving together) while shifting/adding row above it?

I have an Excel file containing two sheets:
Teams (which contains details of teams and time schedule of their matches)
Results (contains calculation and number of matches and results, etc).
WhatResultsdid is that each game that has launched status on Teams it gets on Results sheet and thenResultsinput the number and calculation on that sheet.
My problem is that when there is a game inside two started games, when it starts the new name comes and shifts the name row below it and keeps the details of the row below it for itself, while it should be to next of it, here is my example:
Sheet1 Teams
A B
1 **Names** **Status**
2 TEAM A Launched
3 TEAM B Pending
4 TEAM C Pending
5 TEAM D Launched
2 Results
A B C D E
1 **Names** **1st Half goals** **2nd half** **total** **points**
2 TEAM A 1 2 3 13
3 TEAM D 3 1 4 10
So what happens here is that if change the status of TEAM B on Sheet1 of Teams it will be appear on 2 Results and it will take the row input of TEAM D.
It will be like this:
2 Results
A B C D E
1 **Names** **1st Half goals** **2nd half** **total** **points**
2 TEAM A 1 2 3 13
3 TEAM B 3 1 4 10
4 TEAM D
Is there any solution? please let me know.
This is the formula on Sheet 2 Results Cell A2
=IFERROR(INDEX(TEAMS!A$2:A$550,SMALL(IF(TEAMS!B$2:B$550="Launched",ROW(TEAMS!B$2:B$550)-ROW(TEAMS!B$1)),ROW(TEAMS!B2))),"")
and Cell B & C & E there is no formula only manual input, and for Cell D =SUM(C2,B2)
I found an answer for my question, thanks to GraH - Guido & Vletm users on Chandoo Forum, who helped me find the answer.
I found 2 answers both helpful and working for my case, you can find them here and here
Thanks.

Removing duplicates from many excel sheets

I got a question if there is any fast way to remove duplicate rows across two excel spreadsheets. After searching I can do it by comparing the same rows in the spreadsheets (VBA). But I want to check whether the row from one is included anywhere in two. If exactly the same row exists in two it should be removed. So far I can do it if they are the same rows (e.g. 1 and 1).
Thanks in advance for any kind of help.
I can think of a workaround for this:
Create a column at the end of each row which is concatenation of all the columns of that particular row: Lets sat below are the two tables on the two excel sheets:
sheet1
A B C D(Concat)
1 2 3 123
4 5 6 456
7 8 9 789
1 3 5 135
4 3 2 432
sheet2
A B C D(Concat)
2 3 4 234
1 1 1 111
1 2 3 123
2 2 2 222
4 5 6 456
We will now identify the duplicate rows based on the last concatenated column. Using the formula =IF(ISNUMBER(MATCH(D4,Sheet1!D:D,0)),"DUP","NONDUP") in the second sheet, we can identify the rows which are already present in sheet1 irrespective of the sequence of the row in sheet1 wrt sheet2.
Result on Sheet2 shows up as below:
A B C D E(Result)
2 3 4 234 NONDUP
1 1 1 111 NONDUP
1 2 3 123 DUP
2 2 2 222 NONDUP
4 5 6 456 DUP

Microsoft Access 2007 Report with Conditional Columns

I am looking to make a very simple report to condense and show data side by side. All of the examples of reports I find are only row by row.
The query I will use will only have three schema "Company, Model, Total"
The format I am trying to get to is
Company Model Total Company Model Total
A 123 2 B 123 4
A 222 3 B 333 3
A 444 7 B 444 7
The idea is to present the information in a way that multiple companies side by side can compare inventory of the same model and find discrepencies. Ideally the report would eventually group all Model's that span every company at the top, but thats a next generation problem.
I have attempted conditional formating on multiple "Company" boxes, but the conditionals do not seem to be applying properly or for some reason every "Company" box is adopting the same conditionals.
I think you want a crosstab query grouping by model (the rowHeader), company as the column header, and first(total) as the value.
The results should look like
model A total B total
123 2 4
222 3
333 3
444 7 7
then you can create another query based on the crosstab results to calculate the difference between company totals, if you want.
You have to do this in two steps:
Build a query that gives you:
Company Model Total
A 123 2
A 222 3
A 444 7
B 123 4
B 333 3
B 444 7
Let's call q this query.
Build a second query
SELECT q1.Company, q1.Model, q1.Total, q1.Company, q2.Model, q2.Total
FROM q AS q1 INNER JOIN q AS q2 ON q1.Model = q2.Model
WHERE q1.company < q2.company;
This will give you:
A 123 2 B 123 4
A 444 7 B 444 7
(There are no matching data for models 222 and 333)

How can I identify the text values that have the lowest row IDs across 4 columns?

I found a few articles that are close, but not the same as what I am trying to do. I have an Excel file that has 4 columns of duplicated data, each column is sorted based on a numeric value that came from a different worksheet.
I need to identify the 25(or so?) rows where the value of the four columns match, and the row ID is the lowest. There will be roughly 250 rows of data to sift through, so I only really need the top 10%.
I don't HAVE to approach it this way. I can dump this data into Access if this cannot be done in Excel. Or I can assign columns next to each text column (a way of assigning IDs to each field in column 1, 2, 3, and 4) and use those values. The approach is negotiable, as long as the outcome works.
Here's what my data looks like in Excel:
A B C D
abc bcd abc def
cde fgh def bcd
def def bcd abc
bcd hji xyz lmn
So in this case I would want to highlight (or somehow identify) the value "def" because it appears closest to the top of all 4 columns, hence it has the lowest row ID. The value "bcd" would be second on the list since it also is identified in all 4 and has a low row id.
Any suggestions would be appreciated. I know SQL fairly well, so if you think dumping it in a DB would be best and you can suggest a query that would be awesome. But ideally... keeping it in Excel would be the least amount of work for me. I'm open to formulas, conditional formatting, etc.
Thanks!!
I THINK I came up with a fairly cool solution...
So, supposing you have this data in columns A-D, begining in cell A2, say.
Now, you know that you ONLY want values if they already exist in column A - Otherwise they're not in all 4 columns.
So:
In E2, type in the formula =Row() - This basically says where A's value is located
In F2, type in =Match($A2,B:B,0) - This will find the first match for A2's value in columns B
Drag that formula across to G2 & H2 (to find the first match for A2's value in C & D respectively).
In I2, type in the formula =Sum(E2:H2)
Now, drag E:H down for your entire dataset.
So, If H = #N/A, that means the values weren't in all 4 columns
And the lower the value for H, the lower the rank of the match - (Column A's text being the value you're matching for).
Now you could sort according to Column H, etc, to suit your needs.
Hope this does the trick (and makes sense)!
Cool Q, BTW!!!
Do you have, or can you create, a master list of all of the possible cell values? If so, then some simple VLOOKUPs on each of the 4 data columns could give, for each unique cell value, the row number in each column. Add up the 4 reesults and sort on the total.
If you don't have the master list of unique values, I'd tend to go to Access because it's a pretty easy set of queries to get what you want.
Clarification Needed
When I first came up with this answer I used the same approach that John used in his clever Excel answer, namely to use the sum of the minimum rows per column to produce the rank. That produces the sample result in the question, but consider the following modified test data:
F1 F2 F3 F4 RowNum
--- --- --- --- ------
XXX bar baz bat 1
foo XXX baz bat 2
YYY bar XXX bat 3
foo YYY baz bat 4
foo bar YYY bat 5
foo bar baz YYY 6
foo bar baz bat 7
foo bar baz bat 8
foo bar baz bat 9
foo bar baz XXX 10
XXX appears in rows 1, 2, 3, and 10, so the sum would be 16. YYY appears in rows 3, 4, 5, and 6 so the sum would be 18. Ranking by sum would declare XXX the winner, even though if you started scanning for XXX from row 1 you would have to go all the way to row 10 to reach the last XXX, whereas if you started scanning for YYY from row 1 you would only have to go down to row 6 to reach the last YYY.
In this case should YYY actually be the winner?
(original answer)
The following code will import the Excel data into Access and add a [RowNum] column
Sub ImportExcelData()
On Error Resume Next '' in case it doesn't already exist
DoCmd.DeleteObject acTable, "ExcelData"
On Error GoTo 0
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "ExcelData", "C:\Users\Gord\Documents\ExcelData.xlsx", False
CurrentDb.Execute "ALTER TABLE ExcelData ADD COLUMN RowNum AUTOINCREMENT(1,1)", dbFailOnError
End Sub
So now we have an [ExcelData] table in Access like this
F1 F2 F3 F4 RowNum
--- --- --- --- ------
abc bcd abc def 1
cde fgh def bcd 2
def def bcd abc 3
bcd hji xyz lmn 4
Let's create a saved query named ExcelItems in Access to string the entries out in a long "list"...
SELECT F1 AS Item, RowNum, 1 AS ColNum FROM ExcelData
UNION ALL
SELECT F2 AS Item, RowNum, 2 AS ColNum FROM ExcelData
UNION ALL
SELECT F3 AS Item, RowNum, 3 AS ColNum FROM ExcelData
UNION ALL
SELECT F4 AS Item, RowNum, 4 AS ColNum FROM ExcelData
...returning...
Item RowNum ColNum
---- ------ ------
abc 1 1
cde 2 1
def 3 1
bcd 4 1
bcd 1 2
fgh 2 2
def 3 2
hji 4 2
abc 1 3
def 2 3
bcd 3 3
xyz 4 3
def 1 4
bcd 2 4
abc 3 4
lmn 4 4
Now we can find the lowest RowNum where Item is found for each ColNum...
TRANSFORM Min(ExcelItems.[RowNum]) AS MinOfRowNum
SELECT ExcelItems.[Item]
FROM ExcelItems
GROUP BY ExcelItems.[Item]
PIVOT ExcelItems.[ColNum] In (1,2,3,4);
...returning...
Item 1 2 3 4
---- - - - -
abc 1 1 3
bcd 4 1 3 2
cde 2
def 3 3 2 1
fgh 2
hji 4
lmn 4
xyz 4
If we save that query as ExcelItems_Crosstab then we can use it to rank the items that appear in all four columns:
SELECT Item, [1]+[2]+[3]+[4] AS Rank
FROM ExcelItems_Crosstab
WHERE ([1]+[2]+[3]+[4]) IS NOT NULL
ORDER BY 2
...returning...
Item Rank
---- ----
def 9
bcd 10