copy data from listbox to sheet after headers and before Total row

Mussa

Board Regular
Joined
Jul 12, 2021
Messages
241
Office Version
  1. 2019
  2. 2010
Hi
I have this code to copy data from listbox to sheet
VBA Code:
Private Sub cmdSend_Click()
    With Me.ListBox1
        If .ListCount > 0 Then
            Sheets("SL").[a7].Resize(.ListCount, .ColumnCount) = .List
        End If
    End With
End Sub
bu I need adapting by copy after row6 and before total row and if I have rows in listbox more than empty rows inside the sheet then should insert new rows with the same borders ,if I have rows in listbox less than empty rows inside the sheet then should delete empty rows

here is the structure inside sheet
Sample.xlsm
ABCDE
6ITEMIDQTYUNIT PRICETOTAL
7
8
9
10TOTAL.00
SL
Cell Formulas
RangeFormula
E10E10=SUM(E7:E9)


when fill listbox
1.JPG


the result should be
Sample.xlsm
ABCDE
6ITEMIDQTYUNIT PRICETOTAL
71ABSS-1001010.00100.00
82*** TYYY 20001222.00264.00
93ASDE9900/1200222.0044.00
104XDD*7777 212.0024.00
11TOTAL432.00
SL
Cell Formulas
RangeFormula
E11E11=SUM(E7:E10)
 
Re: "it delete the borders"
Show the code with which you tried to fix that so we can fix it for you.
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Show the code with which you tried to fix that so we can fix it for you.
I don't understand it !
just I used code in post #27
I will make borders manually bfore I run the code .
 
Upvote 0
Add the four lines as indicated so the code should look like this and try.
Code:
Private Sub CommandButton1_Click()
Dim c As Range    '<---- Add this line
Dim sh4 As Worksheet    '<---- Change as required
Set sh4 = Worksheets("Sheet4")    '<---- Change as required
With sh4    '<---- Change as required
    .Cells(1).Offset(6).Resize(Cells(Rows.Count, 1).End(xlUp).Row - 6, 5).Delete Shift:=xlUp
    .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Resize(Me.ListBox1.ListCount, Me.ListBox1.ColumnCount).Value = ListBox1.List
With .Cells(Rows.Count, 1).End(xlUp).Offset(1)
    .Value = "TOTAL"
    .Interior.Color = RGB(255, 255, 0)
    .Offset(, 4).Formula = "=SUM(E7:E" & Cells(Rows.Count, 1).End(xlUp).Row - 1 & ")"
End With
End With
Cells(1).Offset(6).Resize(Cells(Rows.Count, 1).End(xlUp).Row - 6, 5).HorizontalAlignment = xlCenter
    For Each c In Range("A7:E" & Cells(Rows.Count, 1).End(xlUp).Row)    '<---- Add this line
        If Len(c) <> 0 Then c.BorderAround ColorIndex:=1, Weight:=xlThin    '<---- Add this line
    Next c    '<---- Add this line
Unload Me
End Sub
 
Upvote 0
Add the four lines as indicated so the code should look like this and try.
sorry about delaying !🙏🙏🙏
the code works greatly when add before TOTAL row , but when delete before TOTAL row will show application defined error in this line
VBA Code:
.Cells(1).Offset(6).Resize(Cells(Rows.Count, 1).End(xlUp).Row - 6, 5).Delete Shift:=xlUp
 
Upvote 0

Forum statistics

Threads
1,215,200
Messages
6,123,601
Members
449,109
Latest member
Sebas8956

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