SimpleMan618
New Member
- Joined
- Sep 29, 2022
- Messages
- 9
- Office Version
- 365
- Platform
- Windows
I have a complex workbook that has many users active in it. I need help with troubleshooting a VBA, however I have never used Dictionary before, so it's been a 5-day nightmare. (Maybe there is another way to solve my problem, but I am still learning VBA and couldn't find a good answer in my searches)
My problem is that the admin will add new "sports" to the Table page. The problem with that is many of the other reference sheets that have the sports listed are manually copied onto their own table. I tried to automate that but with the duplicates on the Table sheet and the users' changing views or sorting, it was messing with the manual entries that they were entering. I believe the solution is to compare the two tables I have (Both on Data sheet which came from the Table and Calendar sheets) and add a row to the tables on the 2023 School Year Updates and Calendar for each unique value that was found.
Here is a mockup of the workbook with mock data and sheet names
I have four sheets
Bottom line, I need a way to update the tables on Calendar and 2023 School Year Updates. Those tables are manually updated because users were changing views and sorting the table which caused misalignment of the statuses and notes among other things. If there is a better way to solve my problem, please let me know. As a reminder, the Mock workbook is just a fraction of the actual workbook. Thank you in advance.
Here is the VBA I was using
My problem is that the admin will add new "sports" to the Table page. The problem with that is many of the other reference sheets that have the sports listed are manually copied onto their own table. I tried to automate that but with the duplicates on the Table sheet and the users' changing views or sorting, it was messing with the manual entries that they were entering. I believe the solution is to compare the two tables I have (Both on Data sheet which came from the Table and Calendar sheets) and add a row to the tables on the 2023 School Year Updates and Calendar for each unique value that was found.
Here is a mockup of the workbook with mock data and sheet names
I have four sheets
- Calendar - Which shows a modified Gnatt view. This has the sports as manual.
- 2023 School Year Updates - Which is where users will update items with Status Updates and notes
- Table - Which is where the Master Table lives. The Sport will show up multiple times in this sheet and this is where the Admin will add a new sport
- Data - This sheet is veryhidden and only used to populate other areas of the workbook but I was using it based on the VBA.
Bottom line, I need a way to update the tables on Calendar and 2023 School Year Updates. Those tables are manually updated because users were changing views and sorting the table which caused misalignment of the statuses and notes among other things. If there is a better way to solve my problem, please let me know. As a reminder, the Mock workbook is just a fraction of the actual workbook. Thank you in advance.
Here is the VBA I was using
VBA Code:
Sub UpdateWorkstreams()
Dim Dict As Object 'Scripting.Dictionary
Dim Where As Range, This As Range
Dim Item, Items, Counts, Result
Dim i As Long, j As Long
Dim Table1 As ListObject
Dim Table2 As ListObject
Dim AddedRow1 As ListRow
Dim AddedRow2 As ListRow
Dim rngSrc As Range
Set Table1 = Sheets("Calendar").ListObjects("Calendar")
Set Table2 = Sheets("2023 School Year Updates").ListObjects("SchYr2023")
Set rngSrc = Sheets("Data").Range("G2:G31")
rngSrc.ClearContents
'Step 1: Collect all items
'Create a dictionary to collect unique items
Set Dict = CreateObject("Scripting.Dictionary")
'Ignore spelling
Dict.CompareMode = vbTextCompare
'Refer to the used cells in column A in Sheet1
With Sheets("Data")
Set Where = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
End With
'Collect the items
GoSub CollectItems
'Refer to the used cells in column C in Sheet2
With Sheets("Data")
Set Where = .Range("E2", .Range("E" & Rows.Count).End(xlUp))
End With
'Collect the items
GoSub CollectItems
'Step 2: Sort out the duplicates
'Get all items and counts
Items = Dict.Keys
Counts = Dict.Items
'Count all items which occurs only one time
j = 0
For i = 0 To UBound(Counts)
If Counts(i) = 1 Then j = j + 1
Next
'Step 3: Output
'Create an array for the output
ReDim Result(1 To j, 1 To 1)
'Fill in the items
j = 0
For i = 0 To UBound(Counts)
If Counts(i) = 1 Then
j = j + 1
Result(j, 1) = Items(i)
End If
Next
'Flush into Sheet3
With Sheets("Data")
.Range("G2").Resize(UBound(Result)).Value = Result
End With
Set AddedRow1 = Table1.ListRows.Add()
With AddedRow1.Range(1) = Result
End With
'Done
Exit Sub
CollectItems:
'Each cell
For Each This In Where
'The compare key is the value
Item = This.Value
'Already found?
If Not Dict.Exists(Item) Then
'No, add to the dictionary
Dict.Add Item, 1
Else
'Yes, increase the number of occurences
Dict(Item) = Dict(Item) + 1
End If
Next
Return
End Sub