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)
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Put "TOTAL" and the formula in the sheet after the data has been transferred into it. Make that part of your code.
 
Upvote 0
Check and change references as required. Try on a copy of your original workbook.

In a regular Module.
Code:
Sub Show_It()
    UserForm1.Show
End Sub

A UserForm1 With a ListBox1 and a CommandButton1
In the Properties Window for the Listbox, set ColumnCount and ColumnWidths to desired amount(s)

In the UserForm Module (Change the Range to the needed amount and where the data is.)
Code:
Private Sub UserForm_Initialize()
Me.ListBox1.List = Sheets("Sheet1").Range("L2:N" & Sheets("Sheet1").Cells(Rows.Count, 12).End(xlUp).Row).Value
End Sub

Code:
Private Sub CommandButton1_Click()
If Cells(Rows.Count, 1).End(xlUp).Value <> "TOTAL" Then Cells(Rows.Count, 1).End(xlUp).Offset(2).Value = "TOTAL"
With Sheets("Sheet1")
    .Columns(1).Find("TOTAL", , , 1).Resize(, 5).ClearContents
    .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Resize(Me.ListBox1.ListCount, Me.ListBox1.ColumnCount).Value = ListBox1.List
End With
With Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1)
    .Value = "TOTAL"
    .Offset(, 4).Formula = "=SUM(E7:E" & Cells(Rows.Count, 1).End(xlUp).Row - 1 & ")"
End With
Unload Me
End Sub
 
Upvote 0
thanks but it will add TOTAL word even if it's existed .
based on your code if it isn't existed will add otherwise should ignore it.
last thing how avoid copy the headers when show in listbox .
 
Upvote 0
Re: "thanks but it will add TOTAL word even if it's existed ." It should not if your actual is like you showed in Post #1. (Last value in Column A)

Re: "last thing how avoid copy the headers when show in listbox ." Change the range to the actual Range. In Post #4 it has L2:N ....
 
Upvote 0
Code:
If Cells(Rows.Count, 1).End(xlUp).Value <> "TOTAL" Then Cells(Rows.Count, 1).End(xlUp).Offset(2).Value = "TOTAL"
You can leave this line out if there is a guarantee that it has "TOTAL" as a last Value in Column A.
 
Upvote 0
You can leave this line out if there is a guarantee that it has "TOTAL" as a last Value in Column A.
ok.

before
Sample1.xlsm
ABCDE
6ITEMIDQTYUNIT PRICETOTAL
7
8
9TOTAL.00
SL
Cell Formulas
RangeFormula
E9E9=SUM(E7:E8)


after
Sample1.xlsm
ABCDE
6ITEMIDQTYUNIT PRICETOTAL
71ABSS-1002220.00440.00
82*** TYYY 20003320.00660.00
93ASDE9900/12001020200.00
10TOTAL1,300.00
SL
Cell Formulas
RangeFormula
E10E10=SUM(E7:E9)

as you see will move TOTAL word down , but should add rows before TOTAL row !
 
Upvote 0
"TOTAL" is down. Do you mean the yellow color?
 
Upvote 0

Forum statistics

Threads
1,215,201
Messages
6,123,621
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