VBA to copy specific cells

topi1

Board Regular
Joined
Aug 6, 2014
Messages
161
Office Version
  1. 2010
Hi, can someone please help with the macro where it copies rows (cells) B and C when the value in the corresponding rows(cells) in the column A is 1 or 2. Following is the example. Thank you.

Before VBA
1 a q
1
2 c e
2 d r
3 e t
3 f y

After VBA
a q

c e
d r
 
Ran out of time to update my reply above to include the code tags :)

VBA Code:
Sub DataSplit()
Dim i As Long
 Dim LR As Long
 
 LR = Range("A" & Rows.Count).End(xlUp).Row
 
 For i = 1 To LR
    Range("D1:D" & i).FormulaR1C1 = _
        "=IF(AND(RC[-3]<3,RC[-2]<>"""",RC[-1]<>""""),RC[-2],"""")"
    Range("E1:E" & i).FormulaR1C1 = _
        "=IF(AND(RC[-4]<3,RC[-3]<>"""",RC[-2]<>""""),RC[-2],"""")"
 Next
For i = 1 To LR
    Range("D1:E" & i).Value = Range("D1:E" & i).Value
Next
End Sub

This version eliminates the 2nd loop

VBA Code:
Sub DataSplit()
Dim i As Long
 Dim LR As Long
 
 LR = Range("A" & Rows.Count).End(xlUp).Row
 
 For i = 1 To LR
    Range("D1:D" & i).FormulaR1C1 = _
        "=IF(AND(RC[-3]<3,RC[-2]<>"""",RC[-1]<>""""),RC[-2],"""")"
    Range("E1:E" & i).FormulaR1C1 = _
        "=IF(AND(RC[-4]<3,RC[-3]<>"""",RC[-2]<>""""),RC[-2],"""")"
 Next

    Range("D1:E" & LR).Value = Range("D1:E" & LR).Value
    
End Sub
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Thanks for letting us know.


Does this one work getting what you need?
Sub DataSplit()
Dim i As Long
Dim LR As Long

LR = Range("A" & Rows.Count).End(xlUp).Row

For i = 1 To LR
Range("D1:D" & i).FormulaR1C1 = _
"=IF(AND(RC[-3]<3,RC[-2]<>"""",RC[-1]<>""""),RC[-2],"""")"
Range("E1:E" & i).FormulaR1C1 = _
"=IF(AND(RC[-4]<3,RC[-3]<>"""",RC[-2]<>""""),RC[-2],"""")"
Next
For i = 1 To LR
Range("D1:E" & i).Value = Range("D1:E" & i).Value
Next
End Sub
Thank you so much. It works as long as neither of the cells in the B and C columns from the same row is empty. Both B and C column cells have to be non-blank for it to work. Thank you so much for helping me.

RC[-3]<3 .... does this mean that the third cell to the left of the destination cell has the value that is less than 3. Trying to understand your formula. Thanks.
 
Upvote 0
Thank you so much. It works as long as neither of the cells in the B and C columns from the same row is empty. Both B and C column cells have to be non-blank for it to work. Thank you so much for helping me.
That is accurate. I used a formula based on the sample data you provided. Will you have data in this manner by chance:

PERSONAL.xlsm
ABC
11rd
22f
32e
43ef
53g
Sheet4

Or will it remain consistent and you only need to extract when both columns B & C have values in them?

Yes. The formula is looking 3 cells to the left for any number less than 3.

Happy to help!
 
Upvote 0
That is accurate. I used a formula based on the sample data you provided. Will you have data in this manner by chance:

PERSONAL.xlsm
ABC
11rd
22f
32e
43ef
53g
Sheet4

Or will it remain consistent and you only need to extract when both columns B & C have values in them?

Yes. The formula is looking 3 cells to the left for any number less than 3.

Happy to help!
Yes. It is possible that sometime B or C may be empty. I apologize, didn't realize that it would matter when I submitted my example.
 
Upvote 0
Yes. It is possible that sometime B or C may be empty. I apologize, didn't realize that it would matter when I submitted my example.
Test this modification out and let me know if it works.

VBA Code:
Sub DataSplit()
Dim i As Long
Dim LR As Long
 
 LR = Range("A" & Rows.Count).End(xlUp).Row
 
 For i = 1 To LR
    Range("D1:D" & i).FormulaR1C1 = "=IF(AND(RC[-3]<3,RC[-2]<>""""),RC[-2],"""")"
    Range("E1:E" & i).FormulaR1C1 = "=IF(AND(RC[-4]<3,RC[-2]<>""""),RC[-2],"""")"
 Next

    Range("D1:E" & LR).Value = Range("D1:E" & LR).Value
    
End Sub
 
Upvote 0
Test this modification out and let me know if it works.

VBA Code:
Sub DataSplit()
Dim i As Long
Dim LR As Long
 
 LR = Range("A" & Rows.Count).End(xlUp).Row
 
 For i = 1 To LR
    Range("D1:D" & i).FormulaR1C1 = "=IF(AND(RC[-3]<3,RC[-2]<>""""),RC[-2],"""")"
    Range("E1:E" & i).FormulaR1C1 = "=IF(AND(RC[-4]<3,RC[-2]<>""""),RC[-2],"""")"
 Next

    Range("D1:E" & LR).Value = Range("D1:E" & LR).Value
   
End Sub
Perfect. Thank you. Thank you.
 
Upvote 0
@Coyotex3 you don't need the loop.

VBA Code:
Sub DataSplit_mod()
    Dim i As Long
    Dim LR As Long
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
    Range("D1:E" & LR).FormulaR1C1 = _
        "=IF(AND(RC1<3,RC[-2]<>""""),RC[-2],"""")"
    Range("D1:E" & LR).Value = Range("D1:E" & LR).Value
End Sub
 
Upvote 1
@Coyotex3 you don't need the loop.

VBA Code:
Sub DataSplit_mod()
    Dim i As Long
    Dim LR As Long
   
    LR = Range("A" & Rows.Count).End(xlUp).Row
   
    Range("D1:E" & LR).FormulaR1C1 = _
        "=IF(AND(RC1<3,RC[-2]<>""""),RC[-2],"""")"
    Range("D1:E" & LR).Value = Range("D1:E" & LR).Value
End Sub
You are 100% right! This is a much better code 😄!

Thank you for the feedback 😊!
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top