show data on userform vertically based on combobox for multiple ranges

Omran Y

Board Regular
Joined
Jul 17, 2023
Messages
60
Office Version
  1. 2013
Platform
  1. Windows
Hello ,

I have multiple ranges for each name in EMPLOYEE sheet (about 10000rows for multiple ranges and multiple names) .
in EMPLOYEE sheet contains specific cells (NAME,FIRST BALANCE,SALARY,NET AMOUNT) should convert to headers consecutively when run the form when select sheet name from combobox1 and sum the columns (FIRST BALANCE,SALARY,NET AMOUNT) by show TOTAL row in lastrow in listbox without forget adding ITEM column in first column to autonumbering 1,2,....
OMRAN.xlsm
ABCD
1START DATELAST DATENAMEFIRST BALANCE
201/01/202331/01/2023OMAR ALI OMAR-20000
3SALARY3000
4NET AMOUNT-17000
5
6
7START DATELAST DATENAMEFIRST BALANCE
801/01/202331/01/2023AHMED OMAR ALI-2000
9SALARY3000
10NET AMOUNT1000
11
12
13START DATELAST DATENAMEFIRST BALANCE
1401/01/202331/01/2023ALI MAHMUD OMAR1000
15SALARY2500
16NET AMOUNT3500
EMPLOYEE



result

AS1.PNG

thanks
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Based on your sample data and description, try the following code:

VBA Code:
Private Sub ComboBox1_Change()

'Clear the previous listbox results and add the header row
    With Me.ListBox1
        .Clear
        .AddItem
        .List(0, 0) = "ITEM"
        .List(0, 1) = "NAME"
        .List(0, 2) = "FIRST BALANCE"
        .List(0, 3) = "SALARY"
        .List(0, 4) = "NET AMOUNT"
    End With

'Set the source worksheet, if it exists
    On Error Resume Next
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets(Me.ComboBox1.Text)
    If Err.Number <> 0 Then Exit Sub
    On Error GoTo ErrorHandler

'Load the source data into an array
    Dim lastRow As Long, arr As Variant
    lastRow = ws.Cells(ws.Rows.Count, 3).End(xlUp).Row
    arr = ws.Cells(1, 3).Resize(lastRow, 2).Value

'Loop thru the array and populate the listbox with each new item found
    Dim i As Long, lstId As Long, tot1 As Double, tot2 As Double, tot3 As Double
    lstId = 1
    For i = 1 To lastRow
        If arr(i, 1) = "NAME" Then
            With Me.ListBox1
                .AddItem
                .List(lstId, 0) = lstId
                .List(lstId, 1) = Format(arr(i + 1, 1), "#,##0.00")
                .List(lstId, 2) = Format(arr(i + 1, 2), "#,##0.00")
                .List(lstId, 3) = Format(arr(i + 2, 2), "#,##0.00")
                .List(lstId, 4) = Format(arr(i + 3, 2), "#,##0.00")
            End With
            tot1 = tot1 + arr(i + 1, 2)
            tot2 = tot2 + arr(i + 2, 2)
            tot3 = tot3 + arr(i + 3, 2)
            lstId = lstId + 1
        End If
    Next i

'Add the total row to the listbox
    With Me.ListBox1
        .AddItem
        .List(lstId, 0) = "TOTAL"
        .List(lstId, 2) = Format(tot1, "#,##0.00")
        .List(lstId, 3) = Format(tot2, "#,##0.00")
        .List(lstId, 4) = Format(tot3, "#,##0.00")
    End With

Exit Sub

ErrorHandler:
    If Err.Number = 94 Then
    ' Invalid use of Null
        Err.Clear
        Resume Next
    Else
        MsgBox Err.Description, vbExclamation, "Runtime Error: " & Err.Number
    End If

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,882
Messages
6,175,164
Members
452,615
Latest member
bogeys2birdies

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