Hello all,
I'm trying to modify a random selection code I have. I'm trying to retrieve 5% of the total number of records in the raw data. I'm currently receiving a run-time error when I try to run it. I'm not entirely sure why it's not working. I commented out the portion of my code that filters on current month, and I tried to change the autofilter values, but not entirely sure what I should place after the autofilter in the code. This is the code I have:
This is the line that is being highlighted after I receive the run-time error:
Any help is greatly appreciated. Thank you.
D.
I'm trying to modify a random selection code I have. I'm trying to retrieve 5% of the total number of records in the raw data. I'm currently receiving a run-time error when I try to run it. I'm not entirely sure why it's not working. I commented out the portion of my code that filters on current month, and I tried to change the autofilter values, but not entirely sure what I should place after the autofilter in the code. This is the code I have:
VBA Code:
Option Explicit
Sub Filter_Data()
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Worksheets("CMOR_XSAR_QA_Data")
Set ws2 = Worksheets("Random Selection")
'Filter & copy current month records
' If ws1.AutoFilterMode Then ws1.AutoFilter.ShowAllData
' With ws1.Range("A1").CurrentRegion
' .AutoFilter
' If .SpecialCells(xlCellTypeVisible).Address <> .Rows(1).Address Then
' ws2.Range("A1").CurrentRegion.Offset(1).ClearContents
' .Offset(1).Resize(.Rows.Count - 1).Copy ws2.Range("A2")
' Else
' MsgBox "No records found for the current month"
' .AutoFilter
' Exit Sub
' End If
' .AutoFilter
' End With
'Randomise the records & show 10% only
Dim LRow As Long, LCol As Long, i As Long
LRow = ws2.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
LCol = ws2.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
With ws2
With .Range(.Cells(2, LCol), .Cells(LRow, LCol))
.Formula = "=Rand()"
End With
.Range("A1").CurrentRegion.Sort key1:=ws2.Cells(2, LCol), order1:=xlAscending, Header:=xlYes
.Columns(LCol).ClearContents
.Rows(Int((LRow - 1) / 5) + 3 & ":" & LRow).Delete
End With
End Sub
This is the line that is being highlighted after I receive the run-time error:
Code:
.Range("A1").CurrentRegion.Sort key1:=ws2.Cells(2, LCol), order1:=xlAscending, Header:=xlYes
Any help is greatly appreciated. Thank you.
D.