Modification and union of both codes

Aretradeser

Board Regular
Joined
Jan 16, 2013
Messages
176
Office Version
  1. 2013
Platform
  1. Windows
Regarding these two macros, I need:
1. That in the extraction of the 5 records, do not include the title of the column.
2. Join both codes.
Thank you very much
CÓDIGO 1
Rich (BB code):
Sub RegistrosAleatorios1()
  Dim arr As Variant
  Dim i As Long, x As Long, y As Long, n As Long
  '
  n = Sheets("Hoja3").Range("C" & Rows.Count).End(3).Row
  Randomize
  arr = Evaluate("=row(1:" & n & ")")
  '
  For i = 1 To n
    x = Int(UBound(arr) * Rnd + 1)
    y = arr(x, 1)
    arr(x, 1) = arr(i, 1)
    arr(i, 1) = y
  Next
  '
  For i = 1 To 5
    Sheets("Hoja1").Range("C11").Offset(0, i - 1).Value = Sheets("Hoja3").Range("C" & arr(i, 1))
  Next
End Sub
CÓDIGO 2
Rich (BB code):
Sub RegistrosAleatorios2()
  Dim arr As Variant
  Dim i As Long, x As Long, y As Long, n As Long
  '
  n = Sheets("Hoja5").Range("C" & Rows.Count).End(3).Row
  Randomize
  arr = Evaluate("=row(1:" & n & ")")
  '
  For i = 1 To n
    x = Int(UBound(arr) * Rnd + 1)
    y = arr(x, 1)
    arr(x, 1) = arr(i, 1)
    arr(i, 1) = y
  Next
  '
  For i = 1 To 5
    Sheets("Hoja1").Range("C16").Offset(0, i - 1).Value = Sheets("Hoja5").Range("C" & arr(i, 1))
  Next
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
See how this works

VBA Code:
Sub RegistrosAleatorios1()
Dim sh1 As Worksheet, sh2 As Worksheet, arr As Variant, ary As Variant, sh As Long
Dim n As Long, i As Long, j As Long, x, y
Set sh1 = Sheets("Hoja3")
Set sh2 = Sheets("Hoja5")
ary = Array(sh1, sh2)
    For sh = LBound(ary) To UBound(ary)
        With ary(sh)
            n = .Range("C" & Rows.Count).End(3).Row
            Randomize
            arr = Evaluate("=row(1:" & n & ")")  '
            For i = 1 To n
                x = Int(UBound(arr) * Rnd + 1)
                y = arr(x, 1)
                arr(x, 1) = arr(i, 1)
                arr(i, 1) = y
            Next  '
            For j = 1 To 5
                If sh = LBound(ary) Then
                    Sheets("Hoja1").Range("C11").Offset(0, j - 1).Value = .Range("C" & arr(i, 1))
                Else
                    Sheets("Hoja1").Range("C16").Offset(0, i - 1).Value = .Range("C" & arr(i, 1))
                End If
            Next
        End With
    Next
End Sub
 
Upvote 0
It gives an error in this line of code:
Rich (BB code):
Sheets("Hoja1").Range("C11").Offset(0, j - 1).Value = .Range("C" & arr(i, 1))
 
Upvote 0
what is the error message?

Just noticed this line needs to be changed to

VBA Code:
Sheets("Hoja1").Range("C16").Offset(0, j - 1).Value = .Range("C" & arr(i, 1))

Maybe both of those statements need to have the
VBA Code:
arr(i, 1)
VBA Code:
changed to
arr(j, 1)
 
Last edited:
Upvote 0
It should now read
VBA Code:
For j = 1 To 5
        If sh = LBound(ary) Then
            Sheets("Hoja1").Range("C11").Offset(0, j - 1).Value = .Range("C" & arr(j, 1))
        Else
            Sheets("Hoja1").Range("C16").Offset(0, j - 1).Value = .Range("C" & arr(j, 1))
       End If
Next
 
Upvote 0
This new code works correctly.
The problem is that in the random extraction of the records in column "C", they include the title of the column; and it should not be like that.
On the other hand, due to an error in my question, the first code I put must extract 6 records, instead of 5.
It is the second code that must extract 5 records.
What modifications would be necessary?
Thank you very much, JLGWhiz, for your brilliant work
 
Upvote 0
i think this will do it
VBA Code:
Sub RegistrosAleatorios3-1rev3()
Dim sh1 As Worksheet, sh2 As Worksheet, arr As Variant, ary As Variant, sh As Long
Dim n As Long, i As Long, j As Long, x, y
Set sh1 = Sheets("Hoja3")
Set sh2 = Sheets("Hoja5")
ary = Array(sh1, sh2)
    For sh = LBound(ary) To UBound(ary)
        With ary(sh)
            n = .Range("C" & Rows.Count).End(3).Row
            Randomize
            arr = Evaluate("=row(1:" & n & ")")  '
            For i = 2 To n
                x = Int(UBound(arr) * Rnd + 1)
                y = arr(x, 1)
                arr(x, 1) = arr(i, 1)
                arr(i, 1) = y
            Next
            If sh = LBound(ary) Then
                For j = 1 To 6
                    Sheets("Hoja1").Range("C11").Offset(0, j - 1).Value = .Range("C" & arr(j, 1))
                Next
            Else
                For j = 1 To 5
                    Sheets("Hoja1").Range("C16").Offset(0, j - 1).Value = .Range("C" & arr(j, 1))
                Next
            End If
        End With
    Next
End Sub
 
Upvote 0
Now it extracts 6 and 5 records respectively.
This part is solved.
But your code when extracting these records, includes the title of the column, and I don't need to include it.
For the extraction you must select the records from row2, not from row1 which is where the column titles are.
I don't know if I have explained myself well.
Again, I thank you very much.
 
Upvote 0
Now it extracts 6 and 5 records respectively.
This part is solved.
But your code when extracting these records, includes the title of the column, and I don't need to include it.
For the extraction you must select the records from row2, not from row1 which is where the column titles are.
I don't know if I have explained myself well.
Again, I thank you very much.
 
Upvote 0
I don't want to have to rewrite the whole code. If I change any more it will invalidate your arrays.
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,825
Members
449,190
Latest member
rscraig11

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