Results 1 to 4 of 4

Thread: Vba Code to find value and paste on certain row

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Vba Code to find value and paste on certain row

    I'm am very new to vba and I have a excel sheet that has roughly 200 names, agent id and their role. I am trying to write a vba code to search for a Name via input box and paste All occurance of that name at the top of the sheet starting at row 3 and then insert a black line between the search data and existing data. Example I input "Jen" and copy every row that "Jen" occurs and inserts a row at row 3 and inserts the copied data moving existing data down. I have included what i have so far.I have pieced it together from codes that i have found online. I don't think it is totally correct. As my cancel button doesn't work and if the person searching leave it empty it still tries to copy data. Any help would be grateful.


    Code:
    Dim LSearchValue As String
    Dim RefCount As Integer
    
    Application.ScreenUpdating = False
    
    LSearchValue = InputBox("Who do you want.", "Enter name") & "*"
    
    Rows("3:3").Select
    Selection.Insert
    
    RefCount = Application.WorksheetFunction.CountIf(Worksheets("agent").Range("A:C"), LSearchValue) - 1
    
    Range("A2").Activate
    
    For Counter = 0 To RefCount Step 1
    
    With Columns("A:C").Find(What:=LSearchValue, After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    
    Rows("3:3").Select
    Selection.Insert
    
    .EntireRow.Copy Destination:=Worksheets("Agent").Range("a3")
    .Activate
    
       End With
    Next
    
    
    Application.ScreenUpdating = True
    
    Application.Goto Reference:=Range("a1"), Scroll:=True
    Attached Files Attached Files

  2. #2
    Senior Member
    Join Date
    Jun 2012
    Posts
    337
    Rep Power
    13
    Why don't you use the autofilter ?

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0
    Ok I could do that if it is easier, can you share how that would work and code.... Like I said very new to this.

  4. #4
    Senior Member
    Join Date
    Jun 2012
    Posts
    337
    Rep Power
    13
    You can perform any search in this or similars forums using 'autfilter' to look for.

Similar Threads

  1. VBA code to move row to new spreadsheet
    By cdurfey in forum Excel Help
    Replies: 6
    Last Post: 06-10-2013, 10:38 PM
  2. Replies: 7
    Last Post: 05-17-2013, 10:38 PM
  3. Find the last row in a range
    By PcMax in forum Excel Help
    Replies: 7
    Last Post: 03-22-2013, 03:46 AM
  4. Replies: 2
    Last Post: 02-11-2013, 08:13 PM
  5. Find Last Used Row In Column
    By Rasm in forum Excel and VBA Tips and Tricks
    Replies: 2
    Last Post: 06-04-2012, 07:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •