Macro for conditional copy range

Khardin

New Member
Joined
May 14, 2012
Messages
9
Hello,

First let me thank you for your time assisting me with this. I have extensively researched the forums and tried to implement/modify various suggestions but to no avail.

I am trying to create a macro that will copy and paste a range of cells based on the condition of another cell.
I know the logic I am trying to use, just not how to implement it:

The logic is:
If Cell D2=1, copy range(E2:I2) and paste in range(K3:P3)
If Cell D2=0, copy range(E2:I2) and paste in range(Q3:U3)
continue to iterate through (ie: D3=1, copy range (E3:I3) and paste in range(K4:P4))
Stop at first empty cell in D

Column D (starting at row 2) will always contain either a 0 or a 1 and I am always looking to copy the same number of cells
The data in range E:I is all numeric
Range K:P and Q:U are all blank starting from row 3

Thank you again for your assistance!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi Khardin,

Try this:

Code:
Dim rngCell As Range
    
    Application.ScreenUpdating = False
    
    For Each rngCell In Range("D2", Range("D" & Rows.Count).End(xlUp))
        Select Case rngCell.Value
            Case Is = 0
                Range("E" & rngCell.Row & ":I" & rngCell.Row).Copy Destination:=Range("Q" & rngCell.Row + 1 & ":U" & rngCell.Row + 1)
            Case Is = 1
                Range("E" & rngCell.Row & ":I" & rngCell.Row).Copy Destination:=Range("K" & rngCell.Row + 1 & ":P" & rngCell.Row + 1)
        End Select
    Next rngCell
    
    Application.ScreenUpdating = True
    
    MsgBox "Process complete.", vbInformation

Regards,

Robert
 
Upvote 0
Hi Khardin,

Try this:

Code:
Dim rngCell As Range
    
    Application.ScreenUpdating = False
    
    For Each rngCell In Range("D2", Range("D" & Rows.Count).End(xlUp))
        Select Case rngCell.Value
            Case Is = 0
                Range("E" & rngCell.Row & ":I" & rngCell.Row).Copy Destination:=Range("Q" & rngCell.Row + 1 & ":U" & rngCell.Row + 1)
            Case Is = 1
                Range("E" & rngCell.Row & ":I" & rngCell.Row).Copy Destination:=Range("K" & rngCell.Row + 1 & ":P" & rngCell.Row + 1)
        End Select
    Next rngCell
    
    Application.ScreenUpdating = True
    
    MsgBox "Process complete.", vbInformation

Regards,

Robert

Thanks Robert! This works perfectly
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,619
Members
449,240
Latest member
lynnfromHGT

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