Forum Discussion
Need VBA Code
Sure, Excel office 365, Windows 11 Laptop
Here is the formula i have used in excel:
=SORT(UNIQUE(IF($A$3="",(VSTACK(UNIQUE(FILTER('Sheet 1'!D:D,('Sheet 1'!D:D<>"Batch No")*('Sheet 1'!D:D<>""))),UNIQUE(FILTER('Sheet 2'!D:D,('Sheet 2'!D:D<>"Batch No")*('Sheet 2'!D:D<>""))),UNIQUE(FILTER('Sheet 3'!D:D,('Sheet 3'!D:D<>"Batch No")*('Sheet 3'!D:D<>""))),UNIQUE(FILTER('Sheet 4'!D:D,('Sheet 4'!D:D<>"Batch No")*('Sheet 4'!D:D<>""))))),
(VSTACK(UNIQUE(FILTER('Sheet 1'!D:D,('Sheet 1'!A:A=$A$3)*('Sheet 1'!D:D<>"Batch No")*('Sheet 1'!D:D<>""))),UNIQUE(FILTER('Sheet 2'!D:D,('Sheet 2'!A:A=$A$3)*('Sheet 2'!D:D<>"Batch No")*('Sheet 2'!D:D<>""))),UNIQUE(FILTER('Sheet 3'!D:D,('Sheet 3'!A:A=$A$3)*('Sheet 3'!D:D<>"Batch No")*('Sheet 3'!D:D<>""))),UNIQUE(FILTER('Sheet 4'!D:D,('Sheet 4'!A:A=$A$3)*('Sheet 4'!D:D<>"Batch No")*('Sheet 4'!D:D<>""))))))),1)
Sub GetUniqueValues()
Dim ws6 As Worksheet
Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet, ws4 As Worksheet
Dim rng1 As Range, rng2 As Range, rng3 As Range, rng4 As Range
Dim filterValue As Variant
Dim result As Variant
Dim i As Long, j As Long
' Set the worksheet references
Set ws6 = ThisWorkbook.Worksheets("Sheet6")
Set ws1 = ThisWorkbook.Worksheets("Sheet1")
Set ws2 = ThisWorkbook.Worksheets("Sheet2")
Set ws3 = ThisWorkbook.Worksheets("Sheet3")
Set ws4 = ThisWorkbook.Worksheets("Sheet4")
' Get the filter value from Sheet6 B3
filterValue = ws6.Range("B3").Value
' Set the ranges based on the filter value
If filterValue <> "" Then
Set rng1 = ws1.Range("A:D").SpecialCells(xlCellTypeConstants)
Set rng2 = ws2.Range("A:D").SpecialCells(xlCellTypeConstants)
Set rng3 = ws3.Range("A:D").SpecialCells(xlCellTypeConstants)
Set rng4 = ws4.Range("A:D").SpecialCells(xlCellTypeConstants)
Else
Set rng1 = ws1.Range("D:D").SpecialCells(xlCellTypeConstants)
Set rng2 = ws2.Range("D:D").SpecialCells(xlCellTypeConstants)
Set rng3 = ws3.Range("D:D").SpecialCells(xlCellTypeConstants)
Set rng4 = ws4.Range("D:D").SpecialCells(xlCellTypeConstants)
End If
' Combine the ranges and get unique values
result = Application.WorksheetFunction.Unique(Application.WorksheetFunction.VStack(rng1, rng2, rng3, rng4))
' Sort the unique values
result = Application.WorksheetFunction.Sort(result, 1)
' Clear existing data in Sheet6 column A
ws6.Range("A:A").ClearContents
' Write the unique values to Sheet6 column A
For i = 1 To UBound(result, 1)
ws6.Cells(i, 1).Value = result(i, 1)
Next i
End SubThis code should work (untested) as long as you have Excel 365 or a version of Excel that supports dynamic arrays and the VStack and Unique functions. If you have an older version of Excel, you may need to use a different approach.
I hope it helps!
- yvsriravi12047Jun 15, 2023Copper Contributor
- NikolinoDEJun 15, 2023Platinum Contributor
It seems that I mistakenly provided an incorrect method name.
To achieve the desired result, you can use a Dictionary object in VBA to collect the unique values from the specified ranges. Here is an updated version of the code:
Sub GetUniqueValues() Dim ws6 As Worksheet Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet, ws4 As Worksheet Dim rng1 As Range, rng2 As Range, rng3 As Range, rng4 As Range Dim filterValue As Variant Dim resultDict As Object Dim result() As Variant Dim cell As Range Dim i As Long ' Set the worksheet references Set ws6 = ThisWorkbook.Worksheets("Sheet6") Set ws1 = ThisWorkbook.Worksheets("Sheet1") Set ws2 = ThisWorkbook.Worksheets("Sheet2") Set ws3 = ThisWorkbook.Worksheets("Sheet3") Set ws4 = ThisWorkbook.Worksheets("Sheet4") ' Get the filter value from Sheet6 B3 filterValue = ws6.Range("B3").Value ' Set the ranges based on the filter value If filterValue <> "" Then Set rng1 = ws1.Range("A:D").SpecialCells(xlCellTypeConstants) Set rng2 = ws2.Range("A:D").SpecialCells(xlCellTypeConstants) Set rng3 = ws3.Range("A:D").SpecialCells(xlCellTypeConstants) Set rng4 = ws4.Range("A:D").SpecialCells(xlCellTypeConstants) Else Set rng1 = ws1.Range("D:D").SpecialCells(xlCellTypeConstants) Set rng2 = ws2.Range("D:D").SpecialCells(xlCellTypeConstants) Set rng3 = ws3.Range("D:D").SpecialCells(xlCellTypeConstants) Set rng4 = ws4.Range("D:D").SpecialCells(xlCellTypeConstants) End If ' Create a Dictionary object to collect unique values Set resultDict = CreateObject("Scripting.Dictionary") ' Collect unique values from the ranges CollectUniqueValues rng1, resultDict CollectUniqueValues rng2, resultDict CollectUniqueValues rng3, resultDict CollectUniqueValues rng4, resultDict ' Sort the unique values result = SortDictionaryKeys(resultDict) ' Clear existing data in Sheet6 column A ws6.Range("A:A").ClearContents ' Write the unique values to Sheet6 column A For i = LBound(result) To UBound(result) ws6.Cells(i + 1, 1).Value = result(i) Next i End Sub Sub CollectUniqueValues(rng As Range, ByRef resultDict As Object) Dim cell As Range For Each cell In rng If Not resultDict.exists(cell.Value) Then resultDict.Add cell.Value, cell.Value End If Next cell End Sub Function SortDictionaryKeys(dict As Object) As Variant Dim keys() As Variant Dim i As Long ' Get the keys from the Dictionary keys = dict.keys ' Sort the keys QuickSort keys, LBound(keys), UBound(keys) SortDictionaryKeys = keys End Function Sub QuickSort(arr As Variant, low As Long, high As Long) Dim pivot As Variant Dim i As Long, j As Long Dim temp As Variant If low < high Then pivot = arr((low + high) \ 2) i = low - 1 j = high + 1 Do Do i = i + 1 Loop Until arr(i) >= pivot Do j = j - 1 Loop Until arr(j) <= pivot If i < j Then temp = arr(i) arr(i) = arr(j) arr(j) = temp End If Loop While i < j If low < j Then QuickSort arr, low, j If i < high Then QuickSort arr, i, high End SubThe QuickSort subroutine is used to sort the array of keys obtained from the dictionary in ascending order. It utilizes the quicksort algorithm for efficient sorting.
With this complete code, you should be able (not tested) to run the GetUniqueValues macro to populate the unique values in Sheet6 column A based on the specified conditions.
- yvsriravi12047Jun 15, 2023Copper Contributor
For your convenience i am giving some sample data here
Sheet1:
Product & Batches Month of
Production
DateDate Product Name Batch No May 01/05/2023 A1 APTELNS0319 May 01/05/2023 A2 APTELNS0327 May 01/05/2023 A3 APTELNS0330 May 01/05/2023 A4 APTELNS0332 May 02/05/2023 A5 APTELNS0332 May 02/05/2023 A6 APTELNS0334 May 02/05/2023 A7 APTELNS0334 May 02/05/2023 A8 APTELNS0334 May 04/05/2023 A9 APTELDNS0145 May 04/05/2023 A10 APTELDNS0146 May 05/05/2023 A11 APTELDNS0147 May 05/05/2023 A12 APTELDNS0148 May 05/05/2023 A13 APTELDNS0149 May 05/05/2023 A14 APTELDNS0150 May 06/05/2023 A15 APTELDNS0151 May 06/05/2023 A16 APTELDNS0152 May 06/05/2023 A17 APTELDNS0153 May 07/05/2023 A18 APTELDNS0154 May 07/05/2023 A19 APTELDNS0155 May 08/05/2023 A20 APTELDNS0156 Sheet 2:
Product & Batches Month of
Production
DateDate Product Name Batch No May 01/05/2023 B1 APTELNS0328 May 01/05/2023 B2 APTELNS0329 May 01/05/2023 B3 APTELNS0331 May 01/05/2023 B4 APTELNS0333 May 02/05/2023 B5 APTELNS0335 May 02/05/2023 B6 APTELNS0337 May 03/05/2023 B7 APTELRL0142 May 03/05/2023 B8 APTELRL0143 May 03/05/2023 B9 APTELRL0144 May 04/05/2023 B10 APTELRL0145 May 05/05/2023 B11 APTELNS0339 May 05/05/2023 B12 APTELRL0146 May 05/05/2023 B13 APTELRL0147 May 06/05/2023 B14 APTELRL0148 May 06/05/2023 B15 APTELRL0149 May 07/05/2023 B16 APTELNS0340 May 07/05/2023 B17 APTELNS0341 May 07/05/2023 B18 APTELNS0342 Sheet 3:
Product & Batches Month of
Production
DateDate Product Name Batch No May 01/05/2023 C1 APTELNS0321 May 01/05/2023 C2 APTELNS0322 May 02/05/2023 C3 APTELNS0323 May 02/05/2023 C4 APTELNS0324 May 03/05/2023 C5 APTELNS0325 May 04/05/2023 C6 APTELNS0326 May 05/05/2023 C7 APTELNS0344 May 05/05/2023 C8 APTELNS0345 May 06/05/2023 C9 APTELNS0346 May 06/05/2023 C10 APTELNS0347 May 06/05/2023 C11 APTELNS0348 May 07/05/2023 C12 APTELNS0350 May 07/05/2023 C13 APTELNS0351 May 08/05/2023 C14 APTELNS0352 May 08/05/2023 C15 APTELNS0353 May 09/05/2023 C16 APTELNS0354 May 10/05/2023 C17 APTELNS0358 May 10/05/2023 C18 APTELNS0359 May 11/05/2023 C19 APTELNS0360 May 11/05/2023 C20 APTELNS0361 May 12/05/2023 C21 APTELNS0362 May 12/05/2023 C22 APTELNS0363 May 13/05/2023 C23 APTELNS0364 May 13/05/2023 C24 APTELD250012 Sheet 4:
Product & Batches Month of
Production
DateDate Product Name Batch No May 28/05/2023 D1 APTELNS0001D May 31/05/2023 D2 APTELNS0002D May 31/05/2023 D3 APTELNS0003D Jun 02/06/2023 D4 APTELNS0004D Jun 02/06/2023 D5 APTELNS0005D Jun 03/06/2023 D6 APTELNS0006D Jun 07/06/2023 D7 APTELNS0007D Jun 08/06/2023 D8 APTELNS0007D Jun 09/06/2023 D9 APTELNS0007D Jun 09/06/2023 D10 APTELNS0008D Jun 09/06/2023 D11 APTELNS0009D Jun 10/06/2023 D12 APTELNS0012D Jun 10/06/2023 D13 APTELNS0013D Jun 11/06/2023 D14 APTELNS0013D Jun 12/06/2023 D15 APTELNS0015D Jun 12/06/2023 D16 APTELNS0016D Jun 12/06/2023 D17 APTELNS0017D Jun 12/06/2023 D18 APTELNS0018D Result should display in sheet 6
Get unique list from C4 cell to downwords
i have dropdown list for month in A2
Condition 1: If A2 is blank i need complete unique list from all 4 sheets
Condition 2: If month selected in A2 (Ex: May) i need unique list of Batch No. i.e. Column D if the month is May in column A