delete blank rows based on empty cells into column from sheet to another

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
531
Office Version
  1. 2019
HI

I want to copy data from sheet1 to sheet2 . should copy with all format and formula without empty rows. the empty rows depend on column A if any cell is empty in column A where located from row 21 until before row SUB TOTAL ,then should delete it because I will add rows before row SUB TOTAL every time
so any empty row from ROW 21 up to before row SUB TOTAL should delet e it

vv1.xlsm
ABCDEFG
1 INVOICE
2COMPANY LTD
3
4DATE
52022/08/02
6INVOICE
7BSJD-146
8CUS
9CUS-146
10
11TO:CONSIGNEE
12NAME
13ADDRESS
14PHONE
15EMAIL
16
17OREDERDATEFRSAL (FOB)TREM
18PUR-144
19
20ITEMDESCRIBETYPEORIGINQTYUNITTOTAL
211BS 1200R20G580THI200.00120.0024,000.00
222BS 1200R20G580JAP230.00110.0025300.00
230.00
240.00
250.00
260.00
27SUB TOTAL49300.00
28TAX1873.40
29FREIGHT0.00
30OTHERS0.00
31TOTAL$51,173.40
32For questions concerning this invoice, please contact
33Name, (321) 456-7890, Email Address
34www.yourwebaddress.com
sheet1
Cell Formulas
RangeFormula
F5F5=TODAY()
G21:G26G21=E21*F21
G27G27=SUM(G21:G22)
G28G28=SUM(G27)*3.8%
G31G31=G27+G28+G29+G30





result
should be
vv1.xlsm
ABCDEFG
1 INVOICE
2COMPANY LTD
3
4DATE
52022/08/02
6INVOICE
7BSJD-146
8CUS
9CUS-146
10
11TO:CONSIGNEE
12NAME
13ADDRESS
14PHONE
15EMAIL
16
17OREDERDATEFRSAL (FOB)TREM
18PUR-144
19
20ITEMDESCRIBETYPEORIGINQTYUNITTOTAL
211BS 1200R20G580THI200.00120.0024,000.00
222BS 1200R20G580JAP230.00110.0025300.00
23SUB TOTAL49300.00
24TAX1873.40
25FREIGHT0.00
26OTHERS0.00
27TOTAL$51,173.40
sheet2
Cell Formulas
RangeFormula
F5F5=TODAY()
G21:G22G21=E21*F21
G23G23=SUM(G21:G22)
G24G24=SUM(G23)*3.8%
G27G27=G23+G24+G25+G26

I hope this clear
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
See if this does what you need:

VBA Code:
Sub CopyAndDeleteRows()
    
    Application.ScreenUpdating = False

    Dim shtOrig As Worksheet, shtCopy As Worksheet
    Dim rowSubTot As Long, rowLastInv As Long
    Dim rRowOrig As Range
      
    Set shtOrig = Worksheets("Sheet1")
    Set shtCopy = Worksheets("Sheet2")
    
    With Application
        rowSubTot = .IfError(.Match("SUB TOTAL", shtOrig.Range("A:A"), 0), 0)
    End With

    If rowSubTot = 0 Then
        MsgBox "Sub Total not found on " & shtOrig.Name
        Exit Sub
    End If
    ' Copy sheet
    shtOrig.Cells.Copy Destination:=shtCopy.Range("A1")

    ' Copy row heights
    For Each rRowOrig In shtOrig.UsedRange.Rows
        shtCopy.Rows(rRowOrig.Row).RowHeight = rRowOrig.RowHeight
    Next rRowOrig

    ' Delete Blank Rows before Sub Total
    With shtCopy
        rowLastInv = .Range("A" & rowSubTot).End(xlUp).Row
        If rowLastInv > 20 Then
            .Range(.Cells(rowLastInv + 1, "A"), .Cells(rowSubTot - 1, "A")).EntireRow.Delete
        End If
        shtCopy.Activate
        shtCopy.Range("A1").Activate
    End With
    
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
        
End Sub
 
Upvote 0
Solution
Hello Abdo,

See if this helps:-

VBA Code:
Option Explicit
Sub AbdoTest()

Dim lr As Long: lr = Sheet1.Cells.Find("*Sub*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row

Application.ScreenUpdating = False
              
        With Sheet1.Range("A20:A" & lr)
                .AutoFilter 1, ""
                .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
                .AutoFilter
        End With
        
        Sheet1.UsedRange.Copy Sheet2.[A1]

Application.ScreenUpdating = True

End Sub

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
@Alex Blakenburg fantastic !
just I try to modify your code should clear contents specific cells(F5,F7,F9,A18) in sheet1 after copy to sheet2 espicially from row21 up to until before sub total . I try to put it in the end of code like this
VBA Code:
With shtOrig
    rowLastInv = .Range("A" & rowSubTot).End(xlUp).Row
        If rowLastInv > 20 Then
            .Range(.Cells(rowLastInv + 1, "A"), .Cells(rowSubTot - 1, "A")).ClearContents
            End If
            End With
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
but doesn't work , can you guide me please?
 
Upvote 0
@vcoolio nice work without loop . perfect by filter, but unfortunately also delete from sheet1 should not delete it
also I would just clear contents from row21 up to before sub total & cells(F5,F7,F9,A18) in sheet1 after copy to sheet2
thanks
 
Upvote 0
@Alex Blakenburg fantastic !
just I try to modify your code should clear contents specific cells(F5,F7,F9,A18) in sheet1 after copy to sheet2 espicially from row21 up to until before sub total .

Yet another reason to avoid merged cells ;)

With my original code before Application.CutCopyMode = False put this:

VBA Code:
    Dim i As Long
    Dim arr As Variant
    arr = Array("F5", "F7", "F9", "A18")
    For i = 0 To UBound(arr)
        shtOrig.Range(arr(i)).MergeArea.ClearContents
    Next i
 
Upvote 0
I can't update last post . so when clearcontents from row 21 to last row before SUBTOTAL . should keep the formula in coumn G .
 
Upvote 0
I can't update last post . so when clearcontents from row 21 to last row before SUBTOTAL . should keep the formula in coumn G .
Add the below after what I gave you previously

VBA Code:
    With shtOrig
        .Range(.Cells(21, "A"), .Cells(rowSubTot - 1, "F")).ClearContents
    End With
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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