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
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Does this work? This code uses a formula on column D to extract the data and displays the results in columns D:E.

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" & LR).Formula2R1C1 = _
        "=IF(AND(RC[-3]<3,RC[-2]:RC[-1]<>""""),RC[-2]:RC[-1],"""")"
 Next

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

This code uses a formula on column D to extract the data and displays the results in columns D:E.
 
Upvote 0
Thank you for your prompt response. It gives an error "Object doesn't support this property or method".
 
Upvote 0
That's odd, I can't replicate the specific error on my end

Before running the code:

PERSONAL.xlsm
ABCDE
11de
21
32rd
42dc
53fd
63cc
73gc
8
9
Sheet5



After running the code:

PERSONAL.xlsm
ABCDEF
11dede
21
32rdrd
42dcdc
53fd
63cc
73gc
8
Sheet5
 
Upvote 0
Please update your profile to show what version of Excel you are using.
@Coyotex3's code will only work if you have MS365, is that the case ?

Also have you put the code into a standard module (not in a sheet module) ?
 
Upvote 1
Please update your profile to show what version of Excel you are using.
@Coyotex3's code will only work if you have MS365, is that the case ?

Also have you put the code into a standard module (not in a sheet module) ?
Hi Alex, I actually did not know this code would only work in M365.

Why is that? Genuinely curious as to why this is.
 
Upvote 0
Hi Alex, I actually did not know this code would only work in M365.

Why is that? Genuinely curious as to why this is.
I can't conclusive prove it since I only have MS365 but if you stop the code before you replace the formulas with values and look in the spreadsheet you will find that it "spills". Your excel needs to support dynamic arrays for that to work and that needs MS365 or Excel 2021.
 
Upvote 1
I can't conclusive prove it since I only have MS365 but if you stop the code before you replace the formulas with values and look in the spreadsheet you will find that it "spills". Your excel needs to support dynamic arrays for that to work and that needs MS365 or Excel 2021.
This makes sense! Thank you. I did see the “spill” message on my end as well. Once OP responds I will try to adjust the code if still needed.
 
Upvote 0
Hi Alex, I actually did not know this code would only work in M365.

Why is that? Genuinely curious as to why th2010 version.

Please update your profile to show what version of Excel you are using.
@Coyotex3's code will only work if you have MS365, is that the case ?

Also have you put the code into a standard module (not in a sheet module) ?
2010 version.
I updated my profile as requested.
The code failed in standard and sheet module.
Thank you.
 
Upvote 0
2010 version.
I updated my profile as requested.
The code failed in standard and sheet module.
Thank you.
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
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,984
Members
449,092
Latest member
Mr Hughes

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