KosieNakata
New Member
- Joined
- Jun 28, 2022
- Messages
- 3
- Office Version
- 365
- Platform
- MacOS
Hi Excel experts!! I'm trying to write a macro where it needs to extract 30% of the data from a dataset, where the 30% of data is selected randomly by using an assigned random number using the function RAND() first. The below is a quick overview of the steps that the macro needs to get done:
3. Then taking step 2's Cell H1 value as the new "Last Row" value incorporate this last row value into a nested function of INDEX() & RANK() function which I am having trouble with..
I have tried the following code to create a nested function which works but really have no clue how I can autofill this line up to the "Last Row".
Hoping to get some ideas to see if it's possible to incorporate the autofill element which stops at the "last row" in step 2 !
- Assign a randomly generated number to each row of data in the master dataset, using RAND().
- On a separate worksheet, calculate the number of TRX that needs to be randomly selected, for example, total number of TRX is 1724 rows, 30% is 517 rows of data.
VBA Code:
'Step 2 - Calculate the number of TRX to be randomly selected (30% of Total Amount)
Dim Num1 As Integer
Dim Num2 As Single
Dim CountaRange As Range
Set CountaRange = Range("'Sales Data Master'!A2:A" & LastRow)
Sheets("Random Pick Process").Select
Range("G1").Value = WorksheetFunction.CountA(CountaRange)
Num1 = Range("G1")
Num2 = 0.3
Range("H1").Formula = Num1 * Num2
3. Then taking step 2's Cell H1 value as the new "Last Row" value incorporate this last row value into a nested function of INDEX() & RANK() function which I am having trouble with..
I have tried the following code to create a nested function which works but really have no clue how I can autofill this line up to the "Last Row".
VBA Code:
v = Application.Index(CountaRange, Application.Rank(Range("'Sales Data Master'!Q2"), RandomRange), 1)
With Application
v = .Index(CountaRange, .Rank(Range("'Sales Data Master'!Q2"), RandomRange), 1)
End With
Range("A3").Formula = v
Hoping to get some ideas to see if it's possible to incorporate the autofill element which stops at the "last row" in step 2 !