0

I have a spreadsheet that consists of columns from A-P and lines 1 to 2016 (and still growing). I am looking for an easy way to search the spreadsheet for a specific word, for example "Gap", and have the lines that contain the word "gap" copied to Sheet2. I would like if it could use a box where I put the word in, so I can search for different things as needed.

I don't expect the spreadsheet to get any smaller (this is an action item list, I need it to search until it reaches a blank line).

How can I do this?

KatieK
  • 13,586
  • 17
  • 76
  • 90
jennifer
  • 1
  • 1
  • 1

1 Answers1

1
'all variables must be declared    
Option Explicit
Sub CopyData()

'this variable holds a search phrase, declared as variant as it might be text or number
Dim vSearch As Variant
'these three variables are declared as long, technically the loop might exceed 32k (integer) therefore it is safer to use long
Dim i As Long
Dim k As Long
Dim lRowToCopy As Long

'the macro prompts a user to enter the search phrase
vSearch = InputBox("Search")
'varialbe i initially declared as 1 - macro starts calculations from the 1st row
i = 1
'macro will loop until it finds a row with no records
'I called a standard XLS function COUNTA to count the number of non-blank cells
'if the macro finds a row with no records it quits the loop
Do Until WorksheetFunction.CountA(Sheets("Main").Rows(i)) = 0

 'here I let the macro to continue its run despite a possible errors (explanation below)
  On Error Resume Next
  lRowToCopy = 0
 'if Find method finds no value VBA returns an error, this is why I allowed macro to run despite that. In case of error variable lRowToCopy keeps 0 value
 'if Find method finds a searched value it assigns the row number to var lRowToCopy
  lRowToCopy = Sheets("Main").Rows(i).Find(What:=vSearch, LookIn:=xlValues,     LookAt:=xlPart, SearchOrder:=xlByRows).Row
 'here we allow macro to disiplay error messages
  On Error GoTo 0

 'if var lRowToCopy does not equal to 0 that means a row with a searched value has been found
  If lRowToCopy > 0 Then

    'this loop looks for the first blank row in 2nd sheet, I also used COUNTA to find absolutely empty row
    For k = 1 To Sheets("ToCopy").Rows.Count

      'when the row is found, the macro performs copy-paste operation
      If WorksheetFunction.CountA(Sheets("ToCopy").Rows(k)) = 0 Then

          Sheets("Main").Rows(i).Copy
          Sheets("ToCopy").Select
          Rows(k).Select
          ActiveSheet.Paste
          'do not forget to exit for loop as it will fill all empty rows in 2nd sheet
          Exit For

      End If
    Next k

  End If

i = i + 1
Loop

End Sub
  • Your code would be great with some comments and explanation on how to use this so that the OP (who doesn't seem to know VBA well) would know where to start – JMax May 15 '12 at 06:40
  • Ok, I will rebuild it when I come back home, now I am at work :D – Marcin Janowski May 15 '12 at 09:24
  • @MarcinJanowski I think JMax meant that your answer should include an explanation (an "answer" to the question), rather than just code. – yoozer8 May 15 '12 at 19:21
  • Do you mean I should add a 2nd answer with the code explanations? I know that the code does not look well now but I think it is easier to understand it following each step. – Marcin Janowski May 15 '12 at 19:29
  • Hi Marcin,I appreciate your help very much! – jennifer May 15 '12 at 20:00
  • I am getting a run-time error'9' subscript out of range....and I have no idea what that means or how to fix it if i've done something wrong. When I click on the debug button it highlights the following in yellow: Do Until WorksheetFunction.CountA(Sheets("Main").Rows(i)) = 0 – jennifer May 15 '12 at 20:02
  • This is because I named my sheets as "Main" and "ToCopy" and your sheets have different names :) – Marcin Janowski May 15 '12 at 20:14
  • I just had a chance to try it and it works fantastic!!!Thank You very much! I cant wait to learn more about VBA so I can do these magical things in excel too!! – jennifer May 16 '12 at 19:54
  • Marcin, If i would want your macro just to search in column B, would it read something like Do Until Worksheet Function.CountA(Sheets("Main").Range("B:B"),.Rows(i))=0 –  Oct 24 '12 at 03:12
  • Marcin, let me first thank you for this script. But i am getting a problem. When i run this macro, the excel hangs and does not repond. however i get the the result when i forcefully restart the sheet. would it because of huge data or is it getting into Never ending loop? Please let me know. Please.. –  Sep 06 '13 at 05:56