Copy data from listbox on userform to listbox and add header for each range

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
651
Office Version
  1. 2019
Hello
I have this code to copy to the bottom when run the form to sheet
VBA Code:
Private Sub CommandButton2_Click()
Dim rngNext As Range
Dim i As Long
Dim col As Long

    Set rngNext = Worksheets("NAMES").Range("A" & Rows.Count).End(xlUp).Offset(1)
   
    For i = 1 To ListBox1.ListCount - 1
   
            For col = 0 To ListBox1.ColumnCount - 1
                rngNext.Offset(, col).Value = ListBox1.List(i, col)
            Next col
           
            Set rngNext = rngNext.Offset(1)
    Next i
End Sub
and the formatting in sheet will be like this
DECREASE.xlsm
ABCDEFG
1NAMES
2
3ITEMDATEINV.NOCASEDEBITCREDITBALANCE
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
NAMES


when try copy data from form to sheet I want copy name from combobox1 and put under header in column D so the result will be


ss1.JPG



should be

DECREASE.xlsm
ABCDEFG
1NAMES
2CCF-1000
3ITEMDATEINV.NOCASEDEBITCREDITBALANCE
4130/06/2023 --500-500
5215/07/2023RVCH2000030,000.00 -29,500.00
6315/07/2023RVCH2000210,000.00 -39,500.00
7415/09/2023BSTR_23448OUTSANDING -2,300.0037,200.00
8515/09/2023BSTR_23449OUTSANDING -1,920.0035,280.00
9615/09/2023BSTR_23450PAID50,400.00 -85,680.00
10715/09/2023BSJ_23444OUTSANDING1,720.00 -87,400.00
11815/09/2023BSJ_23446PAID -4,900.0082,500.00
12915/09/2023VSTR_23444PAID -50082,000.00
131015/09/2023VSTR_23446OUTSANDING3,600.00 -85,600.00
141115/09/2023RSS_23222OUTSANDING -86084,740.00
151215/09/2023VCH20005 -15,000.0069,740.00
161316/09/2023VSTR_23449PAID -3,760.0065,980.00
171416/09/2023RSS_23224PAID2,950.00 -68,930.00
18SUM98,670.0028,740.0069,930.00
NAMES


and if I select another name then should copy the same formatting as in original sheet based how many rows need it and leave two empty rows as break .
example

ss2.JPG


should be
DECREASE.xlsm
ABCDEFG
1NAMES
2CCF-1000
3ITEMDATEINV.NOCASEDEBITCREDITBALANCE
4130/06/2023 --500-500
5215/07/2023RVCH2000030,000.00 -29,500.00
6315/07/2023RVCH2000210,000.00 -39,500.00
7415/09/2023BSTR_23448OUTSANDING -2,300.0037,200.00
8515/09/2023BSTR_23449OUTSANDING -1,920.0035,280.00
9615/09/2023BSTR_23450PAID50,400.00 -85,680.00
10715/09/2023BSJ_23444OUTSANDING1,720.00 -87,400.00
11815/09/2023BSJ_23446PAID -4,900.0082,500.00
12915/09/2023VSTR_23444PAID -50082,000.00
131015/09/2023VSTR_23446OUTSANDING3,600.00 -85,600.00
141115/09/2023RSS_23222OUTSANDING -86084,740.00
151215/09/2023VCH20005 -15,000.0069,740.00
161316/09/2023VSTR_23449PAID -3,760.0065,980.00
171416/09/2023RSS_23224PAID2,950.00 -68,930.00
18SUM98,670.0028,740.0069,930.00
19
20
21NAMES
22CCF-1001
23ITEMDATEINV.NOCASEDEBITCREDITBALANCE
24130/06/20232,000.00 -2,000.00
25215/07/2023RVCH2000125,000.00 -27,000.00
26315/09/2023VSTR_23445OUTSANDING1,000.00 -28,000.00
27416/09/2023BSTR_23452PAID7,200.00 -35,200.00
28SUM35,200.00 -35,200.00
29
NAMES

I hope to find solve for this subject.
EDITED: subject should copy from listbox to sheet, not from listbox to listbox as show in topic,sorry!
thanks
 
Excellent!(y)
sorry sometimes my subjects is complicated and poor details.
I'm always glad to provide me the great solution.
thank you for everything.:)
 
Upvote 1

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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