简体   繁体   中英

Copy row if cell is empty

I have a raw datasheet in the first tab with Invoice numbers and a cell next to it stating if they have (quarter number and year)/ haven't been paid (empty cell).

I want to copy the unpaid invoice numbers (or even better the whole row) to second sheet.

Because there are couple thousand rows, I can't use an if statement and then sort and delete the rows that have been paid.

Is there a way to show only the wanted information?

Suppose you have the following structure starting from A1 (worksheet Orig )

起源

The following generic commented code solve the problem:

Sub CopyInv()
 Dim O As Worksheet 
 Dim D As Worksheet
 Set O = Worksheets("Orig") 
 Set D = Worksheets("Dest")
 Dim CF As Integer ' Last column
 Dim LD As Long ' Last line in destination
 Dim LO As Long ' Last line filled
 Dim LOF As Long ' Last line in origin
' Final table colunm in source
 CF = O.Cells(1, 1).End(xlToRight).Column
' Last line with data in destination (emulate ^Up)
 LD = D.Cells(Cells.Rows.Count, 1).End(xlUp).Row
 Range(D.Cells(1, 1), D.Cells(LD, CF - 1)).Clear
 O.Select
' Sort by 3rd column
 O.Cells(1, 3).Sort key1:=O.Cells(1, 3), 
                Order1:=xlAscending, Header:=xlYes 
 Cells(1, 1).Select
' Last filled data in "paid" column
 LO = O.Cells(1, 3).End(xlDown).Row + 1
' Last line in origin
 LOF = O.Cells(Cells.Rows.Count, 1).End(xlUp).Row
  ' Copy header in destination
 Range(O.Cells(1, 1), O.Cells(1, 2)).Copy (D.Cells(1, 1))  
 If LOF >= LO Then   ' Copy in destination
   Range(O.Cells(LO, 1), O.Cells(LOF, 2)).Copy (D.Cells(2, 1)) 
 End If
 D.Select
 Cells(1, 1).Select
End Sub

The result in destination (worksheet Dest ) starting from A1 is:

目的地

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM