Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88030

Randomly select data from multiple sheets in Excel [closed]

$
0
0

Sample Data

I have data of various companies arranged in different sheets date-wise.

How can I select randomly any number of sample (say 10) where data can be extracted from any sheets with the condition that there is letter Y in the corresponding row in column "H".

In other words, I need to refer to random rows from random sheets with the given criteria and get the result of the values in a separate sheet.

Found a somewhat similar post here1 and here2

Solution can be formula based but use of VBA is preferred as there is huge amount of data to be processed.

One approach may be to Collect data from multiple sheets into one with VBA code then apply autofilter and sort all rows with letter Y in column "H" and then select random sample. But with huge amount of data this process doesn't seem to be efficient as the sheet would be full of too much of data and create a problem.

The data can be filtered in their respective sheets first and then copied to a summary/master sheet and then sampling may be done.

Please, guide me in the right direction.

In case it is confusing some, the below code is what I have now as my working code (may not be the best option what I was looking for, before the question was closed- but it solved my purpose) and no longer need any answer. I have posted it just in case it may help someone in need:-

Sub Test()
    Call RunMacroTimes(3)                         'run macro 3 times
End Sub

Sub RunMacroTimes(ByRef Times)
    Do
        Application.Run "'Book1.xlsm'!CopyRandomFilteredRowsMultipleSheets"
        Times = Times - 1
        DoEvents
    Loop Until Times = 0
End Sub
Sub CopyRandomFilteredRowsMultipleSheets()
    Dim wb     As Workbook
    Dim ws     As Worksheet
    Sheets("yes").Cells.Clear
    'apply filter to sheets
    For Each ws In Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4"))
        If ws.AutoFilterMode Then ws.AutoFilterMode = False
        ws.Range("A1").AutoFilter 1, "asdf"
    Next ws
    Dim rngVisible As Range
    Dim arr    As Range
    Dim CellsToCount As Long
    Dim RandCell As Range

    Dim wf     As WorksheetFunction
    Set wf = Application.WorksheetFunction
    Sheets("Sheet"& wf.RandBetween(1, 4)).Activate
    Set rngVisible = ActiveSheet.Range("A1:A20").SpecialCells(xlCellTypeVisible)
    CellsToCount = Int(Rnd * rngVisible.Count) + 1
    With ActiveSheet
        For Each arr In rngVisible.Areas
            If arr.Cells.Count >= CellsToCount Then
                Set RandCell = arr.Cells(CellsToCount)
                Exit For
            Else
                CellsToCount = CellsToCount - arr.Cells.Count
            End If
        Next
        RandCell.Select
        ActiveSheet.Range(Selection, Selection).EntireRow.Copy
    End With
    With Sheets("yes")
        .Range("A"& .Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
    End With
End Sub
Sub ClearFilterFromAllSheets()
    For Each ws In Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4"))
        If ws.AutoFilterMode Then ws.AutoFilterMode = False
    Next ws
End Sub

Viewing all articles
Browse latest Browse all 88030

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>