Macro or VBA to Clear Contents of Rows By Date (Starting At A5)
Please don't reply, I figured it out (didn't have much time and one
problem was fixed by repeating a step):
'Clear contents of monthly data for refreshing
Range("A5:J1000").Select
Selection.ClearContents
'Copy data from all worksheets starting in cell A5 to master tab
Dim sht As Worksheet
Dim target As Worksheet
Dim rng As Range
Dim targetrng As Range
'You can change "Master" to your exact merge sheet name (tab
name for the sheet)
Set target = ActiveWorkbook.Worksheets("Master")
For Each sht In ActiveWorkbook.Worksheets
If Not sht.Name = target.Name Then
'Set rng = sht.UsedRange
Set rng = sht.Range("a5:J500")
Set targetrng = target.Cells(65536, 1).End(xlUp).Offset(5)
rng.Copy targetrng
End If
Next sht
Range("A5:J500").Select
'Run sort routine by date on list
ActiveWorkbook.Worksheets("Master").Sort.SortField s.Clear
ActiveWorkbook.Worksheets("Master").Sort.SortField s.Add Key:=Range
("A5:A500") _
, SortOn:=xlSortOnValues, Order:=xlAscending,
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Master").Sort
.SetRange Range("A5:J500")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Dim c As Range, compdate As Date
compdate = Range("A1").Value
If IsDate(compdate) Then
For Each c In Range("A5:A500", Range("K65536").End(xlUp))
If IsDate(c.Value) And c.Value compdate Then
c.EntireRow.Delete
End If
Next
End If
If IsDate(compdate) Then
For Each c In Range("A5:A500", Range("K65536").End(xlUp))
If IsDate(c.Value) And c.Value compdate Then
c.EntireRow.Delete
End If
Next
End If
End Sub
|