'Copy a specific range of rows once conditions are met and paste it to another sheet
I have two worksheets:
Sheet1 - "raw"
Sheet2 - "copy"
The button that has the macro code is in Sheet2. Once I click the button, the macro will run with the first condition that if cell B2 in Sheet1 has a value, it should copy row B2:X2 and paste it to the second row (after the header) of Sheet2. But if no value, it should loop to the next row and repeat the operation.
The condition I'm having trouble with adding, is as follows:
If cell X2 is equal to 'Yes', it should copy row B2:M2 and row range Y2:AH2 of Sheet1 then paste it to range B3:N3 of Sheet2 and O3:X3 of Sheet2. This same goes for row AI2, AT2, BE2, BP2, and CA2 wherein values contain Yes or No. But if the answer is 'No' or has no value, it will loop on the next row in Sheet1 then run again the same copy method until the last row. But it shouldnt copy a blank row.
Below is the current code for the first condition.
Sub Copy3()
Dim srcws As Worksheet
Dim destws As Worksheet
Set srcws = Worksheets("raw")
Set destws = Worksheets("copy")
srcws.Select
SecondRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To SecondRow
ThisValue = Cells(i, 2).Value
If ThisValue <> " " Then
srcws.Range("B" & i & ":W" & i).Copy _
Destination:=destws.Range("B" & i)
End If
Next i
Application.ScreenUpdating = True
End Sub
This is somehow working though i dont want to copy blank rows only those have values on cell B2.
Any help will be appreciated for this to work!
For the Second condition - If cell X2 is equal to 'Yes', then it will copy range B2:M2 and range Y2:AH2 from Sheet1 to next row of Sheet2 which is under the row of copied data from first condition.
Basically because every row has the employee data and first employee dependent from range B2:W2 of Sheet1 then every row cell of X (Sheet1) contains value of 'Yes' or 'No'. That if row X2 value is Yes, it should copy the employee details from Sheet1 range B2:M2 and paste it to range B3:N3 of Sheet2. Then copy range Y2:AH2 from Sheet1 and paste it to range O3:X3 of Sheet2
I hope this will help explain the second condition. Though i dont have the code yet created for this.
Thank you!
Condition 2: If cell X = Yes, then copy range B:M from Sheet1 and paste it to next row range B:N of Sheet2 and copy range Y:AH of Sheet1 and paste it to next row range O:X of Sheet2 But if No, go to next row of Sheet1 and apply condition 1
Condition 3: If cell AI = Yes then copy range B:M from Sheet1 and paste it to next row range B:N of Sheet2 and copy range AJ:AS of Sheet1 and paste it to next row range O:X of Sheet2
Condition 4: If cell AT = Yes then copy range B:M from Sheet1 and paste it to next row range B:N of Sheet2 and copy range AU:BD of Sheet1 and paste it to next row range O:X of Sheet2
Condition 5: If cell BE = Yes then copy range B:M from Sheet1 and paste it to next row range B:N of Sheet2 and copy range BF:BO of Sheet1 and paste it to next row range O:X of Sheet2
Condition 6: If cell BP = Yes then copy range B:M from Sheet1 and paste it to next row range B:N of Sheet2 and copy range BQ:BZ of Sheet1 and paste it to next row range O:X of Sheet2
Sorry for the confusion but let me know for any details needed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
