I have a data column on a tab [Data] with date/times listed in column A that I need to compare with a range of dates & times on tab [Home] and return the text to the left of the dates if the time is between them. I am having issues with the range of dates on [Home] throwing it off, I have done this kind of thing with a single cell but not a range of cells like I have now. A step further is if the first word on [Data] in column B matches "Line1" it needs to compare dates form a certain range of cells and if "Line2" than another. Want to return say the day of the week into Column E, to show what "Sales Date" that line item was from instead of actual time.
Any help would be GREATLY appreciated!
I should add that sheet [Data] and other tabs is pulled and created from a text file via a hefty macro.
Any help would be GREATLY appreciated!
WasteData.xlsm | |||||
---|---|---|---|---|---|
A | B | C | |||
10 | Line1 | ||||
11 | mm/dd/yyyy hh:mm | mm/dd/yyyy hh:mm | |||
12 | Sales Date | Start | Finish | ||
13 | Thursday | 7/6/2020 8:39 | |||
14 | Friday | ||||
15 | Saturday | ||||
16 | Monday | 7/3/2020 23:47 | 7/3/2020 3:02 | ||
17 | Tuesday | 7/3/2020 3:13 | 7/4/2020 5:45 | ||
18 | |||||
19 | Line2 | ||||
20 | mm/dd/yyyy hh:mm | mm/dd/yyyy hh:mm | |||
21 | Sales Date | Start | Finish | ||
22 | Thursday | 7/6/2020 10:58 | |||
23 | Friday | ||||
24 | Saturday | ||||
25 | Monday | 7/1/2020 8:48 | 7/2/2020 4:16 | ||
26 | Tuesday | 7/2/2020 4:25 | 7/2/2020 7:45 | ||
Home |
WasteData.xlsm | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | Date/Time | Location | Weight (Lbs) | Sales Date | |||
2 | 7/4/2020 0:02 | Line1 Mixer | 200 | ||||
3 | 7/4/2020 0:03 | Line1 Mixer | 167 | ||||
4 | 7/4/2020 3:08 | Line1 Proofbox | 604 | ||||
5 | 7/4/2020 3:10 | Line1 Oven | 484 | ||||
6 | 7/4/2020 3:18 | Line1 Wrap 1 | 375 | ||||
7 | 7/4/2020 3:18 | Line1 Wrap 2 | 431 | ||||
8 | 7/4/2020 3:19 | Line1 Destroy | 149 | ||||
9 | 7/4/2020 3:20 | Line1 Destroy | 326 | ||||
10 | 7/4/2020 3:38 | Line1 Wrap 2 | 275 | ||||
11 | 7/4/2020 4:07 | Line2 Wrap 1 | 141 | ||||
12 | 7/4/2020 4:11 | Line2 Wrap 2 | 321 | ||||
13 | 7/4/2020 4:12 | Line2 Wrap 2 | 360 | ||||
14 | 7/4/2020 4:13 | Line1 Mixer | 299 | ||||
Data |
I should add that sheet [Data] and other tabs is pulled and created from a text file via a hefty macro.
VBA Code:
Sub wastereport()
'***********************Force reset***********************
For Each sh In Worksheets
If sh.Name Like "Data" Then
Sheets("Home").Activate
MsgBox "Please reset data"
Exit Sub
End If
Next
'*********************************************************
'*********************Retreive Data from .txt*************
Dim FileNum As Integer
Dim DataLine As String
mypath = Application.ActiveWorkbook.Path 'Path for datafile
filename = mypath & "\" & "WasteData.txt"
FileNum = FreeFile()
Open filename For Input As #FileNum
Worksheets.Add(After:=Worksheets(1)).Name = "Data"
counter = 2
While Not EOF(FileNum)
Line Input #FileNum, DataLine ' read in data 1 line at a time
Cells(counter, 1) = DataLine
counter = counter + 1
Wend
Close #FileNum
Selection = Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.TextToColumns DataType:=xlDelimited, _
ConsecutiveDelimiter:=False, Comma:=True
'*******************************************************************
'***************************Select data from dates******************
startdate = Sheets("Home").Cells(3, 2)
enddate = Sheets("Home").Cells(3, 4)
lastweight = Range("C65536").End(xlUp).Row 'find last row
deletecount = 0
For i = 2 To lastweight
If Cells(i, 1) < startdate Then
deletecount = deletecount + 1
End If
Next
If deletecount > 0 Then
Range(Cells(2, 1), Cells(deletecount + 1, 14)).Select
Selection.Delete shift:=xlUp
End If
deletecount2 = 0
lastweight = Range("C65536").End(xlUp).Row 'find last row
For i = 2 To lastweight
If Cells(i, 1) > enddate Then
deletecount2 = deletecount2 + 1
End If
Next
If deletecount2 > 0 Then
Range(Cells(lastweight - deletecount2 + 1, 1), Cells(lastweight, 14)).Select
Selection.Delete shift:=xlUp
End If
''*****************************************************************************
''************************Clean up data and add headings*************
lastweight = Range("C65536").End(xlUp).Row 'find last row
For i = 2 To lastweight
Cells(i, 3) = Trim(Cells(i, 3))
Cells(i, 3) = Replace(Cells(i, 3), "[", "")
Cells(i, 3) = Replace(Cells(i, 3), "]", "")
Cells(i, 2) = Trim(Cells(i, 2))
Next
For i = lastweight To 2 Step -1
If Cells(i, 3) < 200 Then
Cells(i, 3).EntireRow.Delete
End If
Next
lastweight = Range("C65536").End(xlUp).Row 'find last row
For i = 2 To lastweight
Cells(i, 3) = Cells(i, 3) - 200 'Trim 200lbs to tare out dumpster weight
Next
Cells(1, 1) = "Date/Time": Cells(1, 2) = "Location": Cells(1, 3) = "Weight (Lbs)"
''*******************************Collect groups********************************
Dim Locations(0 To 16) As String
Dim Lines(0 To 1) As String
Dim Locationweights(0 To 16) As Double
Dim Lineweights(0 To 1) As Double
Locations(0) = "Bread Mixer": Locations(1) = "Bread Divider": Locations(2) = "Bread Proofbox": Locations(3) = "Bread Oven"
Locations(4) = "Bread Wrap 1": Locations(5) = "Bread Wrap 2": Locations(6) = "Bread Wrap 3": Locations(7) = "Buns Eagle / Pan-O-Mat"
Locations(8) = "Buns Proofbox": Locations(9) = "Buns Oven": Locations(10) = "Buns Cooler": Locations(11) = "Buns Wrap 1"
Locations(12) = "Buns Wrap 2": Locations(13) = "Buns Bulk Packer": Locations(14) = "Bread Destroy": Locations(15) = "Buns Destroy"
Locations(16) = "Bread Crumbs"
Lines(0) = "Bread": Lines(1) = "Buns"
For j = 0 To 16
For i = 2 To lastweight
If Cells(i, 2) = Locations(j) Then
Locationweights(j) = Locationweights(j) + Cells(i, 3)
End If
Next
Next
For i = 0 To 6
Lineweights(0) = Lineweights(0) + Locationweights(i)
Lineweights(1) = Lineweights(1) + Locationweights(i + 7)
Next
Lineweights(0) = Lineweights(0) + Locationweights(14) + Locationweights(16)
Lineweights(1) = Lineweights(1) + Locationweights(15)
firstday = DateValue(Month(Cells(2, 1)) & "/" & Day(Cells(2, 1)) & "/" & Year(Cells(2, 1)))
lastday = DateValue(Month(Cells(lastweight, 1)) & "/" & Day(Cells(lastweight, 1)) & "/" & Year(Cells(lastweight, 1)))
numdays = lastday - firstday
ReDim days(0 To numdays) As String
ReDim dayweights(0 To numdays) As Single
For i = 0 To numdays
days(i) = firstday + i
Next
ReDim bunsdays(0 To numdays) As String
ReDim breaddays(0 To numdays) As String
ReDim bunsdayweights(0 To numdays) As Double
ReDim breaddayweights(0 To numdays) As Double
For i = 0 To numdays
For j = 2 To lastweight
If DateValue(Month(Cells(j, 1)) & "/" & Day(Cells(j, 1)) & "/" & Year(Cells(j, 1))) = days(i) Then
If Cells(j, 2) = Locations(0) Or Cells(j, 2) = Locations(1) Or Cells(j, 2) = Locations(2) Or Cells(j, 2) = Locations(3) Or Cells(j, 2) = Locations(4) Or Cells(j, 2) = Locations(5) Or Cells(j, 2) = Locations(6) Or Cells(j, 2) = Locations(14) Or Cells(j, 2) = Locations(16) Then
breaddayweights(i) = breaddayweights(i) + Cells(j, 3)
ElseIf Cells(j, 2) = Locations(7) Or Cells(j, 2) = Locations(8) Or Cells(j, 2) = Locations(9) Or Cells(j, 2) = Locations(10) Or Cells(j, 2) = Locations(11) Or Cells(j, 2) = Locations(12) Or Cells(j, 2) = Locations(13) Or Cells(j, 2) = Locations(15) Then
bunsdayweights(i) = bunsdayweights(i) + Cells(j, 3)
End If
End If
Next
Next
Dim Breadwrap(0 To 5) As String
Dim Breadwrapweight(0 To 5) As Double
Breadwrap(0) = "Bread Wrap 1": Breadwrap(1) = "Bread Wrap 2": Breadwrap(2) = "Bread Wrap 3"
Breadwrap(3) = "Bread Wrap 1": Breadwrap(4) = "Bread Wrap 2": Breadwrap(5) = "Bread Wrap 3"
For i = 0 To 2
For j = 2 To lastweight
If Hour(Cells(j, 1)) >= 8 And Hour(Cells(j, 1)) < 16 Then
If Cells(j, 2) = Breadwrap(i) Then
Breadwrapweight(i) = Breadwrapweight(i) + Cells(j, 3)
End If
ElseIf Hour(Cells(j, 1)) < 8 Or Hour(Cells(j, 1)) >= 16 Then
If Cells(j, 2) = Breadwrap(i) Then
Breadwrapweight(i + 3) = Breadwrapweight(i + 3) + Cells(j, 3)
End If
End If
Next
Next
'*****************************************************************************************
'***************************Write to sheet "Tables"***************************************
Worksheets.Add(After:=Worksheets(2)).Name = "Tables"
Cells(1, 1) = "By Line": Cells(2, 1) = "Line": Cells(2, 2) = "Weight (lbs)"
Cells(3, 1) = Lines(0): Cells(4, 1) = Lines(1): Cells(3, 2) = Lineweights(0): Cells(4, 2) = Lineweights(1)
Cells(5, 1) = "Total": Cells(5, 2) = Application.Sum(Range(Cells(3, 2), Cells(4, 2)))
Cells(1, 4) = "By Location": Cells(2, 4) = "Location": Cells(2, 5) = "Weight (lbs)"
For i = 0 To 16
Cells(i + 3, 4) = Locations(i)
Cells(i + 3, 5) = Locationweights(i)
Next
Cells(20, 4) = "Total": Cells(20, 5) = Application.Sum(Range(Cells(3, 5), Cells(19, 5)))
Cells(1, 7) = "By Day": Cells(2, 7) = "Day": Cells(2, 8) = "Bread (lbs)": Cells(2, 9) = "Buns (lbs)"
For i = 0 To numdays
Cells(i + 3, 7) = days(i)
Cells(i + 3, 8) = breaddayweights(i)
Cells(i + 3, 9) = bunsdayweights(i)
Next
Cells(numdays + 4, 7) = "Total": Cells(2, 10) = "Total": Cells(numdays + 4, 8) = Application.Sum(Range(Cells(3, 8), Cells(numdays + 3, 8)))
Cells(numdays + 4, 9) = Application.Sum(Range(Cells(3, 9), Cells(numdays + 3, 9)))
For i = 0 To numdays + 1
Cells(i + 3, 10) = Application.Sum(Range(Cells(i + 3, 8), Cells(i + 3, 9)))
Next
Cells(1, 12) = "Bread Wrap by Shift": Cells(3, 12) = "Shift 1": Cells(4, 12) = "Shift 2"
Cells(2, 13) = "Wrapper 1": Cells(2, 14) = "Wrapper 2": Cells(2, 15) = "Wrapper 3"
For i = 0 To 2
Cells(3, 13 + i) = Breadwrapweight(i)
Cells(4, 13 + i) = Breadwrapweight(i + 3)
Next
Cells(5, 12) = "Total": Cells(2, 16) = "Total"
For i = 0 To 2
Cells(5, 13 + i) = Application.Sum(Range(Cells(3, 13 + i), Cells(4, 13 + i)))
Cells(i + 3, 16) = Application.Sum(Range(Cells(i + 3, 13), Cells(i + 3, 15)))
Next
'***************************************************************************************
'************************Make tables look pretty****************************************
With Range(Cells(1, 1), Cells(1, 4)).Font
.Bold = True
End With
With Range(Cells(1, 7), Cells(1, 12)).Font
.Bold = True
End With
With Columns("B")
.ColumnWidth = .ColumnWidth * 1.5
End With
With Columns("C")
.ColumnWidth = .ColumnWidth * 0.4
End With
With Columns("D")
.ColumnWidth = .ColumnWidth * 2
End With
With Columns("E")
.ColumnWidth = .ColumnWidth * 1.5
End With
With Columns("F")
.ColumnWidth = .ColumnWidth * 0.4
End With
With Columns("G")
.ColumnWidth = .ColumnWidth * 2
End With
With Columns("H")
.ColumnWidth = .ColumnWidth * 1.5
End With
With Columns("I")
.ColumnWidth = .ColumnWidth * 1.2
End With
With Columns("J")
.ColumnWidth = .ColumnWidth * 2
End With
With Columns("K")
.ColumnWidth = .ColumnWidth * 0.4
End With
With Columns("M")
.ColumnWidth = .ColumnWidth * 1.3
End With
With Columns("N")
.ColumnWidth = .ColumnWidth * 1.3
End With
With Columns("O")
.ColumnWidth = .ColumnWidth * 1.3
End With
Range(Cells(2, 1), Cells(4, 2)).Borders.LineStyle = xlContinuous
Range(Cells(2, 4), Cells(19, 5)).Borders.LineStyle = xlContinuous
Range(Cells(2, 7), Cells(numdays + 3, 10)).Borders.LineStyle = xlContinuous
Range(Cells(2, 12), Cells(4, 15)).Borders.LineStyle = xlContinuous
'**************************************************************************
'**********************Charts**********************************************
Worksheets.Add(After:=Worksheets(3)).Name = "Charts"
Dim Chart1 As Chart
Sheets("Tables").Activate
With ActiveSheet
Set DataRange = .Range(.Cells(3, 1), .Cells(4, 2))
End With
Set Chart1 = Sheets("Charts").Shapes.AddChart(xl3DColumnClustered).Chart
Chart1.SetSourceData Source:=DataRange
With Chart1
.HasTitle = True
.ChartTitle.Text = "Waste by Line"
.HasLegend = False
.ChartStyle = 2
End With
Dim Chart2 As Chart
Sheets("Tables").Activate
With ActiveSheet
Set DataRange = .Range(.Cells(3, 4), .Cells(19, 5))
End With
Set Chart2 = Sheets("Charts").Shapes.AddChart(xl3DColumnClustered).Chart
Chart2.SetSourceData Source:=DataRange
With Chart2
.HasTitle = True
.ChartTitle.Text = "Waste by Location"
.HasLegend = False
.ChartStyle = 2
End With
Dim Chart3 As Chart
Sheets("Tables").Activate
With ActiveSheet
Set DataRange = .Range(.Cells(2, 7), .Cells(numdays + 3, 9))
End With
Set Chart3 = Sheets("Charts").Shapes.AddChart(xl3DColumnClustered).Chart
Chart3.SetSourceData Source:=DataRange
With Chart3
.HasTitle = True
.ChartTitle.Text = "Waste by Day"
.HasLegend = True
.ChartStyle = 2
End With
Dim Chart4 As Chart
Sheets("Tables").Activate
With ActiveSheet
Set DataRange = .Range(.Cells(2, 12), .Cells(4, 15))
End With
Set Chart4 = Sheets("Charts").Shapes.AddChart(xl3DColumnClustered).Chart
Chart4.SetSourceData Source:=DataRange
With Chart4
.HasTitle = True
.ChartTitle.Text = "Bread Wrap by Shift and Wrapper"
.HasLegend = True
.ChartStyle = 2
End With
Sheets("Charts").Activate
Dim ChtObj As ChartObject
L = 10
T = 10
For Each ChtObj In ActiveSheet.ChartObjects
ChtObj.Left = L
ChtObj.Top = T
L = L + 400
If L > 500 Then
L = 10
T = T + 250
End If
Next ChtObj
End Sub
Last edited by a moderator: