Macro help

VBABEGINER

Well-known Member
Joined
Jun 15, 2011
Messages
1,284
Office Version
  1. 365
Platform
  1. Windows
Hi All, Long back again.. :-)

need help in terms of vba code.

I've created one column with the help of Rank formula. Now I want any 2 records of every rank number on another sheet. Randomly. any code can I get please..
 
You may try such ammended code:
VBA Code:
Sub test2()
Dim lr As Long
ActiveSheet.Copy After:=Sheets(1)
ActiveSheet.AutoFilterMode = False
If ActiveSheet.ListObjects.Count > 0 Then ActiveSheet.ListObjects(1).Unlist
lr = Cells(Rows.Count, "BN").End(xlUp).Row
Columns("BQ:BQ").Insert Shift:=xlToRight
Range("BM2:BP" & lr).Sort Key1:=Range("BN2"), Key2:=Range("BP2"), Order2:=xlAscending, Header:=xlNo, Orientation:=xlSortColumns
Range("BQ2:BQ" & lr).FormulaR1C1 = "=COUNTIFS(R1C[-3]:RC[-3],RC[-3],R1C[-1]:RC[-1],""consider"")"
Range("BQ2:BQ" & lr).Value = Range("BQ2:BQ" & lr).Value
Range("BP1:BQ" & lr).AutoFilter Field:=2, Criteria1:="0", Operator:=xlOr, Criteria2:=">2"
Rows("2:" & lr).Delete Shift:=xlUp
ActiveSheet.AutoFilterMode = False
Range("BP1:BQ" & lr).AutoFilter Field:=1, Criteria1:="<>consider"
Rows("2:" & lr).Delete Shift:=xlUp
ActiveSheet.AutoFilterMode = False
Columns("BQ:BQ").Delete Shift:=xlToLeft
End Sub
Hi Kaper, I checked and share some error happening. Could you pls suggest on this.. :-)
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Of course I have no foggiest idea what ranges were in second CONCAT in column BO. For sure not to the same row. You may convert formulas to values before sorting, as they wil be probably no longer need to be recalculated.
VBA Code:
Range("BM2:BP" & lr).value = Range("BM2:BP" & lr).value
would do the job.
so the code would look like:
VBA Code:
Sub test3()
Dim lr As Long
ActiveSheet.Copy After:=Sheets(1)
ActiveSheet.AutoFilterMode = False
If ActiveSheet.ListObjects.Count > 0 Then ActiveSheet.ListObjects(1).Unlist
lr = Cells(Rows.Count, "BN").End(xlUp).Row
Columns("BQ:BQ").Insert Shift:=xlToRight
Range("BM2:BP" & lr).value = Range("BM2:BP" & lr).value
Range("BM2:BP" & lr).Sort Key1:=Range("BN2"), Key2:=Range("BP2"), Order2:=xlAscending, Header:=xlNo, Orientation:=xlSortColumns
Range("BQ2:BQ" & lr).FormulaR1C1 = "=COUNTIFS(R1C[-3]:RC[-3],RC[-3],R1C[-1]:RC[-1],""consider"")"
Range("BQ2:BQ" & lr).Value = Range("BQ2:BQ" & lr).Value
Range("BP1:BQ" & lr).AutoFilter Field:=2, Criteria1:="0", Operator:=xlOr, Criteria2:=">2"
Rows("2:" & lr).Delete Shift:=xlUp
ActiveSheet.AutoFilterMode = False
Range("BP1:BQ" & lr).AutoFilter Field:=1, Criteria1:="<>consider"
Rows("2:" & lr).Delete Shift:=xlUp
ActiveSheet.AutoFilterMode = False
Columns("BQ:BQ").Delete Shift:=xlToLeft
End Sub
 
Upvote 0
Of course I have no foggiest idea what ranges were in second CONCAT in column BO. For sure not to the same row. You may convert formulas to values before sorting, as they wil be probably no longer need to be recalculated.
VBA Code:
Range("BM2:BP" & lr).value = Range("BM2:BP" & lr).value
would do the job.
so the code would look like:
VBA Code:
Sub test3()
Dim lr As Long
ActiveSheet.Copy After:=Sheets(1)
ActiveSheet.AutoFilterMode = False
If ActiveSheet.ListObjects.Count > 0 Then ActiveSheet.ListObjects(1).Unlist
lr = Cells(Rows.Count, "BN").End(xlUp).Row
Columns("BQ:BQ").Insert Shift:=xlToRight
Range("BM2:BP" & lr).value = Range("BM2:BP" & lr).value
Range("BM2:BP" & lr).Sort Key1:=Range("BN2"), Key2:=Range("BP2"), Order2:=xlAscending, Header:=xlNo, Orientation:=xlSortColumns
Range("BQ2:BQ" & lr).FormulaR1C1 = "=COUNTIFS(R1C[-3]:RC[-3],RC[-3],R1C[-1]:RC[-1],""consider"")"
Range("BQ2:BQ" & lr).Value = Range("BQ2:BQ" & lr).Value
Range("BP1:BQ" & lr).AutoFilter Field:=2, Criteria1:="0", Operator:=xlOr, Criteria2:=">2"
Rows("2:" & lr).Delete Shift:=xlUp
ActiveSheet.AutoFilterMode = False
Range("BP1:BQ" & lr).AutoFilter Field:=1, Criteria1:="<>consider"
Rows("2:" & lr).Delete Shift:=xlUp
ActiveSheet.AutoFilterMode = False
Columns("BQ:BQ").Delete Shift:=xlToLeft
End Sub
Hi Kaper, Thank you so much for your reply. I didnt get a chance to cross check the same. I will ensure that either tomorrow EOD or day after tomorrow I will confirm you back.. Till then Thanks again
 
Upvote 0
So, as there was no next post in this thread, probably the solution worked for you. Would be nice if you mark the post with answer (post #12) as a solution.
 
Upvote 0
So, as there was no next post in this thread, probably the solution worked for you. Would be nice if you mark the post with answer (post #12) as a solution.
Hi Kaper, Thanks for your msg. Yesterday only I checked the 2nd solution you provided and i find some difficulty. Hence I did some correction in last column formula make sure that our code will work in much better. will inform you by today.
 
Upvote 0
No problem, I based on "Tomorrow by EOD" declaration in post #13 :cool:
 
Upvote 0
No problem, I based on "Tomorrow by EOD" declaration in post #13 :cool:
Hi Kaper, can you please do one favor. I've attached few ss. Pls review and can you pls provide me code based on this. what im checking is, if rank is less 2 then it should pick up all those 2 records. If rank count is more than 2 then with the combination of col BN or BO and Col BR, it only gives me 2 records for every record contain in BN or BO.
 

Attachments

  • ss-col BQ formula.jpg
    ss-col BQ formula.jpg
    231.1 KB · Views: 5
  • ss-col BP formula.jpg
    ss-col BP formula.jpg
    207.1 KB · Views: 5
  • ss-col BR formula.jpg
    ss-col BR formula.jpg
    204.6 KB · Views: 5
Upvote 0
This will output the results in col.BQ:BR.
Code:
Sub test()
    Dim a, b, e, i&, n&, x, y, temp, dic As Object
    Set dic = CreateObject("Scripting.Dictionary")
    a = Range("bm2", Range("bm" & Rows.Count).End(xlUp)(1, 2)).Value
    For i = 1 To UBound(a, 1)
        dic(a(i, 2)) = Trim$(dic(a(i, 2)) & " " & i)
    Next
    ReDim b(1 To dic.Count * 2, 1 To 2)
    For Each e In dic
        x = Split(dic(e))
        If UBound(x) > 1 Then
            For i = 0 To 1
                y = WorksheetFunction.RandBetween(i, UBound(x))
                temp = x(i): x(i) = x(y): x(y) = temp
            Next
        End If
        For i = 0 To 1
            If UBound(x) >= i Then
                n = n + 1
                b(n, 1) = a(x(i), 1): b(n, 2) = a(x(i), 2)
            End If
        Next
    Next
    [bq2].Resize(n, 2) = b
End Sub
 
Upvote 0
Thanks Fuji. I appreciate your time and providing solution to me. Kindly wait, let me check on this.
I also highly appreciate Kaper who as well provides / reply to my post. Thank you Guys, God bless you!
 
Upvote 0
This will output the results in col.BQ:BR.
Code:
Sub test()
    Dim a, b, e, i&, n&, x, y, temp, dic As Object
    Set dic = CreateObject("Scripting.Dictionary")
    a = Range("bm2", Range("bm" & Rows.Count).End(xlUp)(1, 2)).Value
    For i = 1 To UBound(a, 1)
        dic(a(i, 2)) = Trim$(dic(a(i, 2)) & " " & i)
    Next
    ReDim b(1 To dic.Count * 2, 1 To 2)
    For Each e In dic
        x = Split(dic(e))
        If UBound(x) > 1 Then
            For i = 0 To 1
                y = WorksheetFunction.RandBetween(i, UBound(x))
                temp = x(i): x(i) = x(y): x(y) = temp
            Next
        End If
        For i = 0 To 1
            If UBound(x) >= i Then
                n = n + 1
                b(n, 1) = a(x(i), 1): b(n, 2) = a(x(i), 2)
            End If
        Next
    Next
    [bq2].Resize(n, 2) = b
End Sub
Hi Fuji, I tested it. Pls refer my ss attached. In col BR it shows which records should be taken (should be consider) but if you see col BN then rows records are the same. Ideally, it shows row records against the number picked up in Col BR. The code only sorts of col BR. Hope did you get what Im trying to explain. :)

Output ideally comes on different sheet with that numbers you displayed in COl BR. It is perfectly selected but row records of that numbers should also get select
 

Attachments

  • solution 1 - fuji.jpg
    solution 1 - fuji.jpg
    185 KB · Views: 6
Last edited:
Upvote 0

Forum statistics

Threads
1,223,948
Messages
6,175,575
Members
452,652
Latest member
eduedu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top