VBA Help

danny8890

New Member
Joined
Feb 7, 2018
Messages
46
Hi Guys,

Could you assist me with the below please,
I want to just copy rows A:F when column A contains the word SSN. at the moment it copys the entire row, cannot figure this one out.

Private Sub Workbook_Open2()


Dim i, LastRow


LastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Sheets("Sheet1").Cells(i, "A").Value Like "*SIM*" Then
Sheets("Sheet1").Cells(i, "A").EntireRow.Copy Destination:=Sheets("SIM").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next i
For i = 2 To LastRow
If Sheets("Sheet1").Cells(i, "A").Value Like "*SSN*" Then
Sheets("Sheet1").Cells(i, "A").EntireRow.Copy Destination:=Sheets("SIM").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next i


End Sub

Thanks
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
try this:
Code:
Private Sub Workbook_Open2()



Dim i, LastRow




LastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Sheets("Sheet1").Cells(i, "A").Value Like "*SIM*" Then
Sheets("Sheet1").Range(Cells(i, 1), Cells(i, 6)).Copy Destination:=Sheets("SIM").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next i
For i = 2 To LastRow
If Sheets("Sheet1").Cells(i, "A").Value Like "*SSN*" Then
Sheets("Sheet1").Range(Cells(i, 1), Cells(i, 6)).Copy Destination:=Sheets("SIM").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next i




End Sub
 
Upvote 0
try this:
Code:
Private Sub Workbook_Open2()



Dim i, LastRow




LastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Sheets("Sheet1").Cells(i, "A").Value Like "*SIM*" Then
Sheets("Sheet1").Range(Cells(i, 1), Cells(i, 6)).Copy Destination:=Sheets("SIM").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next i
For i = 2 To LastRow
If Sheets("Sheet1").Cells(i, "A").Value Like "*SSN*" Then
Sheets("Sheet1").Range(Cells(i, 1), Cells(i, 6)).Copy Destination:=Sheets("SIM").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next i




End Sub
Hi, thanks for quick response, i have tried this and getting error message "run time error 1004" which is what i keep getting when try to get it to work.
 
Upvote 0
Hi & welcome to the board.
How about
Code:
Private Sub Workbook_Open2()

   Dim i, LastRow
   
   LastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
   For i = 2 To LastRow
      If Sheets("Sheet1").Cells(i, "A").Value Like "*SIM*" Then
         Sheets("Sheet1").Range("A" & i).Resize(, 6).Copy Destination:=Sheets("SIM").Range("A" & Rows.Count).End(xlUp).Offset(1)
      End If
   Next i
   For i = 2 To LastRow
      If Sheets("Sheet1").Cells(i, "A").Value Like "*SSN*" Then
         Sheets("Sheet1").Range("A" & i).Resize(, 6).Copy Destination:=Sheets("SIM").Range("A" & Rows.Count).End(xlUp).Offset(1)
      End If
   Next i

End Sub
If it doesn't matter if the SSn & SIM are mixed up, another option would be
Code:
Private Sub Workbook_Open2()

   Dim i, LastRow
   
   With Sheets("Sheet1")
      LastRow = .Range("A" & Rows.Count).End(xlUp).Row
      For i = 2 To LastRow
         If .Cells(i, "A").Value Like "*SIM*" Or Cells(i, "A").Value Like "*SSN*" Then
            .Range("A" & i).Resize(, 6).Copy Destination:=Sheets("SIM").Range("A" & Rows.Count).End(xlUp).Offset(1)
         End If
      Next i
   End With

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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