+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: Conditional copy of specific cells to another sheet

  1. #1
    Registered User
    Join Date
    07-28-2010
    Location
    princeton, nj
    MS-Off Ver
    Excel 2003
    Posts
    13

    Conditional copy of specific cells to another sheet

    there are a few threads related to conditional copy but none are addressing the issue I have.

    Have a master sheet , Would like to copy specific column values ( along with the cell formating) to another sheet in the same workbook if there is a "X" in a specific column.

    there are multiple columns with X to be copied to mutiple worksheets

    Sample file attached
    For example Would like to copy from Master sheet to a sheet "Ent App" columns Number and Title based on the "X" in the column Ent App.Would like the formating of the cells to be the same as in the master sheet after copying.
    Same for all other columns in the worsheet.


    this is the first time with excel macros....Any help appreciated.
    Attached Files Attached Files

  2. #2
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Conditional copy of specific cells to another sheet

    Hi Anra

    Try this code
    Option Explicit
    Sub Macro1()
        Dim LR As Long
        Dim ws As Worksheet
        Dim Col As String
        Application.ScreenUpdating = False
        For Each ws In ActiveWorkbook.Worksheets
            If Not ws.Name = "Master" Then
                If ws.Name = "Ent App" Then
                    Col = "C"
                ElseIf ws.Name = "Ent infr" Then Col = "D"
                ElseIf ws.Name = "Ent Arch" Then Col = "E"
                ElseIf ws.Name = "Ent PMO" Then Col = "F"
                End If
                With Sheets("Master")
                    LR = .Range("A" & .Rows.Count).End(xlUp).Row
                    .AutoFilterMode = False
                    .Range(Col & "1:" & Col & LR).AutoFilter Field:=1, Criteria1:="X"
                    .Range("A1:B" & LR).SpecialCells(xlVisible).Copy
                    .AutoFilterMode = False
                End With
                Sheets(ws.Name).Range("A1").PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
                                                         SkipBlanks:=False, Transpose:=False
                Sheets(ws.Name).Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
                                                         False, Transpose:=False
            End If
        Next ws
        Application.ScreenUpdating = True
    End Sub
    Let me know of issues.

    John
    John

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

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

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

  3. #3
    Registered User
    Join Date
    07-28-2010
    Location
    princeton, nj
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Conditional copy of specific cells to another sheet

    Thanks so much..just ran on the sample data and works perfectly..I will try it out on the real data and will let you know. Thanks again

  4. #4
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Conditional copy of specific cells to another sheet

    Hi Anra

    Yes please. Let me know how it goes.

    John
    John

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

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

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

  5. #5
    Registered User
    Join Date
    07-28-2010
    Location
    princeton, nj
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Conditional copy of specific cells to another sheet

    Hi John - It works great on the real data!! Thank you!!

    I'm embedding each worksheet on to a ppt and could you pl let me know how to add to this script -
    when a new sheet is created with filtered data it automatically updates the sheet ( as it does now)
    & also creates a new file and updates the same data.

    So each filter will have 1 sheet + 1new file

    Example : From master sheet we have created "Ent App" sheet (happening now with filtered contents) in addition to this create a new file and add a sheet "Ent App" with the same filtered content.

    Is this possible? Any help on this will be great...thanks!

  6. #6
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Conditional copy of specific cells to another sheet

    Hi Anra

    A couple of things. I've no idea what this is
    a ppt
    Secondly, if I understand you correctly, you wish to filter the data for "Ent App" to it's worksheet as the code does now. Then you wish to create a new workbook called "Some Thing" that will have a worksheet in it called "Ent App". This new "Ent App" worksheet will contain the same data as the original copy of "Ent App". Is my understanding correct?

    John
    John

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

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

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

  7. #7
    Registered User
    Join Date
    07-28-2010
    Location
    princeton, nj
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Conditional copy of specific cells to another sheet

    Yes that is correct and ppt - was power point..sorry

  8. #8
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Conditional copy of specific cells to another sheet

    Hi Anra

    I think this code does as you requested
    Option Explicit
    Dim ws As Worksheet
    Sub Macro1()
        Dim LR As Long
     
        Dim Col As String
        Application.ScreenUpdating = False
        For Each ws In ActiveWorkbook.Worksheets
            If Not ws.Name = "Master" Then
                If ws.Name = "Ent App" Then
                    Col = "C"
                ElseIf ws.Name = "Ent infr" Then Col = "D"
                ElseIf ws.Name = "Ent Arch" Then Col = "E"
                ElseIf ws.Name = "Ent PMO" Then Col = "F"
                End If
                With Sheets("Master")
                    LR = .Range("A" & .Rows.Count).End(xlUp).Row
                    .AutoFilterMode = False
                    .Range(Col & "1:" & Col & LR).AutoFilter Field:=1, Criteria1:="X"
                    .Range("A1:B" & LR).SpecialCells(xlVisible).Copy
                    .AutoFilterMode = False
                End With
                Sheets(ws.Name).Range("A1").PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
                                                         SkipBlanks:=False, Transpose:=False
                Sheets(ws.Name).Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
                                                         False, Transpose:=False
            Call AddBook
            End If
     
        Next ws
        Application.ScreenUpdating = True
    End Sub
     
     
    Sub AddBook()
    Dim bk As Workbook
    Dim bk1 As Workbook
    Set bk = ThisWorkbook
    Set bk1 = Workbooks.Add()
    Application.DisplayAlerts = False
    bk1.SaveAs bk.Path & "\" & ws.Name & ".xls", FileFormat:=xlExcel8
    ActiveSheet.Name = ws.Name
    bk.Worksheets(ws.Name).Cells.Copy
    bk1.Worksheets(ws.Name).Range("A1").PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
                                                         SkipBlanks:=False, Transpose:=False
    bk1.Worksheets(ws.Name).Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
                                                         False, Transpose:=False
    Application.DisplayAlerts = True
    bk1.Close savechanges:=True
    'bk.Activate
    End Sub
    Please note there are two procedures here and they should both go in the same module. Also note that I've changed the previous code slightly so you'll need to replace it also. The code is in the attached.

    Let me know of issues.

    John
    Attached Files Attached Files
    John

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

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

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

  9. #9
    Registered User
    Join Date
    07-28-2010
    Location
    princeton, nj
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Conditional copy of specific cells to another sheet

    Many Thanks John..tried it out with my actual data and it works great!!

    Some Files created are large with many rows..would it be possible to add some condition
    to say that when the row count in the newly created file exceeds say 50 (testing purposes) to create
    a new file which has header row + rows that are greater than 50...

    No changes to the tab sheets created by the macro..just the files should add the confition

  10. #10
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Conditional copy of specific cells to another sheet

    Hi Anra

    Glad it works for you.

    Regarding this
    Some Files created are large with many rows..would it be possible to add some condition
    to say that when the row count in the newly created file exceeds say 50 (testing purposes) to create
    a new file which has header row + rows that are greater than 50...
    Are you saying that if in a new file to be created, the Row Count is going to be more that 50, then create a second file and split the file into two files? If this is the case, submit a file that exceeds row count of 50 to give me a file to test against.

    John
    Last edited by jaslake; 07-30-2010 at 07:19 PM.
    John

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

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

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

  11. #11
    Registered User
    Join Date
    07-28-2010
    Location
    princeton, nj
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Conditional copy of specific cells to another sheet

    Hi John - thats correct. Attached are 2 file. Sample file with apx 150 rows ( master file ) and another file Ent App.xls both for test... thanks a bunch
    Attached Files Attached Files

  12. #12
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Conditional copy of specific cells to another sheet

    Hi Anra

    I'm traveling this weekend and next week. I'll have some but not much spare time. I'll look at this possibility for you. What would the second Ent App file be called...perhaps Ent App 1?

    John
    John

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

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

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

  13. #13
    Registered User
    Join Date
    07-28-2010
    Location
    princeton, nj
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Conditional copy of specific cells to another sheet

    yes..that should be fine. if there are 140 rows for example..would like it to create 3 files Ent App1( 50 rows ) Ent App2 ( 50 rows) Ent App3 ( 40 rows). thank you!

  14. #14
    Registered User
    Join Date
    07-28-2010
    Location
    princeton, nj
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Conditional copy of specific cells to another sheet

    Hi John - would appreciate any help on the rowcount section. Have no macro experience...I tried doing it myself and it started to mess with the existing code...please help

    Thank you so much for all the help so far, I ran the macro on the final data set and it ran perfectly... Thank you!!

  15. #15
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Conditional copy of specific cells to another sheet

    Hi Anra

    Sorry I haven't gotten back to you. I've been working with my son, daughter-in-law and two grandsons with their new home. Been working 10 hour days and haven't had the clarity of mind to work on your solution.

    Going home on Thursday and will address your issue this weekend.

    John
    John

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

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

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

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0