Exclude Data before TOTAL row for each separated range when show on userform

Abdo

Board Regular
Joined
May 16, 2022
Messages
201
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
I got code from this forum to show data on userform for specific sheet , but the problem will contain blank rows among separated ranges and I just want populating TOTAL row for each range for each client and exclude data before TOTAL row .
the separated ranges could be 20000 ranges.
here is data in sheet
DIVIDED1.xlsm
ABCDEFG
1DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCE
201/01/2022-ABDEND1OPENNING20000-20000
304/01/2022PA-B3ABDEND1PA20000-40000
405/01/2022SA-B35ABDEND1SA-100039000
5TOTAL---40000100039000
6
7
8DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCE
914/01/2022PA-B352ABDEND10PA140-140
1014/01/2022PA-B352ABDEND10PA100240-
11TOTAL---240240-
12
13
14DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCE
1514/04/2022PA-B442ABDEND100PA1040-1040
16TOTAL---1040-1040
17
18
19DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCE
2030/09/2024PA-B1342ABDEND1000PA1004010040
2130/09/2024SA-B1342ABDEND1000SA4010000
2201/10/2024SA-B1343ABDEND1000SA10009000
23TOTAL---1004010409000
splitting


on userform should exclude data before TOTAL row for each separated range . just show client name and TOTAL row
1.JPG

thanks
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Here is some form code that does what you need.

VBA Code:
Private Sub UserForm_Initialize()
    Dim lastRow As Long, i As Long, count As Long
    count = 0
    lastRow = Worksheets("sheet1").Cells.Find("*", _
        SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For i = 1 To lastRow
        If Cells(i, 1).Value = "TOTAL" Then
            count = count + 1
            ListBox1.AddItem count
            ListBox1.List(ListBox1.ListCount - 1, 1) = Cells(i - 1, 3).Value
            ListBox1.List(ListBox1.ListCount - 1, 2) = Cells(i, 5).Value
            ListBox1.List(ListBox1.ListCount - 1, 3) = Cells(i, 6).Value
            ListBox1.List(ListBox1.ListCount - 1, 4) = Cells(i, 7).Value
        End If
    Next i
End Sub

It produced this output when I ran it on your data.
1718615776711.png
 
Upvote 0
Hi
here is the code
VBA Code:
Option Explicit
Dim a As Variant


Private Sub UserForm_Initialize()
a = Sheets("splitting").Range("A2:G" & Sheets("splitting").Range("G" & Rows.count).End(3).Row).Value

Dim lindex&
  Dim i As Long, j As Long, k As Long
 
  ListBox1.Clear
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
  For i = 1 To UBound(a)
    
      k = k + 1
      For j = 1 To 7
        If j = 1 Then  
          b(k, j) = Format(a(i, j), "dd/mm/yyyy")
          
        Else
          b(k, j) = a(i, j)

End If

      Next
    
  Next
  If k > 0 Then ListBox1.List = b
  With ListBox1
  
  .ColumnWidths = "70;100;90;150;100;120;120"
For i = 0 To .ListCount - 1
            .List(i, 4) = Format(.List(i, 4), "#,##0.00")
            .List(i, 5) = Format(.List(i, 5), "#,##0.00")
            .List(i, 6) = Format(.List(i, 6), "#,##0.00")
        Next i

  End With
End Sub
 
Upvote 0
Try this.
VBA Code:
Private Sub UserForm_Initialize()
    Dim a As Variant
    Dim i As Long, k As Long
    a = Sheets("splitting").Range("A2:G" & Sheets("splitting").Range("G" & Rows.count).End(3).Row).Value
    ListBox1.Clear
    ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
    For i = 1 To UBound(a)
        If a(i, 1) = "TOTAL" Then
            k = k + 1
            b(k, 1) = k
            b(k, 2) = a(i - 1, 3)
            b(k, 3) = a(i, 5)
            b(k, 4) = a(i, 6)
            b(k, 5) = a(i, 7)
        End If
    Next
    If k > 0 Then
        ListBox1.ColumnCount = 5
        ListBox1.List = b
        With ListBox1
            .ColumnWidths = "70;100;120;120"
            For i = 0 To .ListCount - 1
                .List(i, 3) = Format(.List(i, 3), "#,##0.00")
                .List(i, 4) = Format(.List(i, 4), "#,##0.00")
                .List(i, 5) = Format(.List(i, 5), "#,##0.00")
            Next i
        End With
    End If
End Sub
 
Upvote 0
Code:
Private Sub UserForm_Initialize()
    Dim a, cols, i&, ii&, n&
    With Sheets("splitting")
        a = Intersect(.UsedRange, .Columns("a:g"))
    End With
    cols = Array(3, 5, 6, 7)
    ReDim b(1 To UBound(cols) + 2, 1 To UBound(a, 1))
    For i = 1 To UBound(a, 1)
        If a(i, 1) = "DATE" Then
            If n = 0 Then
                n = n + 1
                b(1, n) = "ITEM"
                For ii = 0 To UBound(cols)
                    b(ii + 2, n) = a(i, cols(ii))
                Next
            End If
        ElseIf a(i, 1) = "TOTAL" Then
            n = n + 1
            b(1, n) = n - 1
            For ii = 0 To UBound(cols)
                b(ii + 2, n) = a(i - IIf(cols(ii) = 3, 1, 0), cols(ii))
            Next
        End If
    Next
    ReDim Preserve b(1 To UBound(b, 1), 1 To n)
    With Me.ListBox1
        .ColumnCount = UBound(b, 1)
        .Column = b
    End With
End Sub
 
Upvote 0
Solution
@Fuji
awesome!
your code is really fast than original code, I like it.:)
could you make format numbers in column 3,4,5 like this "#,##0.00" ,please?
I tried as in original code but shows error !:sick:
 
Upvote 0
Change
Code:
                b(ii + 2, n) = a(i - IIf(cols(ii) = 3, 1, 0), cols(ii))
to
Code:
                b(ii + 2, n) = Format(a(i - IIf(cols(ii) = 3, 1, 0), cols(ii)), "#,0.00;-#,0.00;0")
 
Upvote 0
@Fuji
I was posting new thread about slowness in original code but you gave me the solution by your code.;)
much appreciated for your help.:)
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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