I have checked a bunch of different posts and can't seem to find the exact code I am looking for. Also I have never used VBA before so I'm trying to take codes from other posts and input my info for it to work. No luck yet. At work we have a payroll system in Excel. I am trying to search for my name "Clarke, Matthew"
and then copy that row and paste it to the workbook I have saved on my desktop "Total hours"
.
Asked
Active
Viewed 3.0k times
19

Andrei Konstantinov
- 6,971
- 4
- 41
- 57

user1548751
- 191
- 1
- 1
- 3
-
3If you are searching for the keyword on a single column (for example, all "`Clarke, Matthew`" are on column A), then the filter feature of Excel should work. – timrau Jul 24 '12 at 13:02
-
2Perhaps posting what you've got so for would give us a better place to start helping. You might also look into the `VLOOKUP` function. – Rob I Jul 24 '12 at 13:16
-
See this http://stackoverflow.com/questions/10319096/error-when-i-use-specialcells-of-autofilter-to-get-visible-cells-in-vba/10319230#10319230 modify it to suit your needs :) – Siddharth Rout Jul 24 '12 at 13:34
-
1Or this http://stackoverflow.com/questions/11317172/delete-row-based-on-condition/11317372#11317372 – Siddharth Rout Jul 24 '12 at 13:35
-
1@SiddharthRout Your answer to this very question worked for me. From Google to working macro in 2 minutes, your links notwithstanding! – Jamie Jan 24 '14 at 03:17
3 Answers
23
CODE
Sub Sample()
Dim wb1 As Workbook, wb2 As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim copyFrom As Range
Dim lRow As Long '<~~ Not Integer. Might give you error in higher versions of excel
Dim strSearch As String
Set wb1 = ThisWorkbook
Set ws1 = wb1.Worksheets("yourSheetName")
strSearch = "Clarke, Matthew"
With ws1
'~~> Remove any filters
.AutoFilterMode = False
'~~> I am assuming that the names are in Col A
'~~> if not then change A below to whatever column letter
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
With .Range("A1:A" & lRow)
.AutoFilter Field:=1, Criteria1:="=*" & strSearch & "*"
Set copyFrom = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow
End With
'~~> Remove any filters
.AutoFilterMode = False
End With
'~~> Destination File
Set wb2 = Application.Workbooks.Open("C:\Sample.xlsx")
Set ws2 = wb2.Worksheets("Sheet1")
With ws2
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
lRow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
lRow = 1
End If
copyFrom.Copy .Rows(lRow)
End With
wb2.Save
wb2.Close
End Sub
SNAPSHOT

Nimantha
- 6,405
- 6
- 28
- 69

Siddharth Rout
- 147,039
- 17
- 206
- 250
-
TY for all the feed back. I am tryng to use that second macro but am now recieving an error message on this line .AutoFilter Field:=1, Criteria1:="=*" & strSearch & "*" .... Tells me 'Run time error 1004: Autofilter method of range failed'. Any suggestions? – user1548751 Jul 27 '12 at 13:36
-
-
I had left that the same, than i tried A1:A50 assuming it meant the range from row 1 - 50 in column A. Still no luck.. I just realized that this sheet is protected and now when i try to run the macro on that sheet it says i cant. Is there another way around this? im not sure that these sheets will allow for the use of macros – user1548751 Jul 27 '12 at 14:00
-
How would i go about sending it or posting it? doubt i can from these computers. Also i ran the macro using a unprotected macro-enabled file and it did work. when i try to use the same macro on the other files that are protected i get an error that i need to unprotect it to do it. – user1548751 Jul 27 '12 at 14:51
-
-
No i dont its a payroll sheet that we input our information onto and i just wanted to keep my own record on my own sheet by using a macro. i can always just copy and paste instead. TY for all the help though. – user1548751 Jul 27 '12 at 15:22
-
-
Very instructive. However, I'm finding that if there's existing data in the destination sheet, the last row is overwritten with the new data. I'm wondering if `lRow = .Cells.Find(What:="*", _ ...` should be `lRow = .Cells.Find(What:="*", _...) + 1`. Thanks. – DaveU Dec 20 '13 at 04:24
2
Expanding on what timrau said in his comment, you can use the AutoFilter function to find the row with your name in it. (Note that I'm assuming you have the source workbook open)
Dim curBook As Workbook
Dim targetBook As Workbook
Dim curSheet As Worksheet
Dim targetSheet As Worksheet
Dim lastRow As Integer
Set curBook = ActiveWorkbook
Set curSheet = curBook.Worksheets("yourSheetName")
'change the Field number to the correct column
curSheet.Cells.AutoFilter Field:=1, Criteria1:="Clarke, Matthew"
'The Offset is to remove the header row from the copy
curSheet.AutoFilter.Range.Offset(1).Copy
curSheet.ShowAllData
Set targetBook = Application.Workbooks.Open "PathTo Total Hours"
Set targetSheet = targetBook.WorkSheet("DestinationSheet")
lastRow = Cells.Find(What:="*", After:=Range("A1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row
targetSheet.Cells(lastRow + 1, 1).PasteSpecial
targetBook.Save
targetBook.Close
As you can see I put placeholders in for the specific setup of your workbook.

Jon Kelly
- 316
- 1
- 4
-
`ActiveSheet.AutoFilter.Range.Offset(1).Copy ` This is an incorrect way to do it :) Please see the two links that I posted in the comment. – Siddharth Rout Jul 24 '12 at 14:17
-
@Siddharth I have found that the `AutoFilter.Range` works fine. `SpecialCells(xlCellTypeVisible)` should work too, but I've had issues with it returning blank cells as well. – Jon Kelly Jul 24 '12 at 14:27
1
I know this is old, but for anyone else searching for how to do this, it can be done in a much more direct fashion:
Public Sub ExportRow()
Dim v
Const KEY = "Clarke, Matthew"
Const WS = "Sheet1"
Const OUTPUT = "c:\totalhours.xlsx"
Const OUTPUT_WS = "Sheet1"
v = ThisWorkbook.Sheets(WS).Evaluate("index(a:xfd,match(""" & KEY & """,a:a,),)")
With Workbooks.Open(OUTPUT).Sheets(OUTPUT_WS)
.[1:1].Offset(.[counta(a:a)]) = v
.Parent.Save: .Parent.Close
End With
End Sub

Excel Hero
- 14,253
- 4
- 33
- 40