简体   繁体   中英

How can I copy specific rows in one worksheet to another based on multiple cell values?

I have only basic vba knowledge and I am looking for help. I have searched this topic but I can't find anything for my specific needs. I have to work with spreadsheets generated by a piece of test equipment. I am trying to look at the values in each row in starting in column C up to as many as 60 columns, which varies. If the value of any cell from C7 on in not “”, I want to copy the entire row to a new sheet. The data in each cell will be either a positive or negative number with 3 decimal places. I may have as many as 3500 rows to go through with 10-20 rows that have any information that I would like to move. Example In the example screenshot, I only care about copying row 7 and 15

I came up with this but I'm sure there has to be a more efficient solution

    Sub Splice()
   
    Dim lr As Integer 'last row
    Dim lc As Integer 'last column
    Dim r As Integer 'row number
    
    With Worksheets("Splice loss")
    lr = .Cells(.Rows.Count, "A").End(xlUp).Row 'count number of rows
    lc = .Cells(1, Columns.Count).End(xlToLeft).Column 'count number of columns
    End With
    
    i = 7 'first row number in destination sheet
        
    For r = 7 To lr
    With Worksheets("Splice loss")
    If WorksheetFunction.Sum(Range(Cells(r, 3), (Cells(r, lc)))) <> 0 Then 'check values in each row
    Range(Cells(r, 3), Cells(r, lc)).EntireRow.Copy Worksheets("Splices").Range("A" & i) 'copy row with data to new sheet
        
    i = i + 1
    End If
    End With
    Next
    

End Sub

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