Hei,
I need help with Excel. For better undrestanding, picture is attached. Basically i need excel to go through row by row and copy all rows with set values (see picture).
etc.
EDIT: ATTACHED EXCEL FILE WITH EXAMPLE OUTPUT
If Sheets(1).Cells(i, 1).Value = 1 Then
//*copy entire row and skip everything until Sheets(1).Cells(i, 1).Value = 1*//
else if Sheets(1).Cells(i, 1).Value = 1 And Sheets(1).Cells(i, 3).Value = "x" Then
*copy entire row and continue loop*
If Sheets(1).Cells(i, 1).Value = 2 Then
//*copy entire row and skip everything until Sheets(1).Cells(i, 1).Value = 2 or higer*//
else if Sheets(1).Cells(i, 1).Value = 2 And Sheets(1).Cells(i, 3).Value = "x" Then
*copy entire row and continue loop*
If Sheets(1).Cells(i, 1).Value = 3 Then
//*copy entire row and skip everything until Sheets(1).Cells(i, 1).Value = 3 or higher*//
else if Sheets(1).Cells(i, 1).Value = 3 And Sheets(1).Cells(i, 3).Value = "x" Then
*copy entire row and continue loop*
If Sheets(1).Cells(i, 1).Value = 4 Then
//*copy entire row and skip everything until Sheets(1).Cells(i, 1).Value = 4 or higher*//
else if Sheets(1).Cells(i, 1).Value = 4 And Sheets(1).Cells(i, 3).Value = "x" Then
*copy entire row and continue loop*
I have made changes to the code, now it copies data from columns A:E, to add more columns change where it says "5" in Cells(i + 1, 5) or Cells(counter_rows, 5) and add a different column number.
The rows with data are stored in an array to speed up the code and make it more compact.
Also you should put this subroutine in a MODULE in VBA. Don't add code to Sheets.
Sub copy_rows_to_sheet2()
Dim nb_rows As Integer
Dim counter_rows As Integer
Application.ScreenUpdating = False 'speed up code
nb_rows = Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row 'count the number of rows with data on sheet(1)
counter_rows = 2 'the first row on sheet(2) where we start copying data from
Dim Arr() As Variant ' declare an unallocated array, stores data from range on the sheets (any number of rows and columns)
For i = 2 To nb_rows
Sheets(1).Select 'to add data to array, first select the sheet1
If Sheets(1).Cells(i, 1).Value = 1 Or Sheets(1).Cells(i, 1).Value = 2 Then
If Sheets(1).Cells(i, 3).Value = "x" Then 'we copy 2 rows when we have x in col 3
Arr = Range(Cells(i, 1), Cells(i + 1, 5)).Value 'copy all values from row i and next row counter_rows and columns (A to E=5)
Sheets(2).Select 'before the array is pasted to sheet2 first it needs to be selected
Range(Cells(counter_rows, 1), Cells(counter_rows + 1, 5)).Value = Arr
counter_rows = counter_rows + 2 'counter increments by 2 rows
Else
Arr = Range(Cells(i, 1), Cells(i, 5)).Value 'copy row i and 5 columns
Sheets(2).Select
Range(Cells(counter_rows, 1), Cells(counter_rows, 5)).Value = Arr
counter_rows = counter_rows + 1 'counter increments by 1 row
End If
End If
Next i
Application.ScreenUpdating = False 'turn back
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.