+ Reply to Thread
Results 1 to 6 of 6

Copy a line with a specific key word into another sheet.

Hybrid View

  1. #1
    Registered User
    Join Date
    08-22-2013
    Location
    michigan, USA
    MS-Off Ver
    Excel 2010
    Posts
    20

    Wink Copy a line with a specific key word into another sheet.

    Hello all,
    I have been working on a worksheet that I will use as a log, and to complete it I would like to copy a line with a specific key word into the corresponding sheet.

    I have 5 sheets in my workbook (the main database named "log", "Bill", "Tim", "Marty", and "Dave").
    When their name appears in the engineer column which is column D, I would like it to automatically copy the line into their respective sheets.

    How can I set this up?

    Many thanks in advance!
    Nikki

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Copy a line with a specific key word into another sheet.

    Sub a()
    sharr = Array("Bill", "Tim", "Marty", "Dave")
    With Sheets("Log")
      LR = .Cells(.Rows.Count, "D").End(xlUp).Row
      For j = 1 To LR
        eng = .Range("D" & j)
        sh = ""
        For e = 0 To 3
          If InStr(eng, sharr(e)) > 0 Then
            sh = sharr(e)
            Exit For
          End If
        Next
        If sh <> "" Then
          LRs = Sheets(sh).Cells(Rows.Count, "A").End(xlUp).Row + 1
          Rows(j).Copy Sheets(sh).Cells(LRs, 1)
        End If
      Next
    End With
    End Sub
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    08-22-2013
    Location
    michigan, USA
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Copy a line with a specific key word into another sheet.

    Thank you so much patel45 you are my hero!!! It works perfectly!

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Copy a line with a specific key word into another sheet.

    Hi, kkihead,

    maybe like this:
    Sub EF949541()
    Dim lngCounter As Long
    
    With Sheets("Log")
      For lngCounter = 1 To .Cells(.Rows.Count, "D").End(xlUp).Row
        Select Case .Range("D" & lngCounter)
          Case "Bill", "Tim", "Marty", "Dave"
            .Rows(lngCounter).Copy Sheets(sh).Cells(Sheets(sh).Range("A" & Rows.Count).End(xlUp).Row + 1, 1)
          Case Else
        End Select
      Next lngCounter
    End With
    
    End Sub
    @patel:
    should it be
    .Rows(j).Copy Sheets(sh).Cells(LRs, 1)
    as this line is within the With-Statement?
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  5. #5
    Registered User
    Join Date
    08-22-2013
    Location
    michigan, USA
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Copy a line with a specific key word into another sheet.

    Hello HaHoBe,
    I tried yours and it said script out of range. Was I meant to change any of the values?

    patel45's worked great but I am wondering if there is a way for the sheets to update automatically as something is entered?

    Anytime I change anything in the log, I will have to delete what is in each sheet and run the macros again for them to be updated without a bunch of duplicate lines.

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Copy a line with a specific key word into another sheet.

    Hi, kkihead,

    regarding the run-time error (my bad I didnīt update the code accordingly):
    Sub EF949541_2()
    Dim lngCounter As Long
    
    With Sheets("Log")
      For lngCounter = .Cells(.Rows.Count, "D").End(xlUp).Row To 1 Step -1
        Select Case .Range("D" & lngCounter)
          Case "Bill", "Tim", "Marty", "Dave"
            .Rows(lngCounter).Copy Sheets(.Range("D" & lngCounter).Value).Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
            .Rows(lngCounter).Delete
          Case Else
        End Select
      Next lngCounter
    End With
    
    End Sub
    This code will take away the line from sheet Log. The other way is (as you mentioned) clear contents on all named sheets prior to running the macro, I wouldnīt take the Workjsheet_Change-event to trigger the macro after a change but use the Worksheet_Deactivate - you would need to activate any other sheet to view what has been copied.

    Ciao,
    Holger
    Last edited by HaHoBe; 08-22-2013 at 01:19 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Copy table row to another sheet when a specific cell contains the right word
    By robclark89 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-17-2013, 09:53 PM
  2. [SOLVED] Remove a specific word line from a word document.
    By Will_2013 in forum Word Formatting & General
    Replies: 4
    Last Post: 05-26-2013, 07:01 PM
  3. Replies: 1
    Last Post: 04-05-2013, 03:40 PM
  4. Excel VBA copy specific lines from a Word document (3rd line from top of page)
    By Excelebrate in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-25-2012, 06:02 AM
  5. Search word and copy line from multiple sheet to single sheet
    By drtorn in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-23-2012, 01:35 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1