Hello, wondering if somebody could help me figure this out, or improve on an existing code?
Please note in this sample I'm using sheets but at work I actually have one master workbook and some individual workbooks where I'm pulling the data from. I can't download the plugin at work, therefore doing my best to recall everything. In truth she sheets contain a lot more columns. But here are the essential details.
I essentially have this master sheet:
And these five individual sheets:
And I'd like to get to this:
Sorry if the example is not exactly perfect. I was trying to recreate this sample from memory.
1st I need to pull the total from each "Amount Due" Column in each workbook if the name on sheet(she1) is found on the respective individual billing individual sheet.
Once That is done I want to paste the total from column J back into column G sheets2 and Sheet3 as these sheets contain a column "credit column"
P.S I have this code that helps me get what I need with the real data at work, which uses sumifs to retrieve the data but it feels like this can be improved on for efficiency or for readability. Sorry I could not provide an example of the real data. Still Wondering if you guys had any tips.
.
Please note in this sample I'm using sheets but at work I actually have one master workbook and some individual workbooks where I'm pulling the data from. I can't download the plugin at work, therefore doing my best to recall everything. In truth she sheets contain a lot more columns. But here are the essential details.
I essentially have this master sheet:
Book3 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | |||
1 | A Suite | Jun-24 | ||||||||||
2 | Name | Country | City | Type | Minerals | Games | TV | Toys | Shoes | Total | ||
3 | Allucar | USA | NYC | Return | 0 | |||||||
4 | Diane | JP | Tokyo | New | 0 | |||||||
5 | Helen | CAN | Vancouver | New | 0 | |||||||
6 | Radahn | GER | Hamburg | New | 0 | |||||||
7 | Rennah | USA | Seattle | Return | 0 | |||||||
8 | Alexander | USA | Fairfax | New | 0 | |||||||
9 | Ty | USA | NYC | New | 0 | |||||||
10 | Bayle | JP | Tokyo | Return | 0 | |||||||
Sheet1 (2) |
Cell Formulas | ||
---|---|---|
Range | Formula | |
J3:J10 | J3 | =SUM(E3:I3) |
And these five individual sheets:
Book3 | |||||||||
---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | |||
1 | A Suite | Jun-24 | |||||||
2 | Name | Country | City | Type | Item A | Amount Due | Credit | ||
3 | Emily | USA | NYC | Return | Cars | 11963 | 0 | ||
4 | Jane | JP | Tokyo | New | Clothing | 12017 | 0 | ||
5 | Helen | CAN | Vancouver | New | Minerals | 11151 | 0 | ||
6 | Diane | GER | Hamburg | New | Games | 10516 | 0 | ||
7 | Jess | USA | Seattle | Return | Games | 12688 | 0 | ||
Sheet2 |
Book3 | |||||||||
---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | |||
1 | A Suite | Jun-24 | |||||||
2 | Name | Country | City | Type | Item A | Amount Due | Credit | ||
3 | Ranni | USA | NYC | Return | Cars | 10000 | 0 | ||
4 | Radahn | JP | Tokyo | New | Clothing | 10000 | 0 | ||
5 | Rennah | CAN | Vancouver | New | Minerals | 10000 | 0 | ||
6 | Fia | GER | Hamburg | New | Games | 10000 | 0 | ||
Sheet3 |
Book3 | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | A Suite | Jun-24 | Toys | ||||
2 | Name | Country | City | Type | Amount Due | ||
3 | Blake | USA | NYC | Return | 500 | ||
4 | Clay | JP | Tokyo | New | 300 | ||
5 | Ty | CAN | Vancouver | New | 400 | ||
6 | John | GER | Hamburg | New | 200 | ||
Sheet5 |
Book3 | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | A Suite | Jun-24 | Toys | ||||
2 | Name | Country | City | Type | Amount Due | ||
3 | Blake | USA | NYC | Return | 500 | ||
4 | Clay | JP | Tokyo | New | 300 | ||
5 | Ty | CAN | Vancouver | New | 400 | ||
6 | John | GER | Hamburg | New | 200 | ||
Sheet5 |
Book3 | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | A Suite | Jun-24 | Shoes | ||||
2 | Name | Country | City | Type | Amount Due | ||
3 | Blaine | USA | NYC | Return | 100 | ||
4 | Allucar | JP | Tokyo | New | 500 | ||
5 | Bayle | CAN | Vancouver | New | 350 | ||
6 | Diane | CAN | Vancouver | New | 200 | ||
7 | Helen | CAN | Vancouver | New | 200 | ||
Sheet6 |
And I'd like to get to this:
Book3 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | |||
1 | A Suite | Jun-24 | ||||||||||
2 | Name | Country | City | Type | Minerals | Games | TV | Toys | Shoes | Total | ||
3 | Allucar | USA | NYC | Return | 0 | 0 | 500 | 0 | 0 | 500 | ||
4 | Diane | JP | Tokyo | New | 10516 | 0 | 0 | 0 | 200 | 10716 | ||
5 | Helen | CAN | Vancouver | New | 12151 | 0 | 0 | 0 | 200 | 12351 | ||
6 | Radahn | GER | Hamburg | New | 0 | 10000 | 0 | 0 | 0 | 10000 | ||
7 | Rennah | USA | Seattle | Return | 0 | 10000 | 0 | 0 | 0 | 10000 | ||
8 | Alexander | USA | Fairfax | New | 0 | 0 | 0 | 200 | 0 | 200 | ||
9 | Ty | USA | NYC | New | 0 | 0 | 0 | 0 | 0 | 0 | ||
10 | Bayle | JP | Tokyo | Return | 0 | 0 | 0 | 0 | 350 | 350 | ||
She1 |
Cell Formulas | ||
---|---|---|
Range | Formula | |
J3:J10 | J3 | =SUM(E3:I3) |
Sorry if the example is not exactly perfect. I was trying to recreate this sample from memory.
1st I need to pull the total from each "Amount Due" Column in each workbook if the name on sheet(she1) is found on the respective individual billing individual sheet.
Once That is done I want to paste the total from column J back into column G sheets2 and Sheet3 as these sheets contain a column "credit column"
P.S I have this code that helps me get what I need with the real data at work, which uses sumifs to retrieve the data but it feels like this can be improved on for efficiency or for readability. Sorry I could not provide an example of the real data. Still Wondering if you guys had any tips.
VBA Code:
Sub Dictionary_Variables()o
Dim dict As Scripting.Dictionary
Dim key As Variant
Dim svc As String, shtName As String
Dim Starttime As Single
Starttime = timer
Set dict = New Dictionary
With dict
.Add "xxxx.xlsx", "Sales"
.Add "xxxx.xlsx", "Sales"
.Add "xxxx.xlsx", "TV"
.Add "xxxx", "TOYS"
.Add "xxxx.xlsx", "Shoes"
End With
Dim sh1 As Worksheet, sht As Worksheet
Dim a As Variant, b As Variant, c As Variant, d As Variant, e As Variant
Dim i As Long, j As Long, LR As Long, LR2 As Long, LR3 As Long, SearchColumn As Long, ResultColumn As Long, OColumn As Long
Dim LookUp As Range, cName As Range
Dim PM_COL As Variant, PM_Col2 As Variant
Dim She1 As Workbook
Set She1 = Workbooks("yyyy.xlsx"): Set sh1 = She1.Sheets("Sheet1")
shtName = sh1.Range("B1").Value
Application.ScreenUpdating = False
For Each key In dict
svc = dict(key)
Set PMSheet = Workbooks(key): Set sht = PMSheet.Sheets(shtName): LR3 = sht.Range("A" & Rows.Count).End(xlUp).Row: LR = sh1.Range("A" & Rows.Count).End(xlUp).Row - 1
Set LookUp = sht.Rows(2)
ResultColumn = LookUp.Find("Amount Due", lookat:=xlWhole).Column
PM_COL = Array("Cars", "Clothing")
If svc = "Sales" Then
For i = 3 To LR
If WorksheetFunction.Sum(Range("E" & i & ":H" & i)) = 0 Then
For Each c In PM_COL
SearchColumn = sh1.Rows(2).Find(c, lookat:=xlWhole).Column
e = Mid(Cells(1, SearchColumn).Address(False, False), 1, 1)
sh1.Range(e & i).Value = WorksheetFunction.SumIfs(sht.Range(sht.Cells(3, ResultColumn), sht.Cells(LR3, ResultColumn)), sht.Range("A3:A" & LR3), sh1.Range("A" & i), sht.Range("E3:E" & LR3), sh1.Range(e & 2) & "*")
Next
End If
Next
Else
SearchColumn = sh1.Rows(2).Find(svc, lookat:=xlWhole).Column
e = Mid(Cells(1, SearchColumn).Address(False, False), 1, 1)
For i = 3 To LR
sh1.Range(e & i).Value = WorksheetFunction.SumIfs(sht.Range(sht.Cells(3, ResultColumn), sht.Cells(LR3, ResultColumn)), sht.Range("A3:A" & LR3), sh1.Range("A" & i))
Next
End If
Next key
For Each key In dict
svc = dict(key)
Set PMSheet = Workbooks(key): Set sht = PMSheet.Sheets(shtName): LR3 = sht.Range("A" & Rows.Count).End(xlUp).Row: LR = sh1.Range("A" & Rows.Count).End(xlUp).Row - 1
Set LookUp = sht.Rows(2)
If svc = "Sales" Then
OColumn = LookUp.Find("Credit", lookat:=xlWhole).Column
d = Mid(Cells(1, OColumn).Address(False, False), 1, 2)
For i = 3 To LR3
sht.Range(d & i).Value = sht.Range(d & i).Value
Next
Else
OColumn = LookUp.Find("Sales Pays", lookat:=xlWhole).Column
d = Mid(Cells(1, OColumn).Address(False, False), 1, 1) ' This might change
For i = 3 To LR3
sht.Range(d & i).Value = sht.Range(d & i).Value
Next
End If
Next key
Application.ScreenUpdating = True
Debug.Print "Total Time to Run = " & timer - Starttime & " Seconds."
End Sub