can you help me in modifay this code for combining data in listbox on userform and calculate values amongst them?

saftawy1

Board Regular
Joined
Oct 12, 2021
Messages
65
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
i found this code for master

DanteAmor

and i try modifay it to suitable for me but its not appear all item in column 8 when the data combining what is the wrong in the code
sample data.xlsb
T
13
SH1

VBA Code:
Dim lr As Long, sht As Worksheet


Private Sub UserForm_Initialize()
  Dim i As Long

      lr = lr + Sheets("SH1").Range("A" & Rows.Count).End(3).Row - 1
    Set sht = Sheets("Temp")
    sht.Range("A1:Q1").Value = Sheets("SH1").Range("A1:Q1").Value

    ListBox1.ColumnHeads = True
    OptionButton1.Value = True
End Sub

Private Sub CommandButton1_Click()
  Dim sh As Worksheet
  Dim dic As Object
  Dim i As Long, j As Long, k As Long, m As Long
  Dim a() As Variant, b As Variant
 
  Set dic = CreateObject("Scripting.dictionary")
  sht.Range("A1").Value = "ID"
  sht.Range("A:A").NumberFormat = "General"
'  sht.Range("I:J").NumberFormat = "#,##0 ""RYAL"""
      If OptionButton1 Then
        Set sh = Sheets("SH1")
        ReDim b(1 To sh.Range("A" & Rows.Count).End(3).Row - 1, 1 To 17)
        Call fillarray(dic, sh, b)
      Else
        b = Sheets("SH1").Range("A2:Q" & Sheets("SH1").Range("A" & Rows.Count).End(3).Row).Value
        sht.Range("A1").Value = "DATE"
      End If
 
  sht.Range("A2:Q" & Rows.Count).ClearContents
  sht.Range("A2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  ListBox1.RowSource = sht.Name & "!" & "A2:Q" & sht.Range("A" & Rows.Count).End(3).Row
End Sub

Sub fillarray(dic, sh, b)
  Dim a() As Variant
  Dim j As Long, k As Long, m As Long, n As Long

  Erase a
  a = sh.Range("A2:Q" & sh.Range("A" & Rows.Count).End(3).Row).Value
  For j = 1 To UBound(a, 1)
    If Not dic.exists(a(j, 8)) Then
      m = m + 1
      n = 1
    Else
      m = Split(dic(a(j, 8)), "|")(0)
      n = Split(dic(a(j, 8)), "|")(1) + 1
    End If
    dic(a(j, 8)) = m & "|" & n
    For k = 1 To UBound(a, 2)
      Select Case k
        Case 1
          b(m, k) = m
        Case 2 To 8
          b(m, k) = a(j, k)
        Case 9
          b(m, k) = b(m, k) + a(j, k)
        Case 10
          b(m, 10) = b(m, 10) + a(j, k)
          b(m, k) = b(m, 10) / n
        Case 11
          b(m, k) = b(m, k) + a(j, k)
        Case 12
          b(m, 12) = b(m, 12) + a(j, k)
          b(m, k) = b(m, 12) / n
        Case 13 To 14
          b(m, k) = b(m, k) + a(j, k)
        Case 15
          b(m, 15) = b(m, 15) + a(j, k)
          b(m, k) = b(m, 15) / n
        Case 16
          b(m, 16) = b(m, 16) + a(j, k)
          b(m, k) = b(m, 16) / n
        Case 17
          b(m, k) = a(j, k)
      End Select
    Next
  Next
End Sub
Private Sub CommandButton2_Click()

End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
A reminder:

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
A reminder:

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
I will take this into account next time, knowing that I did not ask for help on the other site until 10 days after posting here without any response
 
Upvote 0

Forum statistics

Threads
1,224,734
Messages
6,180,632
Members
452,991
Latest member
JM_000888

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