Copy part of a range with For each Loop ?

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
788
Office Version
  1. 365
Platform
  1. Windows
Hi,
Unsure of best way to achieve this but I'm thinking with a for each loop.

Copy each row in Range(A10:H5000) if value in Column P is 0 to a new sheet

Example if just P22, P36, P80:P100 contain a 0 then copy:

A22:H22
A36:H36
A80:H100

Thanks for any help
 
Last edited:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
This one might not be the most efficient way to do it but gets the job done:
Code:
Sub GetZeroRows()

Dim ZeroRows As Range
Dim MyTable As Range
Dim MyRange As Range
Dim NewSheet As Worksheet
Dim c As Range


With Application
    .ScreenUpdating = False 'Makes the macro run faster
    .EnableEvents = False   'Disables event macros
End With


With Sheet1 'Sets the sheet where the original values are found
    Set MyTable = .Range("A1:H5000")    'Sets the table to copy values from
    Set MyRange = Intersect(MyTable.EntireRow, .Range("P:P"))   'Sets the range to look the values from


    Set ZeroRows = Find_Range(0, MyRange, xlValues, xlWhole, False) 'Looks for matching values


    If Not ZeroRows Is Nothing Then
        Set NewSheet = Worksheets.Add(After:=Sheets(Sheets.Count))  'Adds a new sheet
        
        With NewSheet
            'Writes the table headers:
            .Cells(.Rows.Count, 1).End(xlUp).Resize(1, MyTable.Columns.Count).Value = MyTable.Rows(1).Value
            
            'Loops through the ZeroRows to write the values
            For Each c In ZeroRows
                .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Resize(1, MyTable.Columns.Count).Value = Intersect(c.EntireRow, MyTable).Value
            Next c
        End With
        
    End If


End With


'Restore original settings:
With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With


End Sub




Function Find_Range(Find_Item As Variant, _
    Search_Range As Range, _
    Optional LookIn As Variant, _
    Optional LookAt As Variant, _
    Optional MatchCase As Boolean) As Range
     
    Dim c As Range
    If IsMissing(LookIn) Then LookIn = xlValues 'xlFormulas
    If IsMissing(LookAt) Then LookAt = xlPart 'xlWhole
    If IsMissing(MatchCase) Then MatchCase = False
     
    With Search_Range
        Set c = .Find( _
        What:=Find_Item, _
        LookIn:=LookIn, _
        LookAt:=LookAt, _
        SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, _
        MatchCase:=MatchCase, _
        SearchFormat:=False)
        If Not c Is Nothing Then
            Set Find_Range = c
            firstAddress = c.Address
            Do
                Set Find_Range = Union(Find_Range, c)
                Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> firstAddress
        End If
    End With
     
End Function

The code uses the Kickbutt VBA Find function from Ozgrid.com
 
Upvote 0
This one might not be the most efficient way to do it but gets the job done:
Code:
Sub GetZeroRows()

Dim ZeroRows As Range
Dim MyTable As Range
Dim MyRange As Range
Dim NewSheet As Worksheet
Dim c As Range


With Application
    .ScreenUpdating = False 'Makes the macro run faster
    .EnableEvents = False   'Disables event macros
End With


With Sheet1 'Sets the sheet where the original values are found
    Set MyTable = .Range("A1:H5000")    'Sets the table to copy values from
    Set MyRange = Intersect(MyTable.EntireRow, .Range("P:P"))   'Sets the range to look the values from


    Set ZeroRows = Find_Range(0, MyRange, xlValues, xlWhole, False) 'Looks for matching values


    If Not ZeroRows Is Nothing Then
        Set NewSheet = Worksheets.Add(After:=Sheets(Sheets.Count))  'Adds a new sheet
        
        With NewSheet
            'Writes the table headers:
            .Cells(.Rows.Count, 1).End(xlUp).Resize(1, MyTable.Columns.Count).Value = MyTable.Rows(1).Value
            
            'Loops through the ZeroRows to write the values
            For Each c In ZeroRows
                .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Resize(1, MyTable.Columns.Count).Value = Intersect(c.EntireRow, MyTable).Value
            Next c
        End With
        
    End If


End With


'Restore original settings:
With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With


End Sub




Function Find_Range(Find_Item As Variant, _
    Search_Range As Range, _
    Optional LookIn As Variant, _
    Optional LookAt As Variant, _
    Optional MatchCase As Boolean) As Range
     
    Dim c As Range
    If IsMissing(LookIn) Then LookIn = xlValues 'xlFormulas
    If IsMissing(LookAt) Then LookAt = xlPart 'xlWhole
    If IsMissing(MatchCase) Then MatchCase = False
     
    With Search_Range
        Set c = .Find( _
        What:=Find_Item, _
        LookIn:=LookIn, _
        LookAt:=LookAt, _
        SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, _
        MatchCase:=MatchCase, _
        SearchFormat:=False)
        If Not c Is Nothing Then
            Set Find_Range = c
            firstAddress = c.Address
            Do
                Set Find_Range = Union(Find_Range, c)
                Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> firstAddress
        End If
    End With
     
End Function

The code uses the Kickbutt VBA Find function from Ozgrid.com

thanks its perfect and works instantly
modified slightly to work with a set sheet instead of adding new
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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