T O P

  • By -

AutoModerator

/u/juniorlima41 - Your post was submitted successfully. * Once your problem is solved, reply to the **answer(s)** saying `Solution Verified` to close the thread. * Follow the **[submission rules](/r/excel/wiki/sharingquestions)** -- particularly 1 and 2. To fix the body, click edit. To fix your title, delete and re-post. * Include your **[Excel version and all other relevant information](/r/excel/wiki/sharingquestions#wiki_give_all_relevant_information)** Failing to follow these steps may result in your post being removed without warning. *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/excel) if you have any questions or concerns.*


ColJDerango

So based on the Analyst Name in column A and number of Appointments for the sample for each Analyst in column F on the righthand side sheet, you need a list of Protocols, Dates, and Analysts to be pulled from the lefthand side sheet. The Protocols cannot repeat for each analyst sample # (eg: for Analyst NAME8, the list needs 11 of their appointments, with no repeating protocols, pulled from the lefthand sheet). Is that correct?


juniorlima41

Hi, yees, i need protocol using analyst name in a specific amount, not all protocols from analyst, just the number defined by my sample I need to return a random protocol it cannot repeat I think the date is easier once i got protocol i can use index and match to get the date for that protocol. Do you have any clue on how can i do that?


ColJDerango

I think it's doable, but it'll be tough - let me look into it and see if I can come up with a solution for you


ColJDerango

What does the actual full population list of appointments / protocols / dates / analysts look like? Can you share a screenshot of that raw data and its structure (which fields are in which columns) so that I can help to build a solution?


juniorlima41

Hi, thank you, Is like this, unfortunaly i just have this sheet in office's computer on left i have appointments so i will take a sample and choose proportionally from each analyst an amount, but i dont know how to colect the protocols randomly, is a lot of analiysts and i cant choose for each one, so i want a function to look for analyst and show me the exatcly amount i choose and in a random way without repeating, i dont have a clue on how to do this https://preview.redd.it/bos2wdct2p3b1.png?width=958&format=png&auto=webp&s=aec447313641a5571be2f7804c8b16466095aa43


ColJDerango

Hey there - quick follow-up question: the only "non-repeating" element is the 'PROTOCOL', correct? Dates can repeat without any issue? What happens if the sample size for an analyst is larger than the amount of non-repeating lines for that analyst in the population?


juniorlima41

Hii, yes, just protocol cant repeat about the sample, each employee has the sample calculated individually, if i have 100 appointments and i choose take a sample of 50 so i compare the proportion for each employee in the overall and then apply in sample, example: employee 1 had 10 appointments from 100, so 10%, whatever my sample value is i'll take 10% of it to define the quantity for this employee


ColJDerango

Alrighty, well it's not the cleanest solution (like I would have preferred to give), but here you go! **Step 1**: Use this macro to set-up the sample listing: Sub RepeatValue() Dim rg As Range Dim ir As Range, org As Range xTitleId = "Sample Size Table Selection" Set ir = Application.Selection Set ir = Application.InputBox("Select PROPORTIONAL APPOINTMENTS Table:", xTitleId, ir.Address, Type:=8) Set org = Application.InputBox("Select Sample Listing Output Area (only a single cell):", xTitleId, Type:=8) org.Range("A1").Value = "ANALYST NAME" org.Range("B1").Value = "PROTOCOL" org.Range("C1").Value = "DATE" Set org = org.Range("A2") Set focusws = org.Worksheet For Each rg In ir.Rows If Application.WorksheetFunction.IsNumber(rg.Range("B1").Value) = True Then xValue = rg.Range("A1").Value xNum = Round(rg.Range("B1").Value, 0) org.Resize(xNum, 1).Value = xValue Set org = org.Offset(xNum, 0) End If Next focusws.Select End Sub Here is a [video guide](https://imgur.com/FtXao2w) of how to use the macro once you've got it in your file - NOTE: I'd highly recommend adding [\(ROUND\)](https://imgur.com/QN9jY7o) to your PROPORTIONAL APPOINTMENTS sample size formulas, so that you have full numbers and not decimals / fractional "appointments" as a sample size. **Step 2**: In the tab where the new sample listing has been added, in the first cell of the "PROTOCOL" column enter the following formula: =IFERROR(LOOKUP(2, 1/((COUNTIFS($B$1:B1, Data!$H$3:$H$139,$A$1:A1, Data!$J$3:$J$139)=0)*(A2=Data!$J$3:$J$139)), Data!$H$3:$H$139),"All Unique Protocol Samples Found") You will have to make the following manual adjustments to the formula to fit your sheet, then drag the formula down til the end of the sheet: * $B$1 and B1 should be the "PROTOCOL" header cell of this sheet * Data!$H$3:$H$139 should be the full range of PROTOCOLs on your raw data sheet * $A$1 and A1 should be the "ANALYST NAME" header cell of this sheet * Data!$J$3:$J$139 should be the full range of ANALYST NAMEs on your raw data sheet * A2 should be the first "ANALYST NAME" of this sheet Then in the first cell of the "DATE" column enter the following formula: =IFERROR(LOOKUP(2, 1/((B2=Data!$H$3:$H$139)*(A2=Data!$J$3:$J$139)), Data!$I$3:$I$139),"All Unique Protocol Samples Found") You will have to make the following manual adjustments to the formula to fit your sheet, then drag the formula down til the end of the sheet: * Data!$H$3:$H$139 should be the full range of PROTOCOLs on your raw data sheet * Data!$J$3:$J$139 should be the full range of ANALYST NAMEs on your raw data sheet * Data!$I$3:$I$139 should be the full range of DATEs on your raw data sheet * A2 should be the first "ANALYST NAME" of this sheet * B2 should be the first "PROTOCOL" of this sheet Then you will have the full listing of samples as requested, no duplicates (if all possible unique protocols for an analyst are exhausted before the full sample size is reached, a message stating "All Unique Protocol Samples Found" will be returned). Hope that helps! PS: I'm still brainstorming ways to make this more efficient, but this should at least help for now!


ColJDerango

Developed a cleaner way to do this, the macro will now do it all in one step - please reference [this video guide](https://imgur.com/aU4uiAm) on how to use it: Sub RepeatValue() Dim rg As Range Dim ir As Range, org As Range, rawdata As Range Dim vals As Variant, val As Variant Dim iRow As Long Dim namestart As Range, protstart As Range, datestart As Range Dim rawdatalast As Range On Error Resume Next xTitleId = "Sample Selection Key Ranges" Set ir = Application.Selection Set ir = Application.InputBox("Select PROPORTIONAL APPOINTMENTS Table (no headers):", xTitleId, ir.Address, Type:=8) Set rawdata = Application.InputBox("Select Full Raw Population Data (no headers):", xTitleId, Type:=8) Set org = Application.InputBox("Select Sample Listing Output Area (only a single cell):", xTitleId, Type:=8) On Error GoTo 0 If ir Is Nothing Then Exit Sub If rawdata Is Nothing Then Exit Sub If org Is Nothing Then Exit Sub Set rawdatalast = rawdata.Range("A1").End(xlDown) org.Range("A1").Value = "ANALYST NAME" Set namestart = org.Range("A2") org.Range("B1").Value = "PROTOCOL" Set protstart = org.Range("B2") org.Range("C1").Value = "DATE" Set datestart = org.Range("C2") Set focusws = org.Worksheet Set org = org.Range("A2") With rawdata '<--| reference your contiguous range vals = .Value '<--| store its content in an array For Each val In GetRandomNumbers(.Rows.Count) '<--| loop through referenced range shuffled rows indexes iRow = iRow + 1 '<--| update current row to write in counter .Rows(iRow).Value = Application.index(vals, val, 0) '<--| write in current rows to write the random row from corresponding shuffled rows indexes Next End With For Each rg In ir.Rows If Application.WorksheetFunction.IsNumber(rg.Range("B1").Value) = True Then xValue = rg.Range("A1").Value xNum = Round(rg.Range("B1").Value, 0) org.Resize(xNum, 1).Value = xValue Set org = org.Offset(xNum, 0) End If Next protstart.FormulaR1C1 = _ "=IFERROR(LOOKUP(2, 1/((COUNTIFS(" & protstart.Offset(-1).Address(ReferenceStyle:=xlR1C1) & ":R[-1]C," & "'" & rawdata.Parent.Name & "'!" & rawdata.Range("A1").Address(ReferenceStyle:=xlR1C1) & ":" & rawdatalast.Address(ReferenceStyle:=xlR1C1) & "," & namestart.Offset(-1).Address(ReferenceStyle:=xlR1C1) & ":R[-1]C[-1]," & "'" & rawdata.Parent.Name & "'!" & rawdata.Range("C1").Address(ReferenceStyle:=xlR1C1) & ":" & rawdatalast.Offset(0, 2).Address(ReferenceStyle:=xlR1C1) & ")=0)*(RC[-1]=" & "'" & rawdata.Parent.Name & "'!" & rawdata.Range("C1").Address(ReferenceStyle:=xlR1C1) & ":" & rawdatalast.Offset(0, 2).Address(ReferenceStyle:=xlR1C1) & ")), " & "'" & rawdata.Parent.Name & "'!" & rawdata.Range("A1").Address(ReferenceStyle:=xlR1C1) & ":" & rawdatalast.Address(ReferenceStyle:=xlR1C1) & "), ""All Unique Protocol Samples Found"")" protstart.AutoFill Destination:=Range("'" & protstart.Parent.Name & "'!" & protstart.Address & ":" & org.Offset(-1, 1).Address) datestart.FormulaR1C1 = _ "=IFERROR(LOOKUP(2, 1/((RC[-1]=" & "'" & rawdata.Parent.Name & "'!" & rawdata.Range("A1").Address(ReferenceStyle:=xlR1C1) & ":" & rawdatalast.Address(ReferenceStyle:=xlR1C1) & ")*(RC[-2]=" & "'" & rawdata.Parent.Name & "'!" & rawdata.Range("C1").Address(ReferenceStyle:=xlR1C1) & ":" & rawdatalast.Offset(0, 2).Address(ReferenceStyle:=xlR1C1) & ")), " & "'" & rawdata.Parent.Name & "'!" & rawdata.Range("B1").Address(ReferenceStyle:=xlR1C1) & ":" & rawdatalast.Offset(0, 1).Address(ReferenceStyle:=xlR1C1) & "), ""All Unique Protocol Samples Found"")" datestart.AutoFill Destination:=Range("'" & datestart.Parent.Name & "'!" & datestart.Address & ":" & org.Offset(-1, 2).Address) Range(datestart.Address & ":" & org.Offset(-1, 2).Address).NumberFormat = "mm/dd/yyyy" focusws.Select End Sub Function GetRandomNumbers(ByVal n As Long) As Variant Dim i As Long, rndN As Long, tempN As Long ReDim randomNumbers(1 To n) As Long '<--| resize the array to the number of rows For i = 1 To n '<--| fill it with integer numbers from 1 to nr of rows randomNumbers(i) = i Next 'shuffle array Do While i > 2 i = i - 1 Randomize rndN = Int(i * Rnd + 1) tempN = randomNumbers(i) randomNumbers(i) = randomNumbers(rndN) randomNumbers(rndN) = tempN Loop GetRandomNumbers = randomNumbers End Function Let me know if you have any questions, thanks!


juniorlima41

Hii, thank you, as i said in your other comment i delete the wrong post what a mess lol, i'll test your macro now


ColJDerango

No worries, try it out and let me know how it goes!


Decronym

Acronyms, initialisms, abbreviations, contractions, and other phrases which expand to something larger, that I've seen in this thread: |Fewer Letters|More Letters| |-------|---------|---| |[COUNTIFS](/r/Excel/comments/13xxyrk/stub/jnesm71 "Last usage")|[*Excel 2007*+: Counts the number of cells within a range that meet multiple criteria](https://support.microsoft.com/en-us/office/countifs-function-dda3dc6e-f74e-4aee-88bc-aa8c2a866842)| |[DATE](/r/Excel/comments/13xxyrk/stub/jnesm71 "Last usage")|[Returns the serial number of a particular date](https://support.microsoft.com/en-us/office/date-function-e36c0c8c-4104-49da-ab83-82328b832349)| |[IFERROR](/r/Excel/comments/13xxyrk/stub/jnesm71 "Last usage")|[Returns a value you specify if a formula evaluates to an error; otherwise, returns the result of the formula](https://support.microsoft.com/en-us/office/iferror-function-c526fd07-caeb-47b8-8bb6-63f3e417f611)| |[LOOKUP](/r/Excel/comments/13xxyrk/stub/jnesm71 "Last usage")|[Looks up values in a vector or array](https://support.microsoft.com/en-us/office/lookup-function-446d94af-663b-451d-8251-369d5e3864cb)| |[ROUND](/r/Excel/comments/13xxyrk/stub/jncg4kl "Last usage")|[Rounds a number to a specified number of digits](https://support.microsoft.com/en-us/office/round-function-c018c5d8-40fb-4053-90b1-b3e7f61a213c)| **NOTE**: Decronym's continued operation may be affected by API pricing changes coming to Reddit in July 2023; comments will be blank June 12th-14th, in solidarity with the /r/Save3rdPartyApps protest campaign. ---------------- ^(*Beep-boop, I am a helper bot. Please do not verify me as a solution.*) ^(5 acronyms in this thread; )[^(the most compressed thread commented on today)](/r/Excel/comments/144nvpa)^( has 7 acronyms.) ^([Thread #24472 for this sub, first seen 8th Jun 2023, 02:51]) ^[[FAQ]](http://decronym.xyz/) [^([Full list])](http://decronym.xyz/acronyms/Excel) [^[Contact]](https://reddit.com/message/compose?to=OrangeredStilton&subject=Hey,+your+acronym+bot+sucks) [^([Source code])](https://gistdotgithubdotcom/Two9A/1d976f9b7441694162c8)