Extracting number from a range randomly

Goalexcel

Board Regular
Joined
Dec 28, 2020
Messages
101
Office Version
  1. 2016
Platform
  1. Windows
Hello Expert, please kindly advise what is the best method to extract numbers from a range randomly
I have a series of numbers in the range of A1 to T16. I would like to extract 3 differents numbers from each columns, and place them in column A21, B21so on. I wonder. if there is a way to do that easily.
 
I am not really convinced but given my question and your answer, try this. At the moment it only works on the active sheet and column A. If it is what you actually want then it can be extended to other columns and sheets if required.

VBA Code:
Sub List4()
  Dim PrevRuns As Long, fr As Long
  Dim Clrs As Variant, FirstRows As Variant
 
  Clrs = Array(vbYellow, vbBlue, vbGreen, vbCyan)
  FirstRows = Array(2, 14, 6, 10)
  PrevRuns = (Range("A" & Rows.Count).End(xlUp).CurrentRegion.Columns(1).SpecialCells(xlConstants).Count - 1) / 4
  If PrevRuns < 4 Then
    With Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(4)
      .Value = Range("A" & FirstRows(LBound(FirstRows) + PrevRuns)).Resize(4).Value
      .Interior.Color = Clrs(LBound(Clrs) + PrevRuns)
    End With
    Range("A" & FirstRows(LBound(FirstRows) + PrevRuns)).Resize(4).Interior.Color = Clrs(LBound(Clrs) + PrevRuns)
  End If
End Sub
Thank you for your replay. I run code but not show nothing.
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Thank you for your replay. I run code but not show nothing.
This is what it did for me

Goalexcel TestPicknumbers.xlsm
A
1Sect 1
21101
31102
41103
51104
61105
71106
81107
91108
101109
111110
121111
131112
141113
151114
161115
171116
18
19
20Sect 1
211101
221102
231103
241104
251113
261114
271115
281116
291105
301106
311107
321108
331109
341110
351111
361112
37
Group 1
 
Upvote 0
This is what it did for me

Goalexcel TestPicknumbers.xlsm
A
1Sect 1
21101
31102
41103
51104
61105
71106
81107
91108
101109
111110
121111
131112
141113
151114
161115
171116
18
19
20Sect 1
211101
221102
231103
241104
251113
261114
271115
281116
291105
301106
311107
321108
331109
341110
351111
361112
37
Group 1
Mr. @Peter_SSs , Please kindly take a look of my file.

 
Upvote 0
OK, try this instead

VBA Code:
Sub List4_v2()
  Dim PrevRuns As Long, fr As Long
  Dim Clrs As Variant, FirstRows As Variant
  
  Clrs = Array(vbYellow, vbBlue, vbGreen, vbCyan)
  FirstRows = Array(2, 14, 6, 10)
  With Range("A" & Rows.Count).End(xlUp)
    If IsNumeric(.Value) Then
      PrevRuns = (.CurrentRegion.Columns(1).SpecialCells(xlConstants).Count - 1) / 4
    Else
      PrevRuns = 0
    End If
  End With
  If PrevRuns < 4 Then
    With Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(4)
      .Value = Range("A" & FirstRows(LBound(FirstRows) + PrevRuns)).Resize(4).Value
      .Interior.Color = Clrs(LBound(Clrs) + PrevRuns)
    End With
    Range("A" & FirstRows(LBound(FirstRows) + PrevRuns)).Resize(4).Interior.Color = Clrs(LBound(Clrs) + PrevRuns)
  End If
End Sub
 
Upvote 0
OK, try this instead

VBA Code:
Sub List4_v2()
  Dim PrevRuns As Long, fr As Long
  Dim Clrs As Variant, FirstRows As Variant
 
  Clrs = Array(vbYellow, vbBlue, vbGreen, vbCyan)
  FirstRows = Array(2, 14, 6, 10)
  With Range("A" & Rows.Count).End(xlUp)
    If IsNumeric(.Value) Then
      PrevRuns = (.CurrentRegion.Columns(1).SpecialCells(xlConstants).Count - 1) / 4
    Else
      PrevRuns = 0
    End If
  End With
  If PrevRuns < 4 Then
    With Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(4)
      .Value = Range("A" & FirstRows(LBound(FirstRows) + PrevRuns)).Resize(4).Value
      .Interior.Color = Clrs(LBound(Clrs) + PrevRuns)
    End With
    Range("A" & FirstRows(LBound(FirstRows) + PrevRuns)).Resize(4).Interior.Color = Clrs(LBound(Clrs) + PrevRuns)
  End If
End Sub
Thank you @Peter_SSs it's work in column A.
 
Upvote 0
VBA Code:
Sub List4_v3()
  Dim PrevRuns As Long, fr As Long, ShNum As Long, ResultsHeaderRow As Long, LBC As Long, LBF As Long, c As Long
  Dim Clrs As Variant, FirstRows As Variant
  
  Clrs = Array(vbYellow, vbBlue, vbGreen, vbCyan)
  LBC = LBound(Clrs)
  FirstRows = Array(2, 14, 6, 10)
  LBF = LBound(FirstRows)
  For ShNum = 1 To Sheets.Count
    With Sheets(ShNum)
      ResultsHeaderRow = .Range("A1").End(xlDown).End(xlDown).Row
      PrevRuns = (.Range("A" & Rows.Count).End(xlUp).Row - ResultsHeaderRow) / 4
      If PrevRuns < 4 Then
        For c = 1 To .Cells(1, Columns.Count).End(xlToLeft).Column
          With .Cells(Rows.Count, c).End(xlUp).Offset(1).Resize(4)
            .Value = .Parent.Cells(FirstRows(LBF + PrevRuns), c).Resize(4).Value
            .Interior.Color = Clrs(LBC + PrevRuns)
          End With
          .Cells(FirstRows(LBF + PrevRuns), c).Resize(4).Interior.Color = Clrs(LBC + PrevRuns)
        Next c
      End If
    End With
  Next ShNum
End Sub
 
Upvote 0
Solution
VBA Code:
Sub List4_v3()
  Dim PrevRuns As Long, fr As Long, ShNum As Long, ResultsHeaderRow As Long, LBC As Long, LBF As Long, c As Long
  Dim Clrs As Variant, FirstRows As Variant
 
  Clrs = Array(vbYellow, vbBlue, vbGreen, vbCyan)
  LBC = LBound(Clrs)
  FirstRows = Array(2, 14, 6, 10)
  LBF = LBound(FirstRows)
  For ShNum = 1 To Sheets.Count
    With Sheets(ShNum)
      ResultsHeaderRow = .Range("A1").End(xlDown).End(xlDown).Row
      PrevRuns = (.Range("A" & Rows.Count).End(xlUp).Row - ResultsHeaderRow) / 4
      If PrevRuns < 4 Then
        For c = 1 To .Cells(1, Columns.Count).End(xlToLeft).Column
          With .Cells(Rows.Count, c).End(xlUp).Offset(1).Resize(4)
            .Value = .Parent.Cells(FirstRows(LBF + PrevRuns), c).Resize(4).Value
            .Interior.Color = Clrs(LBC + PrevRuns)
          End With
          .Cells(FirstRows(LBF + PrevRuns), c).Resize(4).Interior.Color = Clrs(LBC + PrevRuns)
        Next c
      End If
    End With
  Next ShNum
End Sub
Thank you so much @Peter_SSs for your time, working on it.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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