Pasting two similar tables with different and variable amount of data - vba

My problem is that I have two tables (with the exact same format, headings and everything but with different amount of data and it can change over time) in two different worksheets. How can I unify them in a new worksheet (paste one of them above the other). I would like a macro to do this so I can avoid manual pasting. Thanks a lot in advance.

Instead of a macro you might want to consider referencing. For this, the number of cells in each table must be defined. Let's say you have 100 rows in table 1 (sheet 1) and 100 rows in table 2 (sheet 2).
A101 (in sheet1) = Sheet2!A1 and drag that formula for the next 100 rows. Good luck.

Related

How would I use VBA to automatically populate an excel worksheet with data from the pivot table of a separate excel workbook?

I have two open Excel workbooks. One has some data in a pivot table, like so:
Row Labels Date
A 5
B 4
C 3
The other, separate workbook is blank other than Column A, which lists the same Row Labels as are in the pivot table, but possibly in a different order:
Row Labels
B
A
C
I would like to place a button on the second workbook that, when pushed, will automatically populate the row labels with the appropriate columns from the pivot table, in the correct order.
I know that VLOOKUP will be involved somehow, but I am not quite sure how to get there. Ideally, it would be nice if this VBA code could be dynamic, so that the button would work no matter the size of the pivot table, and no matter how many row labels will be in the second workbook.
Use the GETPIVOTDATA function - for example:
=GETPIVOTDATA("Date",[PivotWorkbook]Sheet1!$A$1,"Row Field",$A2)
The workbook containing the Pivot Table must be open to calculate the result.

How to compare a list of rows to another list of rows in Excel?

I am trying to figure out if there are any differences between a list of data with another. In order for a row of data to "match" with another row, the row must have the same values in their corresponding column. The rows themselves do not have to be in any particular order. In particular, I am dealing with a parts list, where there are part numbers, descriptions, etc. I am trying to figure out if any rows of data are different from rows of data from another list.
I found Compare two sheets using arrays, which may have the answer to my problem, but I am having trouble figuring out how to adapt to my code due to inexperience in Visual Basic.
I was able to get it to work for a single column of data, comparing one column of data from one sheet to another, but cannot get it to compare entire rows of data.
Here is an example of I want this to work:
Sheet 1 Sheet 2
Column 1 Column 2 Column 1 Column 2
Row 1 22a 33 11 11
Row 2 22a 33a 22a 33
Row 3 55 22b 55 23b
The code in the link will tell you what is not in sheet 1 but in sheet 2 and vice versa. In this example, I would like the code to tell me Sheet 1 Row 2 and Sheet 1 Row 3 are not in Sheet 2, and Sheet 2 Row 1 and Sheet 2 Row 3 are not in Sheet 1 (Sheet 1 Row 1 and Sheet 2 Row 2 match).
If that is ok by you, you can do it without VBA using the following formula:
={IF(IFERROR(MATCH(A1&"|"&B1;Sheet7!$A$1:$A$3&"|"&Sheet7!$B$1:$B$3;0);-1)=-1;"Unique";"")}
Assuming that each of your tables start in A1 (so that the tables with three entries span A1:B3), and entering this formula into C1 (and copying it down), press CTRL+SHIFT+ENTER when entering the formula to create an array formula, this will show the word "Unique" in column C if the pair in that row on that sheet is not in any of the row-pairs on sheet 2.
You can then use conditional formatting to highlight unique rows, filter on the tables to include only unique rows, or some other way of doing what you need.
NOTE 1: I have entered my numbers in Sheet6 and Sheet7 instead of 1 and 2. The formula written above goes into Sheet6.
NOTE 2: My language use ; instead of , as function separator, so if yours use , you need to change that.
NOTE 3: You will need to expand the ranges Sheet7!$A$1:$A$3 and Sheet7!$B$1:$B$3 if your set grows (this will happen automatically if new rows are inserted in between the old ones). The best is still probably to create named ranges for each of the 4 columns, exchange the references with those, and manage the named ranges instead of the formulas.
NOTE 4: If your data set contains the character "|", you need to change that as well, to match some character that you for sure do not have there.
Alternatively you could in column C on each cheet enter (assuming first entry in C1)
=A1&"|"&B1"
and copy this down, then run the solution from your copied example using that C column instead of on A1 and B1.

VBA to check for blank cells in columns based on value in another column

Given
O 1 2 3 A
A 4 5 6 B
B 7 8 9 D
O 3
C 15
T 18
I'm looking for VBA code to validate that when column A contains a value that the remaining columns also contain values and when it doesn't contain a value, that columns 2 & 5 also contain values but 3 & 4 don't.
I've simplified the example, in a real sheet there will be many more columns and rows to check.
I've considered COUNTIF and INDEX/MATCH and array forumlas but from my understanding these all work on single columns at a time.
I want to do something like WHEN A1:An<>"" THEN COUNTBLANK(B:E) ELSE COUNTA (C:D)
Is the best way to use autofilter using blanks in A and then countblank and then a second autofilter for values in A.
Thanks
You can do it with a couple of nested IF formulae as follows:
=IF(A1<>"",
"A not empty, "&IF(COUNTBLANK(B1:E1)=0,
"B:E not blank",
"B:E have blanks"),
"A blank, "&IF(AND(COUNTBLANK(B1)+COUNTBLANK(E1)=0,
COUNTBLANK(C1)+COUNTBLANK(D1)=2),
"Columns 2&5 have values and Columns 3&4 don't",
"but condition not met"))
The reason for going down the VBA route is that I want a generic reusable function as opposed to a formula I copy between cells and sheets changing the columns etc along the way ending up with a lot of duplicate code.
So something that takes a column to test and a value to test it with. Third parameter would be a range of columns to validate, and the fourth parameter the validation.
I don't want any solution to have the columns hard coded and I don't want intermediate totals at the end of rows. This is fairly easily achieved in Excel itself...
The reason for trying to use countblank is that I can apply it to a range.
After a lot of searching I discovered this (the columns don't match the original example)
=SUMPRODUCT((A2:A19<>"")*(B2:D19=""))
=SUMPRODUCT((A2:A19="")*(D2:D19=""))
=SUMPRODUCT((A2:A19="")*(B2:C19<>""))
Nice huh? I just need to convert it into VBA now.
Thanks

match column from one worksheet to another, and put a 1 under the matched column

I have 2 worksheet. In worksheet it listed in separate columns skills a person has
I now want to transfer it into another format.where I have listed all available skills, whenever there's a matched skills from worksheet1 to worksheet2, there should be a 1 output to corresponding worksheet2 columns.
Can anyone help me do that?
Assuming that both of your worksheets start from A1...
Sheet2 = D2 formula would be
=COUNTIFS(Sheet1!$A$1:$A$100,Sheet2!$A2,Sheet1!$E$1:$E$100,sheet2!D$1,Sheet1!$F$1:$F$100,sheet2!D$1,Sheet1!$G$1:$G$100,sheet2!D$1,Sheet1!$H$1:$H$100,sheet2!D$1,Sheet1!$I$1:$I$100,sheet2!D$1,Sheet1!$J$1:$J$100,sheet2!D$1)
Change ranges according to your requirement, Do take care of the freezing $ sign within the ranges.....once done...drag your formula across and below..
How does Countifs work:
=COUNTIFS(range1,value to find within range1,range2,value to find within range2, range3, value to find within range3.......so on)
Pivot table can solve the above issue.

Need to populate master list from many different sheets in excel

So lets say I have 3 different sheets each with information on them like this:
Sheet 1 Sheet2 Sheet 3
item1|2 item2|7 item1|4
item3|5 item3|6 item6|2
item9|7 item8|4 item7|8
The first part (code#) is a code for an item. The second number is the quantity of that item. What I need to do, is to be able to populate a master list which will pull the quantity of each item and sum them up. In the above case, the master list would look like this:
item1|6
item2|7
item3|11
item4|0
item5|0
item6|2
item7|8
item8|4
item9|7
I'm sorry if this is confusing but I really need some help!
Elaborating on #Skip Intro's solution:
Ensure all three sheets have the same labels (say Item and Qty), preferably in Row1.
Alt+D then P, select Multiple consolidation ranges and PivotTable, Next, Next.
Select your range from Sheet1 (say A:B), Add, repeat for Sheet2 and Sheet3, Finish.
Change Count of Value to Sum of Value, if required and might as well select PivotTable, PivotTable Options, Totals & Filters, untick (if necessary) Show grand totals for rows.
If you don't want (blank) to show either filter it out or restrict the data ranges (at the start) to less than whole columns.