Excel randomly select name from list with multiple entries - vba

I have an excel 2007 worksheet with employee names in column A and total number of entries in column B. I need to be able to randomly select x number of employee names from the total number of entries, allowing for the fact that some will have multiple entries.
For example:
Amy............30
Brian..........12
Charlene.......15
Michael.........1
Nathan..........7
What is the best way to do this?
My initial thoughts are:
1) find the max() of column B occurances of a random number in another column, like C. Then find the top values for all of that new column.
2) create a VBA array of all of the potiential entries and randomly pick one from there.
3) loop through all of the names in column A and create a temp worksheet with column B instances of each, then assign a random num generator and choose the top n.
Having said that, there may be something a lot easier. I am not sure where to begin. Normally I can find code that is similar to what I need, but I am not having any luck. Any help that you can offer would be appreciated.
Thank you in advance.

I would probably do something like this if I understand your question correctly(I just read your question title):

Related

How to use more than 200 nested if conditions in excel?

I have the following data in excel sheet A.
Category Name
Fruit Apple
Vegetable Brinjal
XYZ Abc
I want to create a formula which takes a value for name column, outputs the corresponding category column.
If I use VLookUp, I have to copy this reference table in each and every excel sheet wherever I need to have this operation.
Hence I am looking for something similar to
IF(input="Apple","Fruit",IF(input="Brinjal","Vegetable",IF(input="Abc",XYZ,"")))
But There is limit on nested ifs in excel and no of cases that we can have in a switch case are also limited.
I have around 200 rows of this table.
use INDEX and MATCH functions. INDEX on "category" by matching "name"
You certainly don't need so many IF statements (though I note your Q Title), for example:
=CHOOSE(MATCH(D13,{"Apple","Brinjal","Abc"},0),"Fruit","Vegetable","XYZ")
which should not grow at quite the rate your version would - but with 200 'pairs' would be getting close to the limit for CHOOSE.
(D13 as example in spreadsheet.)

VBA macro for excel. Mark row if cells in column A are the same but different in column C

I do not have any code yet as I dont know VBA that much or at all.
I got excell sheet with 4 columns. Column A is main group and C is subgroups within groups from column A.
i need to mark somehow rows where within same JobID, WFID is the same and where WFID is different within same JobID.
JobID TaskID WFID
39822 913914 Complete
39822 913915 no complete
37941 905439 Complete
37941 905440 Complete
Would you be able to help pleasE?
You do not really need macros to identify your rows.
Simply put this formula in a new column
=COUNTIFS($A$1:$A$8,A1,$C$1:$C$8,"<>"&C1)
Wherever there are different values in col C for same value of col A it will throw a 1 else 0
If you want to highlight, I recommend using conditional formatting based on the value of your new column with (0/1) values.
Hope this answers your query.

SQL counting number of rows

I am looking for a way to search for a certain number of rows as a quality check. For example, we have tables that have a certain set of results that are needed.
Here is a quick table for an example:
ID: Name: Result: Reportable:
ONE A 10 X
TWO B 12 X
THREE C 1
FOUR D 18 X
FOUR(redo) D 11 X
So we are looking to double check results as there are people who accidentally report results multiple times (as in the case with ID FOUR). We have used having counts but we need the numbers to be specific and need a query to verify that number is satisfied.
In the table above we only want IDs ONE, TWO, and FOUR, however we have 4 results (one extra). Currently we have our check showing the count needed (ie 3) and the current result count (4) to show the mismatch but want a query to easily only show the result needed. We would need the redo result most of the time so we have set it so we take the latest date, but it doesn't help filter how many rows or results. I apologize if anything is confusing and I am not able to share the SQL query that we have currently. It's my first time posting so if I need to clarify anything please let me know as this seems to be very complicated. Thank you for your time.
EDIT: The details
We have one table (Table A) letting us know which results are reportable. The ones that are reportable go into another table (Table B). We have had issues in which people have made too many results reportable which overpopulates the Table B. Our old query had a count in Table B, but due to mistakes in people placing multiple reportables, samples which had many redos seem to be finished as they were all placed and met the count in Table B.
So now by using the Table A that helps tell us how many are Reportable, we want this to double check that the samples are indeed ready.
As I understand the question, you want ids that have multiple reportables. Assuming you really mean name, then:
select name
from t
where reportable = 'X'
group by name
having count(*) >= 2;

Merging two tables while deleting duplicates in the first table and

This is my first post here and your help would be greatly appreciated! I've read a lot of other posts on this site, however I cannot find the answer to my specific question. I tried using VLOOKUP, INDEX, MATCH, Pivottables, etc. However it doesn't work out the way I want.
Background information: for my thesis I'm studying the difference in cost of capital among single segment and multi segment firms. My dataset contains two tables, one with the SIC codes of the firm's segments and one with the corresponding sales of that segment. The issue with these tables is that there are duplicate SIC codes in the first table. I want to remove the duplicates from the first table and simultaneously calculate the sum of the sales of these duplicate SIC codes in the second table.
My data looks as follows:
The SIC codes per segment and the sales per segment:
Input of SIC codes and sales per segment (one company for 20 years)
What I want to do is eliminate the duplicate SIC codes. If I change the table with SIC codes I also need the table with sales to change accordingly. However, the sales of duplicate segments should not be deleted but added to the first duplicate segment. I can computed this manually for one company, however for 1800 companies would this would be very time consuming. The manually computed output for the SIC codes and for Sales looks like this (so I don't need to merge the table, the output is still in two different tables):
Required output for the SIC codes table and Sales table (one company for 20 years
Thanks a lot!
This is the way to go about it. In 2 parts: get a list of unique SIC codes in each row, and, sum up the unique corresponding values to them.
Part I:
General logic to this part: nested INDEX functions.
Let's assume your output table (range including headers A25:J45) is exactly below your main/input table (range including headers A1:J21). Assuming only 20 rows of data, but you can drag the formulae to as many rows.
The first column should always be picking up values from the corresponding first column of input table. A26 =A2 and so on.
For B26, use this formula =INDEX($A2:$J2,MATCH(0,INDEX(COUNTIF($A26:A26,$A2:$J2),0,0),0))
You can drag this formula across to J45/end of output table. (You can use "Evaluate Formula" in excel to understand the workings of the logic)
This should populate your output table with unique SIC codes for each row.
Part II:
General logic to this part: SUM and Arrays
Let's assume your output table (range including headers L25:U45) is exactly below your main/input table (range including headers L1:U21). Assuming only 20 rows of data, but you can drag the formulae to as many rows.
In cell L26, which is the first row, left most element in the output table, you will need an array formula (Ctrl+Shift+Enter). If you don't know what array formula are read here.
Formula for L26 {=SUM(IFERROR($A2:$J2=A26,0)*IFERROR($L2:$U2,0))}.
You will need to enter only this in L26 =SUM(IFERROR($A2:$J2=A26,0)*IFERROR($L2:$U2,0)), and hit Ctrl+Shift+Enter. Excel will put the curly brackets around it on its own self.
Copy paste the formula in the rest of the output table.
Screenshot for reference based on your Excel file.
There is a remove duplicates function in excel 2010 on the Data Tab Screenshot

Excel: Selecting Specific Columns and Rows in Pivot Table and Formatting

I'm using a Pivot Table in Excel 2010, and while searching posts I find that a lot of users are frustrated like me because it doesn't keep all formats.
So what I'm trying to do is run a macro that formats columns in a Pivot table, but limited to the last row and column in the table. I have the formatting info, but I just need to know how to apply it to specific columns and rows.
What I was thinking might work is finding the last column of the Values row, in this case "Stops per Rte" which is the last Values column; however, I have months listed at the top, so it repeats across months. If the user filters only certain months then the # of columns will decrease.
Same goes for the # of rows: of course, the user should be able to expand/collapse rows as needed, so I only want the column format to go to the last row or just above "Grand Total", if possible.
Hopefully, this makes sense. Thanks in advance! = )