VBA - copy non-contiguous range in loop

Trebor8484

Board Regular
Joined
Oct 27, 2018
Messages
69
Office Version
  1. 2013
Platform
  1. Windows
Hi all,

Any ideas why the below piece of code won't copy the desired range.

It should be copying ranges C2:K2 and L2:M2 in the first iteration.

Basically the code should always copy C:K for the relevant row in the loop then either L:M,N:O,P:Q,R:S,T:U depending on which column the loop is currently on.

Code:
 Sub MergeMe()


    Dim lr As Long
    Dim DataSht As Worksheet


    Dim c As Range
    Dim CopyRange As Range
    Dim dstsht As Worksheet
    
    Set DataSht = Sheets("Data")
    Set dstsht = ThisWorkbook.Sheets("Merged")
    
    lr = DataSht.Range("A" & Rows.Count).End(xlUp).Row
    
    For Each c In DataSht.Range("L2:L" & lr & ",N2:N" & lr & ",P2:P" & lr & ",R2:R" & lr & ",T2:T" & lr)
        If c.Value <> "" Then
            Set CopyRange = Union(DataSht.Range(Cells(c.Row, "C"), Cells(c.Row, "K")), DataSht.Range(Cells(c.Row, c.Column), Cells(c.Row, c.Offset(0, 1))))
            CopyRange.Copy
            dstsht.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
        End If
    Next c


End Sub

Thanks
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
You need to qualify the Cells as well as the Range, try
Code:
With datasht
   Set copyrange = Union(.Range(.Cells(c.Row, "C"), .Cells(c.Row, "K")), .Range(.Cells(c.Row, c.Column), .Cells(c.Row, c.Offset(0, 1))))
End With
 
Upvote 0
Thanks Fluff

Its not erroring now, but the range isn't correct:

?copyrange.Address
$A$2:$L$2

It should be C2:M2

So on the first iteration of the loop the last part of the range function: .Range(.Cells(c.Row, c.Column), .Cells(c.Row, c.Offset(0, 1))))

Should return: L2:M2

?c.Address
$L$2
?c.offset(0,1).Address
$M$2


Thanks
 
Upvote 0
How about
Code:
   Set copyrange = Union(.Range("C" & c.Row).Resize(, 9), c.Resize(, 2))
 
Upvote 0
So, just to clarify, if the loop later finds something in column N

For Each c In DataSht.Range("L2:L" & lr & ",N2:N" & lr & ",P2:P" & lr & ",R2:R" & lr & ",T2:T" & lr)

Then the range should be C2:K2,N2:O2

Then
 
Upvote 0
Have you tried the code in post#4?
 
Upvote 0
Hi, yes just tested and it works perfectly thanks.

Just out of interest, why the need for a with statement to set the range? I would have thought that as the For Each Loop had set the sheet then the rest of the loop would have known what sheet it referred to?
 
Upvote 0
If you are working on the non-active sheet you need to qualify all ranges & cells, otherwise VBA will assume that they refer to the active sheet.
Although with the slimmed down code in post#4 there is no real need for the with statement anymore, so you could just use
Code:
   Set copyrange = Union(datasht.Range("C" & c.Row).Resize(, 9), c.Resize(, 2))
 
Upvote 0
Sorry, another quick question.

I also need to copy whatever is in column Z for the particular row number in the loop. Not sure how to amend the resize function to accommodate?
 
Upvote 0
How about
Code:
         Set CopyRange = Union(.Range("C" & c.Row).Resize(, 9), c.Resize(, 2), .Range("Z" & c.Row))
 
Upvote 0

Forum statistics

Threads
1,214,383
Messages
6,119,198
Members
448,874
Latest member
Lancelots

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