I have two worksheets: one named (raw_data) and the other one named (stock_check). The file has around 200k~ lines in the sheet named raw_data.
The raw_data looks like the following (only added the columns that matter for this case).
On my first sheet raw_data I have roughly 50+ columns, out of which I am trying to copy two columns based on the header ID to the sheet stock_check and remove the duplicates. For this I am using the following piece of code:
Sub CopyMultipleColumns()
Dim wb As Workbook
Dim newSht As Worksheet, Hdrs As Variant, i As Long, EdrisRange As Range
Hdrs = Array("Sales_ID", "Category", "ShopTo_ID")
Set wb = ThisWorkbook
Set newSht = ThisWorkbook.Worksheets("stock_check")
For i = LBound(Hdrs) To UBound(Hdrs)
Set EdrisRange = FindHeaderInWorkbook(wb, CStr(Hdrs(i)), newSht)
If Not EdrisRange Is Nothing Then
Application.Intersect(EdrisRange.EntireColumn, EdrisRange.Parent.UsedRange).Copy _
newSht.Cells(1, i + 1).PasteSpecial xlPasteValues
End If
Next i
Application.CutCopyMode = False
End Sub
Calling the following function to find the specific headers in the sheet raw_data
Function FindHeaderInWorkbook(wb As Workbook, HeaderText As String, excludeSheet As
Worksheet)
Dim sht As Worksheet, rng As Range
For Each sht In wb.Worksheets
If sht.Name <> excludeSheet.Name Then
Set rng = sht.Rows(1).Find(what:=HeaderText, lookat:=xlWhole)
If Not rng Is Nothing Then Exit For
End If
Next sht
Set FindHeaderInWorkbook = rng
End Function
Out of which I am then removing the duplicates using the following the RemoveDuplicates function in VBA.
Sub RemoveDuplicates()
With ThisWorkbook.Worksheets("stock_check")
.UsedRange.RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlYes
End With
End Sub
Up to here it all works fine, and it generates the following output in Excel (one unique line in Excel):
This is the part where I am stuck/don't know how to continue. What I want to do next is create an available_size column (next to ShopTo_ID) that adds all the available sizes from the raw_data for that particular sales_ID together and separates them by a ",". See below of my expected outcome:
As a next step, I then want to check if the core_size is available, for which I have the following ruleset:
- if available_size includes sizes 8 and 9, then "YES", otherwise "NO"
Which should generate the following output:
As a last step I then want to add a column named stock_depth, based on the number of "," that will check for category X/Y if there is enough stock depth based on the ruleset:
- if available_size is equal or more than 4 and category is X then "YES", otherwise "NO"
- if available_size is equal or more than 2 and category is Y then "YES", otherwise "NO"
Which should generate the following:
Could anyone be so kind to provide me a few pointers of how to continue? Thanks a lot!




