I am using this syntax to bring in the names of all employees whom are active, then from there filter the data on subsequent worksheets and use a formula to bring the data over to the All
worksheet.
This syntax works, but I am by no means a VBA guru, and would love any ways on optimization to speed this up!
Function GatherData()
'Declaring variables
Dim sortValue As String, startdateparam As String, enddateparam As String
Dim startdateparamyear As String, enddateparamyear As String
Dim FirstDayOfYear As String, LastDayOfYear As String
'Setting variables
startdateparam = "01/01/2016"
enddateparam = "03/01/2016"
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'If sheet exists deleting it
On Error Resume Next
ThisWorkbook.Sheets("All").Delete
'Adding in an All worksheet
With ThisWorkbook
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "All"
End With
'Setting and declaring variables
Dim copyto As Worksheet: Set copyto = Sheets("All")
Dim copyfrom As Worksheet: Set copyfrom = Sheets("Data")
'Filtering the data before copying
With Sheets("Data")
With .ListObjects("Data").Range
.AutoFilter
.AutoFilter Field:=16, Criteria1:=">=" & CDbl(CDate(startdateparam)), Operator:=xlAnd, Criteria2:=" <= " & CDbl(CDate(enddateparam))
End With
End With
'Copying Distinct Customer Names To The All Worksheet
copyfrom.Range("A2:A65536").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=copyto.Range("A2"), Unique:=True
'Removing any duplicates that were copied
Columns("A:A").Select
ActiveSheet.Range("$A$1:$A$65536").RemoveDuplicates Columns:=1, Header:=xlNo
Range("A1").Select
'Filtering Data before copy
With Sheets("Data")
With .ListObjects("Data").Range
.AutoFilter
.AutoFilter Field:=16, Criteria1:=">=" & CDbl(CDate(startdateparam)), Operator:=xlAnd, Criteria2:=" <= " & CDbl(CDate(enddateparam))
End With
End With
'Altering variables for new format
startdateparamyear = Month(startdateparam) & "/" & Day(startdateparam) & "/" & Year(startdateparam) - 1
enddateparamyear = Month(enddateparam) & "/" & Day(enddateparam) & "/" & Year(enddateparam) - 1
With Sheets("TR")
With .ListObjects("TR").Range
.AutoFilter
.AutoFilter Field:=5, Criteria1:=">=" & CDbl(CDate(startdateparamyear)), Operator:=xlAnd, Criteria2:=" <= " & CDbl(CDate(enddateparamyear))
End With
End With
'Altering variables for new filter
FirstDayOfYear = "1/1/" & Year(startdateparam)
LastDayOfYear = "12/31/" & Year(enddateparam)
With Sheets("TR")
With .ListObjects("TR").Range
.AutoFilter
.AutoFilter Field:=1, Criteria1:=sortValue
.AutoFilter Field:=5, Criteria1:=">=" & CDbl(CDate(FirstDayOfYear)), Operator:=xlAnd, Criteria2:=" <= " & CDbl(CDate(LastDayOfYear))
End With
End With
'Updating all sheet with data
With Sheets("All")
With .Range("C2:C" & .Cells(.Rows.Count, "A").End(xlUp).Row)
.Formula = "=SUMIFS('Data'!C:C,'Data'!A:A,'All Customers'!A2)"
.Value = .Value
End With
With .Range("D2:D" & .Cells(.Rows.Count, "A").End(xlUp).Row)
.Formula = "=SUMIFS('Data'!D:D,'Data'!A:A,'All Customers'!A2)"
.Value = .Value
End With
With .Range("E2:E" & .Cells(.Rows.Count, "A").End(xlUp).Row)
.Formula = "=SUMIFS('TR'!B:B,'TR'!A:A,'All Customers'!A2)"
.Value = .Value
End With
With .Range("F2:F" & .Cells(.Rows.Count, "A").End(xlUp).Row)
.FormulaR1C1 = "=SUM((RC[-3]+RC[-2])-RC[-1])"
.Value = .Value
End With
'Function To Bring in Additional Data
CopyMoreData
'Updating All worksheet Again
With .Range("J2:J" & .Cells(.Rows.Count, "A").End(xlUp).Row)
.Formula = "=SUMIFS('TR'!B:B,'TR'!A:A,'All Customers'!A2)"
.Value = .Value
End With
With .Range("K2:K" & .Cells(.Rows.Count, "A").End(xlUp).Row)
.FormulaR1C1 = "=IFERROR(SUM((RC[-7]+RC[-6])/RC[-3])/12,0)"
.Value = .Value
End With
With .Range("M2:M" & .Cells(.Rows.Count, "A").End(xlUp).Row)
.FormulaR1C1 = "=IFERROR(SUM((RC[-7]+RC[-6])/RC[-3]),0)"
.Value = .Value
End With
'Bringing in more data
NewOrOldCust
'Bringing in even more data
GetManager
End With
End Function
Sub CopyMoreData()
Dim v As Variant, i As Long, j As Long, arr As Variant
Dim ws1 As Worksheet: Set ws1 = Sheets("Data")
Dim ws2 As Worksheet: Set ws2 = Sheets("All")
v = ws1.Range("A1").CurrentRegion.Value
With CreateObject("Scripting.Dictionary")
.CompareMode = 1
For i = 2 To UBound(v, 1)
If Not .Exists(v(i, 1)) Then
.Item(v(i, 1)) = Array(v(i, 7), v(i, 8), v(i, 9))
Else
arr = .Item(v(i, 1))
For j = LBound(arr) To UBound(arr)
If arr(j) = 0 Or arr(j) = "" Then
If v(i, j + 7) <> 0 And v(i, j + 7) <> "" Then arr(j) = v(i, j + 7)
End If
Next j
.Item(v(i, 1)) = arr
End If
Next i
Application.ScreenUpdating = False
ws2.Range("A2").Resize(.Count).Value = Application.Transpose(.Keys)
i = 2
For Each itm In .Items
ws2.Cells(i, 7).Resize(, 3).Value = itm
i = i + 1
Next itm
Application.GoTo ws2.Range("A1")
Application.ScreenUpdating = True
End With
End Sub
Public Sub NewOrOldCust()
Const kFml As String = "=IF( ISERROR( MATCH( RC1, #rSrc, 0 ) ), ""No"", ""Yes"" )"
Dim rTrg As Range, rSrc As Range
Dim sFml As String
With ThisWorkbook.Worksheets("All")
Set rTrg = .Range("N2:N" & .Cells(.Rows.Count, 1).End(xlUp).Row)
End With
With ThisWorkbook.Worksheets("NewOrOld")
Set rSrc = .Range("A1:A" & .Cells(.Rows.Count, 1).End(xlUp).Row)
End With
Rem Reset Formula
sFml = kFml
sFml = Replace(sFml, "#rSrc", rSrc.Address(ReferenceStyle:=xlR1C1, External:=1))
Rem Apply Formula
With rTrg
.FormulaR1C1 = sFml
.Value = .Value2
End With
End Sub
Public Sub GetManager()
With ThisWorkbook.Worksheets("All")
lr = .Cells(.Rows.Count, 1).End(xlUp).Row
With .Range("B2:B" & lr)
.Formula = "=IFERROR(IF(VLOOKUP(A1, 'Data'!A:B, 2, 0)="""","""",VLOOKUP(A1, 'Data'!A:B, 2, 0)),"""")"
.Value = .Value
End With
End With
End Sub