Hello all,
I've written some VBA code to copy column headers and random selected records on to a new worksheet. However, the data that is being copied is overwriting the column headers. I've tried a few things to have the data pasted starting on the second row, but I've been unsuccessful thus far. I was hoping someone can take a look at my code to see if someone can figure out why the column headers are being overwritten. Any and all help is much appreciated. The code I have is below:
Thank you,
D.
I've written some VBA code to copy column headers and random selected records on to a new worksheet. However, the data that is being copied is overwriting the column headers. I've tried a few things to have the data pasted starting on the second row, but I've been unsuccessful thus far. I was hoping someone can take a look at my code to see if someone can figure out why the column headers are being overwritten. Any and all help is much appreciated. The code I have is below:
VBA Code:
Sub Filter_by_month()
'
' Filter_by_month Macro
Sheets("FILENAME").Range("A:M").AutoFilter Field:=7, Criteria1:=xlFilterLastMonth, _
Operator:=xlFilterDynamic
End Sub
Sub CreateSheet()
Sheets.Add After:=Sheets(Sheets.Count)
End Sub
Sub Copy_Header()
Application.ScreenUpdating = False
Dim i As Long
For i = 2 To Sheets.Count
Sheets("FILENAME").Rows(1).Copy Destination:=Sheets("Sheet1").Rows(1)
Next
Sheets("Sheet1").Cells(1, 1).Select
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
Sub Copy()
Dim LastRow As Long
Dim NbRows As Long
Dim RowList()
Dim i As Long, J As Long, k As Long
Dim RowNb As Long
Dim s As String
Sheets("FILENAME").Activate
Application.ScreenUpdating = False
For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
s = i & ":" & i
If IsEmpty(Cells(i, 1).Value) Then
Rows(s).EntireRow.Hidden = False
End If
Next
Application.ScreenUpdating = True
Sheets("FILENAME").Activate
LastRow = Range("A" & Rows.Count).End(xlUp).Row
NbRows = LastRow * 0.1
'NbRows = IIf(LastRow < 200, LastRow * 0.1, 10)
ReDim RowList(2 To NbRows)
k = 1
For i = 2 To NbRows
RowNb = Rnd() * LastRow
For J = 1 To k
If (RowList(J) = RowNb) Then GoTo NextStep
Next J
RowList(k) = RowNb
Rows(RowNb).Copy Destination:=Sheets("Sheet1").Cells(k, "A")
k = k + 1
NextStep:
Next i
End Sub
Thank you,
D.