VBA to concatenate three columns and display in a single listbox or Combobox

aaleem

Board Regular
Joined
Sep 26, 2014
Messages
56
Office Version
  1. 2016
Hi,

Im trying to get the unique dates from a range, sorted them in ascending order converting dates into the financial year week (which is based on July to Jun year). Once this is done the result i would like to display in a listbox by concatenating the week number + Week Start Date + Week End date

i able to get to the last bit however the data display in list box is not in the above format.

any help and guidance will be much appreciated. There are two codes one for unique dates and the other one for listing the values in combobox.

Worksheet prompt_example.xlsm
ABCDEFGHIJKLMNOPQ
1Start Time (DD/MM/YYYY)Start Time (DD/MM/YYYY)Week start DateWeek End dateWeek Number
224-05-2124-05-21Week start DateStart Date24-05-2123-05-2129-05-2147
329-05-2129-05-2130-05-2105-06-2148
430-05-2130-05-2106-06-2112-06-2149
530-05-2131-05-2113-06-2119-06-2150
630-05-2101-06-2120-06-2126-06-2151
731-05-2102-06-2127-06-2103-07-2152
801-06-2103-06-2104-07-2110-07-211
901-06-2104-06-2111-07-2117-07-212
1001-06-2105-06-2118-07-2124-07-213
1102-06-2106-06-2125-07-2131-07-214
1203-06-2107-06-2101-08-2107-08-215
1304-06-2108-06-2108-08-2114-08-216
1431-05-2109-06-2115-08-2121-08-217
1501-06-2110-06-2122-08-2128-08-218The desired result should be
1602-06-2111-06-2129-08-2104-09-219
1703-06-2112-06-2105-09-2111-09-2110WK-47-23-05-21 - 29-05-21
1804-06-2113-06-2112-09-2118-09-2111
1905-06-2114-06-2119-09-2125-09-2112
2006-06-2115-06-2126-09-2102-10-2113
2107-06-2116-06-2103-10-2109-10-2114
2208-06-2117-06-2110-10-2116-10-2115
2309-06-2118-06-2117-10-2123-10-2116
2410-06-2119-06-2124-10-2130-10-2117
2511-06-2120-06-2131-10-2106-11-2118
2612-06-2121-06-2107-11-2113-11-2119
2713-06-2122-06-2114-11-2120-11-2120
2814-06-2123-06-2121-11-2127-11-2121
2915-06-2124-06-2128-11-2104-12-2122
3016-06-2125-06-2105-12-2111-12-2123
3117-06-2126-06-2112-12-2118-12-2124
3218-06-2127-06-2119-12-2125-12-2125
3319-06-2128-06-2126-12-2101-01-2226
3420-06-2129-06-2102-01-2208-01-2227
Sheet1
Cell Formulas
RangeFormula
I2I2=F2
J2J2=MIN(F:F)-WEEKDAY(I2,17)+1
K2:K34K2=IF(J2="","",J2+7-WEEKDAY(J2,17))
L2:L34L2=WEEKNUM(J2-184)
J3:J34J3=IF(MAX(F:F)>K2,K2+1,"")
A9:A10,A5:A6A5=A4
A16:A34,A11:A13,A7:A8A7=A6+1



VBA Code:
Sub Uniques()
    Dim oColl       As New Collection
    
    Dim nr          As Variant
    Dim vArr1       As Long
    Dim vItem       As Variant
    Dim j           As Long
    
    Application.ScreenUpdating = False
    
    nr = Range("A" & Rows.Count).End(xlUp).Row
    vArr = Range("A1:A" & (nr))
    
    On Error Resume Next
    'For j = LBound(vArr) To UBound(vArr1)
    For j = 1 To nr
        
        oColl.Add vArr(j, 1), CStr(vArr(j, 1))
    Next j
    On Error GoTo 0
    
    For Each vItem In oColl
        X = X + 1
        Range("F" & X).Value = vItem
        Columns("F:F").Sort key1:=Range("F2"), _
                            order1:=xlAscending, Header:=xlYes
        'Debug.Print vItem
    Next vItem
    
    Call ListBox1
    Application.ScreenUpdating = True
End Sub

Sub ListBox1()
    
    Dim UnionRange          As Range
    Dim cell                As Range
    Dim lr As Variant

    Application.ScreenUpdating = False
    lr = Columns("j").Find("*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows, LookIn:=xlValues).Row
    ' use Union to merge as many named ranges you need
    'Set UnionRange = Union(Range("Wk_Start"), Range("Wk_End"), Range("Wk_Num"))
    Set UnionRange = Union(Range("J1:J" & lr), Range("k1:K" & lr), Range("L1:L" & lr))
    
    Me.ComboBox1.Clear
    
    For Each cell In UnionRange
        ComboBox1.AddItem cell.Value
    Next cell
    ComboBox1.ListIndex = 0
    
    Range("g2").Value = Me.ComboBox1.Text
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top