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)
 
Code:
With Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1)
    .Interior.Color = RGB(255,255,0)    '<----- Add this line    
    .Value = "TOTAL"
    .Offset(, 4).Formula = "=SUM(E7:E" & Cells(Rows.Count, 1).End(xlUp).Row - 1 & ")"
End With
 
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
thanks , but still keep color when move TOTAL row down as in post # 8 , also if move up TOTAL row will leave color under TOTAL row !
 
Upvote 0
Change this section, or whatever it has been changed to
Code:
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
to so
Code:
With Sheets("Sheet1").Columns(1).Find("TOTAL", , , 1).Resize(, 5)
    .ClearContents
    .Interior.Color = xlNone
End With
 
Upvote 0
will delete TOTAL word and pops error object variable or with block variable not set
VBA Code:
With Sheets("Sheet1").Columns(1).Find("TOTAL", , , 1).Resize(, 5)
 
Upvote 0
Did you change the sheet and column references?
 
Upvote 0
here is the code
VBA Code:
With Sheets("SL")
    .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("SL").Columns(1).Find("TOTAL", , , 1).Resize(, 5)
    .ClearContents
    .Interior.Color = xlNone
End With
 
Upvote 0
As suggested in Post #14, change the top 4 lines to the bottom 4 lines.
Delete the top 4 lines of the code you have in Post #18 so what you'll have left is
VBA Code:
With Sheets("SL").Columns(1).Find("TOTAL", , , 1).Resize(, 5)
    .ClearContents
    .Interior.Color = xlNone
End With
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,664
Members
449,114
Latest member
aides

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