VBA code to find rows that meet criteria and randomly output data from one column in corresponding row

ddub25

Well-known Member
Joined
Jan 11, 2007
Messages
625
Office Version
  1. 2019
Platform
  1. Windows
The code below works in randomly selecting any entry in Column "Exercise" and outputting it to Cell X8, but it is supposed to find rows where it finds a match for the text in cell V8, and then randomly choose one of these rows and then output the data in the corresponding cell of column "Exercise". Can anyone help?

VBA Code:
Private Sub CommandButton4_Click()

    Dim rngType As Range
    Dim rngExercise As Range
    Dim cell As Range
    Dim rowMatch As Long
    Dim rowCount As Long
    
    ' Set the named ranges for "Type" and "Exercise"
    Set rngType = ThisWorkbook.Names("Type").RefersToRange
    Set rngExercise = ThisWorkbook.Names("Exercise").RefersToRange
    
    ' Get the number of rows in the "Exercises" sheet
    rowCount = Worksheets("Exercises").UsedRange.Rows.count
    
    ' Find the rows where there is a match for V8 in named range "Type"
    For Each cell In rngType
        If cell.Value = Range("V8").Value Then
            ' Randomly choose a row that meets the specified criteria
            rowMatch = Int((rowCount - 1) * Rnd + 1) + 1 ' Add 1 to skip the header row
            ' Output the corresponding entry from named range "Exercise" in cell X8
            Range("X8").Value = Worksheets("Exercises").Range("A" & rowMatch).Offset(0, rngExercise.Column - 1).Value
            Exit Sub ' Exit the loop once a matching row is found
        End If
    Next cell
    
    ' If no matching row is found, output an error message
    MsgBox "No matching row found."

End Sub
 

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.

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

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