Copy Rows to new sheet based if value in column E exist anywhere in Column A

KCPrescott

New Member
Joined
May 5, 2016
Messages
2
I have excel file with several hundred entries.
Sheet 1 contains the main list of all the Companies formatted by:
Column A=User ID#
Column B=Company

In Column E there is a list of selected companies noted by their ID#

User IDCompanyCountrySelected User ID
1Company1US3
2Company2China5
3Company3Canada7
4Company4US19
5Company5US44
6Company6US65

<tbody>
</tbody>

I need a macro that will search Column A for all variables in Column E and then copy that row to Sheet 2.

So from the sample above Sheet 2 would look like:

User IdCompanyCountrySelected User ID
3Company3Canada7
5Comany5US44

<tbody>
</tbody>

I tried to modify this code:

Sub copyrows()

Dim tfCol As Range, Cell As Object

Set tfCol = Range("A2:A500") 'Substitute with the range which includes your True/False values

For Each Cell In tfCol

If IsEmpty(Cell) Then
Exit Sub
End If

If Cell.Value = ("3") Then 'Substitute with the Selected User ID
Cell.EntireRow.Copy
Sheet2.Select 'Substitute with your sheet
ActiveSheet.Range("A65536").End(xlUp).Select
Selection.Offset(1, 0).Select
ActiveSheet.Paste
End If

Next

End Sub


However this only lets me put in one value at a time. Is there a way to have it automatically use all the values from a specific column or to enter all the values into the code at one time?

Like: If Cell.Value = ("Search Column E for all values") Then
(Replacing the text in red for actual code)

Or: If Cell.Value = ("3" "5" "7"..."65") Then
(Replacing the text in red for actual code)

It seems like it should be feasible but I am new to excel and how to go about using macros and formulas. Please help.
 

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.
Try:
Code:
Sub CopyRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Sheets("Sheet1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim ID As Range
    Dim foundID As Range
    For Each ID In Sheets("Sheet1").Range("A2:A" & LastRow)
        Set foundID = Sheets("Sheet1").Range("E:E").Find(ID, LookIn:=xlValues, lookat:=xlWhole)
        If Not foundID Is Nothing Then
            ID.EntireRow.Copy Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
        End If
    Next ID
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Try this:
Code:
Sub Copy_My_Rows()
Application.ScreenUpdating = False
Dim Arr() As Variant
Dim Lastrow As Long
Dim Lastrowa As Long
Lastrow = Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row
Lastrowa = Cells(Rows.Count, "E").End(xlUp).Row
Lastrowb = Sheets(2).Cells(Rows.Count, "A").End(xlUp).Row + 1

Arr = Range("E1:E" & Lastrowa)
Dim R As Long
Dim C As Long
Dim i As Long

    For i = 1 To Lastrow

    For R = 1 To UBound(Arr, 1)  ' First array dimension is rows.
        
        For C = 1 To UBound(Arr, 2) ' Second array dimension is columns.
            
            If Cells(i, 1).Value = Arr(R, C) Then
                Rows(i).Copy Destination:=Sheets(2).Rows(Lastrowb)
                Lastrowb = Lastrowb + 1
           
           End If
    Next C
Next R
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try
Code:
Option Explicit

Sub Treat()
Dim WkRg  As Range
Dim Rg  As Range
Dim Temp
   Set WkRg = Sheet1.Cells(Rows.Count, 1).End(3).Offset(1, 0)
   For Each Rg In Sheet1.UsedRange.Offset(1, 0).Columns(1).Cells
      Temp = Evaluate("NOT(ISERROR(MATCH(" & Rg.Value & ",Sheet1!E:E,0)))")
      If (Temp) Then Set WkRg = Union(WkRg, Rg)
   Next
   Sheet2.Cells.ClearContents
   WkRg.EntireRow.Copy Destination:=Sheet2.Cells(1, 1)
End Sub
 
Upvote 0
Glad I could help and thanks for the rep. :)
 
Upvote 0

Forum statistics

Threads
1,215,426
Messages
6,124,829
Members
449,190
Latest member
rscraig11

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