+ Reply to Thread
Results 1 to 19 of 19

[SOLVED] Need Excel Code to copy one row to another sheet if row contains certian TEXT

Hybrid View

Drscott21 [SOLVED] Need Excel Code to... 03-22-2016, 01:23 PM
JOHN H. DAVIS Re: PLEASE HELP! Need Excel... 03-22-2016, 01:54 PM
Drscott21 Re: PLEASE HELP! Need Excel... 03-22-2016, 02:26 PM
JOHN H. DAVIS Re: PLEASE HELP! Need Excel... 03-22-2016, 02:35 PM
Drscott21 Re: PLEASE HELP! Need Excel... 03-22-2016, 02:41 PM
JOHN H. DAVIS Re: PLEASE HELP! Need Excel... 03-22-2016, 02:48 PM
Drscott21 Re: PLEASE HELP! Need Excel... 03-22-2016, 03:08 PM
bbrock316 Re: PLEASE HELP! Need Excel... 03-22-2016, 02:59 PM
Winon Re: PLEASE HELP! Need Excel... 03-22-2016, 03:32 PM
Drscott21 Re: PLEASE HELP! Need Excel... 03-22-2016, 03:34 PM
jaslake Re: PLEASE HELP! Need Excel... 03-22-2016, 06:13 PM
jaslake Re: PLEASE HELP! Need Excel... 03-22-2016, 07:14 PM
Drscott21 Re: PLEASE HELP! Need Excel... 03-23-2016, 08:17 AM
  1. #1
    Registered User
    Join Date
    03-22-2016
    Location
    United States, NC
    MS-Off Ver
    2010
    Posts
    40

    Thumbs up [SOLVED] Need Excel Code to copy one row to another sheet if row contains certian TEXT

    I am trying to figure out how to copy Rows Containing 2-3 Surg or 2-3 Sicu on Sheet named Admissions, and have it automatically paste the entire row on sheet named Surg Adm. I am going crazy over this I have tried multiple solutions that I found when I googled it. But nothing is working. I have uploaded the workbook so you can see what I am working with.
    Attached Files Attached Files
    Last edited by Drscott21; 04-08-2016 at 09:00 AM.

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: PLEASE HELP! Need Excel Code to copy one row to another sheet if row contains certian

    Unfortunately I couldn't access your attachment. However, maybe this will help. Note it assumes the text strings are in Column "A".

    Sub Drscott21()
    Dim i As Long, ws As Worksheet, ws2 As Worksheet
    Set ws = Sheets("Admissions")
    Set ws2 = Sheets("Surg Adm")
    With ws
        For i = 2 To .Range("A" & Rows.Count).End(3).row
            If .Cells(i, "A") Like "*2-3 Surg*" Or .Cells(i, "A") Like "*2-3 Sicu*" Then
                .Rows(i).Copy ws2.Range("A" & Rows.Count).End(3)(2)
            End If
        Next i
    End With
    End Sub

  3. #3
    Registered User
    Join Date
    03-22-2016
    Location
    United States, NC
    MS-Off Ver
    2010
    Posts
    40

    Re: PLEASE HELP! Need Excel Code to copy one row to another sheet if row contains certian

    Thanks, unfortunately it didn't work the text strings are in Column "D". I tried to change the "A" to "D" and it didn't work. I need to copy the entire and then paste it on the sheet named "Surg Adm".

    I Attached the file again you have to save it first and then try to open it.
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: PLEASE HELP! Need Excel Code to copy one row to another sheet if row contains certian

    This works for me on your sample.

    Sub Drscott21()
    Dim i As Long, ws As Worksheet, ws2 As Worksheet
    Set ws = Sheets("Admission")
    Set ws2 = Sheets("Surg Adm")
    With ws
        For i = 2 To .Range("D" & Rows.Count).End(3).row
            If .Cells(i, "D") Like "*2-3 surg*" Or .Cells(i, "D") Like "*2-3 sicu*" Then
                .Rows(i).Copy ws2.Range("A" & Rows.Count).End(3)(2)
            End If
        Next i
    End With
    End Sub

  5. #5
    Registered User
    Join Date
    03-22-2016
    Location
    United States, NC
    MS-Off Ver
    2010
    Posts
    40

    Re: PLEASE HELP! Need Excel Code to copy one row to another sheet if row contains certian

    That does seem to work but how can I make it copy and then paste the all of the rows that contain "2-3 Surg" or "2-3 Sicu"? Because it is currently only copying and then pasting one row.

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: PLEASE HELP! Need Excel Code to copy one row to another sheet if row contains certian

    When I tested on your sample there were 12 rows meeting your criteria. They all were copied. Not sure what you mean when you say only one row was pasted?

  7. #7
    Registered User
    Join Date
    03-22-2016
    Location
    United States, NC
    MS-Off Ver
    2010
    Posts
    40

    Re: PLEASE HELP! Need Excel Code to copy one row to another sheet if row contains certian

    When I run the Marco it is only copying one row and pasting it on the Sheet named "Surg Adm".

    See Attachement.
    Attached Images Attached Images

  8. #8
    Registered User
    Join Date
    03-22-2016
    Location
    Cleveland, OH
    MS-Off Ver
    Office 2010
    Posts
    5

    Re: PLEASE HELP! Need Excel Code to copy one row to another sheet if row contains certian

    Watch this, I believe the code is a bit different, but it should do what you want.

    https://www.youtube.com/watch?v=fxgQm32sBeI

  9. #9
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: PLEASE HELP! Need Excel Code to copy one row to another sheet if row contains certian

    Hello Drscott21,

    Which version of Excel are you actually using, since the uploaded sample Workbook is in Excel 2003 format, and not Excel 2007 or later, which makes a big difference. Your profile shows that you are using Excel 2010. Please try the code provided on that platform.

    Regards.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  10. #10
    Registered User
    Join Date
    03-22-2016
    Location
    United States, NC
    MS-Off Ver
    2010
    Posts
    40

    Re: PLEASE HELP! Need Excel Code to copy one row to another sheet if row contains certian

    Hello Winon,
    I am using Excel 2010. I had to upload an older version so those helping me could see it.

  11. #11
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: PLEASE HELP! Need Excel Code to copy one row to another sheet if row contains certian

    Hi Drscott21

    In your Thread you've dealt with...
    Rows Containing 2-3 Surg or 2-3 Sicu
    Where do the other items go? Let's deal with all of them...tell me where and we'll put them there.

    Row\Col
    A
    B
    C
    1
    Service Go to WS???
    2
    N42-1B
    3
    2-3 surg Goes to Surg Adm
    4
    43-1
    5
    8-1
    6
    2-3 med
    7
    2-3 micu
    8
    4-4 sarrtp
    9
    2-3 sicu Goes to Surg Adm
    10
    Ed med
    11
    N42-2C
    Last edited by jaslake; 03-22-2016 at 07:22 PM.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  12. #12
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: PLEASE HELP! Need Excel Code to copy one row to another sheet if row contains certian

    Hi Drscott21

    This Code in the attached appears to work on your Sample Data...CTRL + x will fire the Code...however, as I suggested in my previous Post...
    Where do the other items go? Let's deal with all of them...tell me where and we'll put them there...
    Option Explicit
    
    Sub Split_Service()
       Dim ws           As Worksheet
       Dim ws1          As Worksheet
       Dim LR           As Long
    
       Application.ScreenUpdating = False
       Set ws = Sheets("Admission")
       Set ws1 = Sheets("Surg Adm")
       With ws1
          .UsedRange.Offset(1, 0).ClearContents
       End With
    
       With ws
          LR = .Range("D" & .Rows.Count).End(xlUp).Row
          .Range("A1:P" & LR).AutoFilter Field:=4, Criteria1:= _
                                         "=2-3 sicu", Operator:=xlOr, Criteria2:="=2-3 surg"
          .Range(.Cells(2, 1), .Cells(LR, "P")).SpecialCells(xlCellTypeVisible).EntireRow.Copy
          ws1.Range("A2").PasteSpecial (xlPasteColumnWidths)
          ws1.Range("A2").PasteSpecial (xlPasteValues)
          .AutoFilterMode = False
       End With
       Application.CutCopyMode = False
       Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    Last edited by jaslake; 03-22-2016 at 07:25 PM.

  13. #13
    Registered User
    Join Date
    03-22-2016
    Location
    United States, NC
    MS-Off Ver
    2010
    Posts
    40

    Re: PLEASE HELP! Need Excel Code to copy one row to another sheet if row contains certian

    The other items need to go to the appropriate worksheet. "Service" is a header so that is already on each worksheet. 8-1, 8-3, Ed Psyc, and 4-4 sarrtp need to go to worksheet named "Psy Adm". 2-3 med, 2-3 micu and ed med need to go to worksheet named "Med Adm". Lastly, N42-2C, N42-1B and 43-1 need to go to worksheet named "Gec Adm".

    Thank You for you help I greatly appreciate it.

  14. #14
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: PLEASE HELP! Need Excel Code to copy one row to another sheet if row contains certian

    Hi Drscott21

    Try this Code in the attached...CTRL + x will fire the Code.

    Option Explicit
    
    Sub Split_Service()
       Dim ws           As Worksheet
       Dim ws1          As Worksheet
       Dim LR           As Long
    
       Application.ScreenUpdating = False
       Set ws = Sheets("Admission")
    
       With ws
          LR = .Range("D" & .Rows.Count).End(xlUp).Row
          .Range("A1:O" & LR).AutoFilter Field:=4, Criteria1:= _
                                         "=2-3 sicu", Operator:=xlOr, Criteria2:="=2-3 surg"
    
          If .Columns(4).SpecialCells(xlCellTypeVisible).Cells.Count > 1 Then
             Set ws1 = Sheets("Surg Adm")
             With ws1
                .UsedRange.Offset(1, 0).ClearContents
             End With
    
             .Range(.Cells(2, 1), .Cells(LR, "O")).SpecialCells(xlCellTypeVisible).EntireRow.Copy
             ws1.Range("A2").PasteSpecial (xlPasteColumnWidths)
             ws1.Range("A2").PasteSpecial (xlPasteValues)
          End If
          .AutoFilterMode = False
    
    
          .Range("A1:O" & LR).AutoFilter Field:=4, Criteria1:= _
                                         Array("4-4 sarrtp", "8-1", "8-3", "ed psyc"), Operator:=xlFilterValues
          
          If .Columns(4).SpecialCells(xlCellTypeVisible).Cells.Count > 1 Then
             Set ws1 = Sheets("Psy Adm")
             With ws1
                .UsedRange.Offset(1, 0).ClearContents
             End With
    
             .Range(.Cells(2, 1), .Cells(LR, "O")).SpecialCells(xlCellTypeVisible).EntireRow.Copy
             ws1.Range("A2").PasteSpecial (xlPasteColumnWidths)
             ws1.Range("A2").PasteSpecial (xlPasteValues)
          End If
          .AutoFilterMode = False
    
    
          .Range("A1:O" & LR).AutoFilter Field:=4, Criteria1:= _
                                         Array("2-3 med", "2-3 micu", "ed med"), Operator:=xlFilterValues
    
          If .Columns(4).SpecialCells(xlCellTypeVisible).Cells.Count > 1 Then
             Set ws1 = Sheets("Med Adm")
             With ws1
                .UsedRange.Offset(1, 0).ClearContents
             End With
    
             .Range(.Cells(2, 1), .Cells(LR, "O")).SpecialCells(xlCellTypeVisible).EntireRow.Copy
             ws1.Range("A2").PasteSpecial (xlPasteColumnWidths)
             ws1.Range("A2").PasteSpecial (xlPasteValues)
          End If
          .AutoFilterMode = False
    
          .Range("A1:O" & LR).AutoFilter Field:=4, Criteria1:= _
                                         Array("N42-2C", "N42-1B", "43-1"), Operator:=xlFilterValues
    
          If .Columns(4).SpecialCells(xlCellTypeVisible).Cells.Count > 1 Then
             Set ws1 = Sheets("Gec Adm")
             With ws1
                .UsedRange.Offset(1, 0).ClearContents
             End With
    
             .Range(.Cells(2, 1), .Cells(LR, "O")).SpecialCells(xlCellTypeVisible).EntireRow.Copy
             ws1.Range("A2").PasteSpecial (xlPasteColumnWidths)
             ws1.Range("A2").PasteSpecial (xlPasteValues)
          End If
          .AutoFilterMode = False
    
       End With
       Application.CutCopyMode = False
       Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files

+ 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. [SOLVED] looking for a code that will clear cell value if cell contains certian text
    By justlearning123 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-13-2015, 08:33 PM
  2. [SOLVED] Formula or code to only copy text in bold from sheet 1 to sheet 2
    By Excelfail in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-09-2014, 12:21 AM
  3. [SOLVED] Hide cell if certian cells equal a certian criteria
    By namluke in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-22-2014, 09:39 AM
  4. need VBA code to copy Certian Cells from one sheet to another in same format
    By splash_b in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-13-2014, 01:50 AM
  5. Replies: 1
    Last Post: 07-31-2013, 10:21 AM
  6. VBA Code to open text files and copy information over to an excel sheet
    By kenadams378 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-03-2012, 12:18 PM
  7. [SOLVED] Copy certian txt from UserForm (Excel) to Table (Word)
    By stojko89 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-01-2012, 03:46 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