简体   繁体   中英

EXCEL VBA Skip blank row

Private Sub CommandButton1_Click()
Dim rng As Range
Dim cell As Variant

Set rng = Range("C8:C12")

For Each cell In rng
    Sheets("Sheet1").Range("A1:H7").Copy Destination:=Sheets("Quantity").Range("XFD4").End(xlToLeft).Offset(-3, 3)
    Sheets("Quantity").Range("XFD1").End(xlToLeft).Offset(0, 1).Value = cell.Offset(1, -1).End(xlUp).Value
    Sheets("Quantity").Range("XFD2").End(xlToLeft).Offset(0, 1).Value = cell.Value
    Sheets("Quantity").Range("XFD3").End(xlToLeft).Offset(0, 1).Value = cell.Offset(0, 1).Value  
Next
End Sub

What I want to accomplish here is to skip blank cell/row . Because it will copy empty data to the sheet. Is there any method eg Not isEmpty or isBlank for this For loop ? Thanks in advance.

You should be able to check IsEmpty(cell) to see if a cell is empty.

For example (untested):

For Each cell In rng
    If Not IsEmpty(cell) Then
        Sheets("Sheet1").Range("A1:H7").Copy Destination:=Sheets("Quantity").Range("XFD4").End(xlToLeft).Offset(-3, 3)
        Sheets("Quantity").Range("XFD1").End(xlToLeft).Offset(0, 1).Value = cell.Offset(1, -1).End(xlUp).Value
        Sheets("Quantity").Range("XFD2").End(xlToLeft).Offset(0, 1).Value = cell.Value
        Sheets("Quantity").Range("XFD3").End(xlToLeft).Offset(0, 1).Value = cell.Offset(0, 1).Value 
    End If 
Next

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