Is there a way to distribute data according to a logic in Excel vba? - excel

I have an Excel sheet with the below data.
There are 10,000 Data rows.
9000 are of "USA" & 1000 are of "Other" country.
I want to evenly distribute the data so that when I have 9 "USA" followed by 1 "Other" data distributed throughout.
Name
Country
Alice
USA
Brook
Other
Cathy
USA
David
USA
Esther
Other
Freddy
USA
Galin
USA
Henry
Other
Indigo
USA
Jenny
USA
Kalin
Other
Linda
USA
How do I accomplish this using manual & excel VBA? Appreciate both solutions. Thanks

This can be achieved with a formula if you have the newest version of Excel.
Try something like (adapt ranges and what you are filtering on as necessary):
=LET(x, FILTER($B$1:$C$12, $C$1:$C$12="a"),
y, FILTER($B$1:$C$12, $C$1:$C$12="b"),
z, ROW(D1:D12), myrows, MAX(z),
ratio, MAX((COUNTA(x)/2)/(COUNTA(y)/2), (COUNTA(y)/2)/(COUNTA(x)/2))+1,
IF(MOD(z,ratio)<>0,
INDEX(x, IF(MOD(SEQUENCE(myrows),ratio)=0, 0, SEQUENCE(myrows)-CEILING(ROW(G1:G12)/ratio-1,1)), SEQUENCE(1,2)),
INDEX(y, IF(MOD(SEQUENCE(myrows),ratio)<>0,0,SEQUENCE(myrows)/ratio), SEQUENCE(1,2))))
For example:
The trick is to create the "correct" sequence for each result; for the first array you want to skip every nth row (in your case 10), and having the nth+1 row not default to n+1, but n, while in the second array you want to skip every row that isn't a some multiple of n, and have the nth rows count sequentially.
A caveat-- as is, I don't believe the formula will work with repetition other than 1, i.e. if you want to do something like 8 rows followed by 2 rows, this won't work.

This works even with older Excel versions:
If this is your data:
Add a Sort column with the following formula in C2 and pull it down:
=IF(B2="USA",COUNTIF($B$2:B2,"USA")+INT((COUNTIF($B$2:B2,"USA")-1)/ROUNDUP(COUNTIF(B:B,"USA")/(COUNTA(B:B)-COUNTIF(B:B,"USA")),0)),COUNTIF($B$2:B2,"Other")*(ROUNDUP(COUNTIF(B:B,"USA")/(COUNTA(B:B)-COUNTIF(B:B,"USA")),0)+1))
Then sort by this column C and USA and Other are evenly spread:

Related

How to condense column data in excel at a change in the row data

I'm not sure exactly how to explain this, but I want to combine rows in Excel while condensing the column data. I'm looking to take a spreadsheet like this:
Name
Coffee
Bananas
Chocolate Ice Cream
Tom
4
Tom
2
Tom
2
Jerry
1
Jerry
4
Amanda
3
Amanda
2
and turn it into this:
Name
Coffee
Bananas
Chocolate Ice Cream
Tom
4
2
2
Jerry
1
4
Amanda
3
2
Any tips would be super helpful.
You can setup a pivot table for this.
Select your table > Insert Pivot Table, then follow this screenshot:
Drag Name colum in Rows
Drag Coffee, Bananas, Chocolate ice creams in Values and you're set!
If you want the data transformed and cleansed, you can always use PowerQuery.
Go to Data -> Get Data -> From File -> whatever source your data is in.
In there, use the Group By function to suppress the rows.
From there, you'll have a new set of data which can then be loaded into the destination worksheet.
If the usage of PowerQuery is new to you, a quick Google search will help you through this.
Naturally, this assumes you have a version of Excel that has PQ built in.
Here/screenshot(s) refer:
Condensed table comprises 3 sections (colour coded respectively):
Row labels (names, blue) - assuming these are unique at source
Column labels (food product, green) - ditto
Values (sum per row / col label, yellow)
note: included an extra '1' for Tom + Chocolate IC in 1st row for testing...
1] Row labels
=UNIQUE(D7:D13)
(array function - requires Office 365 compatible version of Excel)
2] Col labels
=E6:G6
(array function - enter in single cell if using Office 365 compatible version of Excel, otherwise highlight relevant area and enter function with shift + alt + enter)
3] Values
=SUMIFS(INDEX($E$7:$G$13,0,MATCH(J$6,$E$6:$G$6,0)),$D$7:$D$13,$I7)
(can drag this function across values / yellow section)

EXCEL: return 1 value from table based on 2 criteria being met

I have a table of shipping prices per number of units and by country. I'd like to have the total shipping cost appear on my order form once the number of bottles and country have been filled in elsewhere on the order form. Any ideas for what formula would do this?
Here's a small part of my table:
1 2 3 4 5 6
France €12.60 €14.40 €17.40 €19.44 €21.36 €23.40
Belgium €20.40 €26.40 €31.20 €36.60 €42.00 €46.80
Germany €20.40 €26.40 €31.20 €36.60 €42.00 €46.80
So for the client shipping to Germany that selects 5 units, how do I get €42.00 to appear in the shipping cost cell?
Thank you
INDEX with two MATCH's.
=INDEX(A1:G4,MATCH("Germany",$A$1:$A$4,0),MATCH(5,$A$1:$G$1,0))
Here is another approach...
Suppose your data is in range A1:G4 including headers,
Put your look up criteria in range A6:B7, and use the following formula to return the desired cost:
=DGET(A1:G4,""&B7&"",A6:A7)

Search a Column for Specified Text, Then Return the Sum of the adjacent column which contains values to sum

I am creating a World Cup spreadsheet.
Example:
Team | Pts |
Brazil | XXX |
Switz. | XXX |
C. Rica| XXX |
Serbia | XXX |
I have another table which displays the Country, Score & whether it was a W (win), L (loss) or T (tie). A WIN will add 3 points, a TIE will add 1 point & a LOSS will add 0 points. Where I put the XXX, I want a formula that will search the table column for the Country Name and ADD up the W's, L's & T's and display the sum of every game there team played with the point system I mentioned.
Any help would be greatly appreciated! Thanks.
Try,
=SUM(COUNTIFS(D:D, A2, E:E, {"T","W","W","W"}))
You can use COUNTIFS. I set up a mock table to demonstrate:
The first COUNTIFS will count a given teams wins, and then multiple by a factor of 3.
The second COUNTIFS will count a given teams ties (no need to multiply here).
Since a Loss equates to 0 points, there is no need to sum or count anything.
Adjust ranges to fit your setup and then auto-fill the equation down your table to calculate for each team.
You can use a formula in H2, as below, and drag down as many rows as required
=(3*SUMPRODUCT(--($A$2:$A$13=$G2),--($C$2:$C$13="W"))+SUMPRODUCT(--($A$2:$A$13=$G2),--($C$2:$C$13="T")))
Data:
You only care about Wins or Ties. So count the wins per country and * 3 and add to that the count of ties, which you don't need to multiply as is 1.
Sumproduct is used to handle the arrays nicely.

Excel Vlookup Multiple Values

I am looking for a vlookup formula that returns multiple matches using two lookup values. I am currently trying to use the concatenate method, but I haven't quite figured it out. The table needs to return all of the multiple matches not just one. Currently, its only returning the last match.
For example, lets say I have a list of multiple city and states. The cities differ but the states remain the same obviously. I want to return the number of people in the each city.
City State #OfPeople
Albany NY 10
Orlando FL 5
Tampa FL 3
Seattle WA 1
Queens NY 8
So I concatenated the city and state column.
Join City State #OfPeople
Albany-NY Albany NY 10
Orlando-FL Orlando FL 5
Tampa-FL Tampa FL 3
Seattle-WA Seattle WA 1
Queens-NY Queens NY 8
The purpose of this is to create an updated log of people in each city has time progresses. I want to have a grand total amount of people in each column. (I know this requires another formula. I'm just focused on returning multiple matches for now). However, I don't want to overwrite the existing data. Hopefully, I explained this well. This is just an example of a larger project I'm working on. I need to be able to build on this list. That's why its important that I be able to return matches multiple times.
Join City State #OfPeople Total
Albany-NY Albany NY 10 10
Orlando-FL Orlando FL 5 15
Tampa-FL Tampa FL 3 18
Seattle-WA Seattle WA 1 19
Queens-NY Queens NY 8 27
Any help would be greatly appreciated!
Considering you're trying to get some grand totals based on multiple criteria, I would suggest using SUMIFS() / COUNTIFS() functions, rather than focusing on searching matching row itself.
However, if you need multiple criteria look up, for some reason, I believe INDEX() + MATCH() combination can perfectly do the job.
The table needs to return all of the multiple matches not just one.
Currently, its only returning the last match
You'll need to use SUMIFS() if there are multiple records for the same city/state combo in your people lookup.
=SUMIFS (sum_range, range1, criteria1, [range2], [criteria2], ...)
Let's assume that you have a cities tab and a people tab. Let's assume you have ten cities that you want to return the total amount of people from.
Cities Tab definition
City range: 'Cities'!A$1:A$10
State range: 'Cities'!B$1:B$10
People Tab definition
City range: 'People'!A$1:A$100
State range: 'People'!B$1:B$100
#OfPeople range: 'People'!C$1:C$100
Drop this formula in the first row of your cities tab, drag down the entire range of cities.
=SUMIFS('People'!C$1:C$100, 'Cities'!A$1, People'!A$1:A$100, 'Cities'!B$1, 'People'!B$1:B$100)

Excel sort data in newspaper style columns

I have an excel sheet that has names and extension numbers. The sheet is designed to be printed as a reference and so it has the data split into 3 columns like how a newspaper is laid out.
EXT Name EXT Name EXT Name
1 bob 4 pete 8 sam
2 dave 5 sally 9 john
I need to have excel sort this data on name, A-Z. I can only work out how to make it sort one column at a time and so I end up having to manaually sort the data every time I add or remove information.
Can excel sort all 3 columns top to bottom and left to right?
Thanks!
Maybe this KB artikel helps.
Or u can try using the small function like
Column A
=SMALL($A$1:$A$9;0+ROWS(A$1:A1))
Column C
=SMALL($A$1:$A$9;30+ROWS(A$1:A1))
In this case you clone the sorted dataset, which is in two columns
The value for the B colums can be found using a vlookup. the value 0 and zero are an offset, so the length of the page is fixed

Resources