VBA to copy mutiple ranges based on criteria

Elvis

New Member
Joined
Apr 23, 2006
Messages
45
Hi,

I have setup the code below in attempt to do a conditional copy of mutiple ranges/rows from one sheet to another but was wondering if there is a way copying the ranges in one go instead of having multiple if statements?

For example, for rows 5 to 10, I want to copy values in column B to N for each row and paste into the sheet result but only if column A of each row starts with or contains "1".

Would using some kind of union function be the best way of doing this?

Hope someone can help!

Thanks,

Elvis

Sub copyranges

Worksheets("Calculate").Select

If Left(Range("a5"), 1).Value = 1 Then

Range("B5:N5").Copy
Sheets("Results").Select
Range("a60000").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
End If


If Left(Range("a6"), 1).Value = 1 Then

Range("B6:N6").Copy
Sheets("Results").Select
Range("a60000").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
End If

If Left(Range("a7"), 1).Value = 1 Then

Range("B7:N7").Copy
Sheets("Results").Select
Range("a60000").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
End If

If Left(Range("a8"), 1).Value = 1 Then

Range("B8:N8").Copy
Sheets("Results").Select
Range("a60000").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
End If

If Left(Range("a9"), 1).Value = 1 Then

Range("B9:N9").Copy
Sheets("Results").Select
Range("a60000").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
End If

If Left(Range("a10"), 1).Value = 1 Then

Range("B10:N10").Copy
Sheets("Results").Select
Range("a60000").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
End If

End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try this:
Code:
Sub copyranges()

    Dim i As Long
    
    With Worksheets("Calculate")
    
        For i = 5 To 10
            If Left(.Cells(i, 1), 1).Value = 1 Then
                .Range("B1:N1").Offset(i, 1).Copy
                Sheets("Results").Range("A65535").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
            End If
        Next i
    
    End With

End Sub
 
Upvote 0
This tests if the value "contains" a 1

Code:
Sub copyranges2()

    Dim cell As Range, rng As Range
    
    For Each cell In Worksheets("Calculate").Range("A5:A10")
        If InStr(1, cell.Text, "1", 1) Then
            If rng Is Nothing Then
                Set rng = cell.Offset(, 1).Resize(, 13)
            Else
                Set rng = Union(rng, cell.Offset(, 1).Resize(, 13))
            End If
        End If
    Next cell
    
    rng.Copy Destination:=Sheets("Results").Range("A" & Rows.Count).End(xlUp).Offset(1)
    MsgBox "Data copied."

End Sub
 
Upvote 0
Thanks guys for the code.

Unfortunatley, the first one generates a run time error 424 stating object required. Debugging takes me to the following line:

If Left(.Cells(i, 1), 1).Value = 1 Then


The second code (copyranges2) generates a run time error 91 saying object variable or with block variable not set. This code actually pastes a few lines into the results sheets before generating the error but it does not look anything like the data I was eexpecting!

Any ideas what's going on?

Elvis
 
Upvote 0

Forum statistics

Threads
1,203,621
Messages
6,056,337
Members
444,861
Latest member
B4you_Andrea

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