mardi 27 juin 2023

Macro to select random row from filtered sheet?

Morning all. I have next to zero knowledge of any kind of coding so please bear with me. I've got a massive spreadsheet with hundreds of thousands of lines. I've got various macros to filter down the sheet, but once I've narrowed it down I'd like to have a macro that can select one line from the filtered results. I did find one that worked (someone else's work) but it didn't take the filtering into account and I'm not sure how to amend it to do so.

Sub Random_Selection()
Dim ColumnA As Long
Dim StartRow As Long
Dim HeaderRow As Long
Dim LastRow As Long

ColumnA = 1
HeaderRow = 5  
StartRow = 6
LastRow = Cells(Rows.Count, 1).End(xlUp).Row

Dim randomNum As Long
randomNum = WorksheetFunction.RandBetween(StartRow, LastRow)
Cells(randomNum, ColumnA).Select

End Sub

As I say - this worked well on a random selection of 'all' the rows - but I need it to only choose at random from the 'visible' rows after filtering has been applied.

Hope someone can help and I apologise for the total lack of knowledge on my part!




Aucun commentaire:

Enregistrer un commentaire