Updated workbook attached. I have worked on this more and have cleaned up the code some. This is what I have:
Sub routine()
' routine to read spreadsheet(s)
' pull data from sheets and create report sheet
Dim wrkbkname As String
Dim ws As Worksheet
Dim rundate As Date
Dim LR As Long, i As Long, j As Long
Dim NR As Long, LC As Long
Dim c As Range, firstaddress As String
Dim rng As Range
Dim colName As String, colName2 As String
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = ActiveSheet
' define scan range start date is midnight
lodate = Format(dtselect1, "mm/dd/yyyy")
hidate = Format(dtselect2, "mm/dd/yyyy")
ActiveSheet.Range("A1:L60000").Select
' create tab(s) for report
frmDateSelect.txtUpdate.Text = " Processing"
DoEvents
' turn off error window on delete
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Sheets("Distribution").Select
Set ws1 = ActiveSheet
LC = ws1.Cells(1, Columns.Count).End(xlToLeft).Column
colName = Replace(Cells(1, LC + 2).Address(0, 0), 1, "")
colName2 = Replace(Cells(1, LC + 2).Address(0, 0), 1, "")
Worksheets.Add(Before:=ws1).Name = dtselect4
Set ws2 = ActiveSheet
ws2.Range("A1:" & colName & 1).Value = ws1.Range("A1:" & colName & 11).Value
With ws1
.Range("A2:" & colName & 1).Copy
With ws2.Range("A2:" & colName & 1)
ActiveSheet.Paste
.RowHeight = 12
Range("D3").Select
End With
LR = .Cells(Rows.Count, 1).End(xlUp).row + 1
ws1.Range(colName2 & 2).Formula = "=IF(D3>=lodate And D3<=hidate,""Copy"","""")"
ws1.Range(colName2 & 2).Copy ws1.Range(colName2 & 3 & ":" & colName2 & LR)
With ws1.Range(colName2 & 2 & ":" & colName2 & LR)
.Value = .Value
End With
NR = 2
With .Columns(LC + 2)
Set c = .Find("Copy", LookIn:=xlValues, lookat:=xlWhole)
If Not c Is Nothing Then
firstaddress = c.Address
Do
ws1.Range("A" & c.row & ":" & colName & c.row).Copy ws2.Range("A" & NR & ":" & colName & NR)
Set c = .FindNext(c)
NR = NR + 1
Loop While Not c Is Nothing And c.Address <> firstaddress
End If
End With
End With
ws2.Range("C2:C" & NR).WrapText = False
ws2.Range("A1:" & colName & NR).Columns.AutoFit
ws1.Range(colName2 & 2 & ":" & colName2 & LR).ClearContents
ws2.Select
Range("F1").Select
Application.ScreenUpdating = True
' turn off error window on delete
Application.DisplayAlerts = False
' put cursor back in a1
ActiveSheet.Range("A1").Select
Application.DisplayAlerts = True
frmDateSelect.txtUpdate.Text = " Done...Finished Processing "
DoEvents
End Sub
This will copy the 2 header lines to a new sheet with the name you input on the userform when the button on the Utilities tab is pressed. The 2 problems I am still having is that column 'L' data is deleted and shaded grey after the processing is complete, and none of the data within the dates selected on the userform is copied to the new sheet which should look like the Example Output sheet. Can anyone help me with this???
Thanks,
Andrew
Bookmarks