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

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
612
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
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Replace your commandbutton2 code to:

VBA Code:
Private Sub CommandButton2_Click()
  Dim lr As Long
  
  If ComboBox1.ListIndex = -1 Then
    MsgBox "Select Name"
    Exit Sub
  End If
  
  With Sheets("NAMES")
    If .Range("D2").Value = "" Then
      lr = 3
    Else
      lr = .Range("A" & Rows.Count).End(3).Row + 5
      .Range("A1:G3").Copy .Range("A" & lr - 2)
    End If
    
    'copy from combobox to sheet
    .Range("D" & lr - 1).Value = ComboBox1.Value
    'copy from listbox to sheet
    .Range("A" & lr).Resize(ListBox1.ListCount, ListBox1.ColumnCount).Value = ListBox1.List
    
    'Format cells
    .Range("B:B").NumberFormat = "dd/mm/yyyy"
    .Range("E:G").NumberFormat = "#,##0.00;-#,##0.00;-"
    .Range("A" & lr + 1, .Range("G" & Rows.Count).End(3)).Borders.LineStyle = xlContinuous
  End With
End Sub
 
Upvote 0
Hi Dante, again
the code is perfect .(y)
I realized to forget this, sorry again for missed this details !🙏🙏
as you know every time the code copy to the bottom , but if I repeat copy for the same name and it's already existed I would replace data without repeat copy to the bottom.
sorry for I post missed details in this days!😣
 
Upvote 0
the code is perfect .(y)
😇


Try:
VBA Code:
Private Sub CommandButton2_Click()
  Dim lr As Long
  Dim f As Range
  
  If ComboBox1.ListIndex = -1 Then
    MsgBox "Select Name"
    Exit Sub
  End If
  
  With Sheets("NAMES")
    Set f = .Range("D:D").Find(ComboBox1.Value, , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      MsgBox "The name already exists"
      Exit Sub
    End If

    If .Range("D2").Value = "" Then
      lr = 3
    Else
      lr = .Range("A" & Rows.Count).End(3).Row + 5
      .Range("A1:G3").Copy .Range("A" & lr - 2)
    End If
    
    .Range("D" & lr - 1).Value = ComboBox1.Value
    .Range("A" & lr).Resize(ListBox1.ListCount, ListBox1.ColumnCount).Value = ListBox1.List
    
    'Format cells
    .Range("B:B").NumberFormat = "dd/mm/yyyy"
    .Range("E:G").NumberFormat = "#,##0.00;-#,##0.00;-"
    .Range("A" & lr + 1, .Range("G" & Rows.Count).End(3)).Borders.LineStyle = xlContinuous
  End With
End Sub
 
Upvote 0
thanks
I'm not sure if this is complicated , but what I meant like
this

DECREASE.xlsm
ABCDEFG
1NAMES
2CCF-1000
3ITEMDATEINV.NOCASEDEBITCREDITBALANCE
4130/06/2023 --500.00-500.00
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 -500.0082,000.00
131015/09/2023VSTR_23446OUTSANDING3,600.00 -85,600.00
141115/09/2023RSS_23222OUTSANDING -860.0084,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
NAMES


and when try to copy form
11.JPG


result


DECREASE.xlsm
ABCDEFG
1NAMES
2CCF-1000
3ITEMDATEINV.NOCASEDEBITCREDITBALANCE
4130/06/2023 --500.00-500.00
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 -500.0082,000.00
131015/09/2023VSTR_23446OUTSANDING3,600.00 -85,600.00
141115/09/2023RSS_23222OUTSANDING -860.0084,740.00
151215/09/2023VCH20005 -15,000.0069,740.00
16SUM95,720.0024,980.0070,740.00
17
18
19NAMES
20CCF-1001
21ITEMDATEINV.NOCASEDEBITCREDITBALANCE
22130/06/20232,000.00 -2,000.00
23215/07/2023RVCH2000125,000.00 -27,000.00
24315/09/2023VSTR_23445OUTSANDING1,000.00 -28,000.00
25416/09/2023BSTR_23452PAID7,200.00 -35,200.00
26SUM35,200.00 -35,200.00
NAMES
 
Upvote 0
I'm not sure if this is complicated
In fact, yes it is. But try this:

VBA Code:
Private Sub CommandButton2_Click()
  Dim lr As Long, k As Long, i As Long
  Dim f As Range
  
  If ComboBox1.ListIndex = -1 Then
    MsgBox "Select Name"
    Exit Sub
  End If
  
  With Sheets("NAMES")
    Set f = .Range("D:D").Find(ComboBox1.Value, , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      
      For i = f.Row + 2 To .Range("A" & Rows.Count).End(3).Row
        If .Range("A" & i).Value = "" Then
          .Rows(f.Row + 2 & ":" & i - 1).Delete
          .Rows(f.Row + 3 & ":" & f.Row + ListBox1.ListCount + 1).Insert Shift:=xlDown ', CopyOrigin:=xlFormatFromLeftOrAbove
          lr = f.Row + 1
          Exit For
        End If
      Next
    
    Else

      If .Range("D2").Value = "" Then
        lr = 3
      Else
        lr = .Range("A" & Rows.Count).End(3).Row + 5
        .Range("A1:G3").Copy .Range("A" & lr - 2)
      End If
    
    End If
    
    .Range("D" & lr - 1).Value = ComboBox1.Value
    .Range("A" & lr).Resize(ListBox1.ListCount, ListBox1.ColumnCount).Value = ListBox1.List
    
    'Format cells
    .Range("B:B").NumberFormat = "dd/mm/yyyy"
    .Range("E:G").NumberFormat = "#,##0.00;-#,##0.00;-"
    .Range("A" & lr + 1, .Range("G" & lr + ListBox1.ListCount - 1)).Borders.LineStyle = xlContinuous
  End With
End Sub

🧙‍♂️
 
Upvote 0
VBA Code:
In fact, yes it is
sorry !
I tested for CCF-1000,CCF-1001 works , but why not with CCF-1002 and the others !
gives error application defined error in this line
VBA Code:
.Range("D" & lr - 1).Value = ComboBox1.Value
 
Upvote 0
This is important: You can explain what steps you took when the error occurred. And exactly what the error says.

After presenting the error, press the debug button, on the yellow line, bring the mouse pointer to the variable lr and take note of the number that appears in the window.

For example:
1727899851727.png


It also explains the conditions of the listbox when the error occurs, that is, how many records exist in the listbox.
 
Upvote 0
before
DECREASE.xlsm
ABCDEFG
1NAMES
2CCF-1000
3ITEMDATEINV.NOCASEDEBITCREDITBALANCE
4130/06/2023 --500.00-500.00
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 -500.0082,000.00
131015/09/2023VSTR_23446OUTSANDING3,600.00 -85,600.00
141115/09/2023RSS_23222OUTSANDING -860.0084,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
21
22NAMES
23CCF-1001
24ITEMDATEINV.NOCASEDEBITCREDITBALANCE
25130/06/20232,000.00 -2,000.00
26215/07/2023RVCH2000125,000.00 -27,000.00
27315/09/2023VSTR_23445OUTSANDING1,000.00 -28,000.00
28SUM28,000.00 -28,000.00
29
30
31NAMES
32CCF-1002
33ITEMDATEINV.NOCASEDEBITCREDITBALANCE
34130/06/2023 - - -
35215/09/2023BSJ_23447PAID -6,000.00-6,000.00
36SUM -6,000.00-6,000.00
37
38
39NAMES
40CCF-1003
41ITEMDATEINV.NOCASEDEBITCREDITBALANCE
42130/06/20231,000.00 -1,000.00
43215/07/2023RVCH200031,200.00 -2,200.00
44316/09/2023VSTR_23447PAID -1,600.00600.00
45SUM2,200.001,600.00600.00
NAMES


after
when show data on form
1.JPG


when click commandbutto2
er.JPG


and lr=0
 
Upvote 0
Try:

VBA Code:
Private Sub CommandButton2_Click()
  Dim lr As Long, k As Long, i As Long
  Dim f As Range
  
  If ComboBox1.ListIndex = -1 Then
    MsgBox "Select Name"
    Exit Sub
  End If
  
  With Sheets("NAMES")
    Set f = .Range("D:D").Find(ComboBox1.Value, , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      
      For i = f.Row + 2 To .Range("A" & Rows.Count).End(3).Row + 1
        If .Range("A" & i).Value = "" Then
          .Rows(f.Row + 2 & ":" & i - 1).Delete
          .Rows(f.Row + 3 & ":" & f.Row + ListBox1.ListCount + 1).Insert Shift:=xlDown ', CopyOrigin:=xlFormatFromLeftOrAbove
          lr = f.Row + 1
          Exit For
        End If
      Next
    
    Else

      If .Range("D2").Value = "" Then
        lr = 3
      Else
        lr = .Range("A" & Rows.Count).End(3).Row + 5
        .Range("A1:G3").Copy .Range("A" & lr - 2)
      End If
    
    End If
    
    .Range("D" & lr - 1).Value = ComboBox1.Value
    .Range("A" & lr).Resize(ListBox1.ListCount, ListBox1.ColumnCount).Value = ListBox1.List
    
    'Format cells
    .Range("B:B").NumberFormat = "dd/mm/yyyy"
    .Range("E:G").NumberFormat = "#,##0.00;-#,##0.00;-"
    .Range("A" & lr + 1, .Range("G" & lr + ListBox1.ListCount - 1)).Borders.LineStyle = xlContinuous
  End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,884
Messages
6,175,174
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