+ Reply to Thread
Results 1 to 11 of 11

VBA Code to copy different data from one sheet to multiple sheets based on criteria

Hybrid View

eharwood VBA Code to copy different... 09-30-2014, 03:17 PM
jaslake Re: VBA Code to copy... 10-04-2014, 12:55 PM
jaslake Re: VBA Code to copy... 10-04-2014, 03:22 PM
eharwood Re: VBA Code to copy... 10-06-2014, 04:07 PM
jaslake Re: VBA Code to copy... 10-06-2014, 05:41 PM
eharwood Re: VBA Code to copy... 10-08-2014, 12:40 PM
jaslake Re: VBA Code to copy... 10-08-2014, 12:57 PM
eharwood Re: VBA Code to copy... 10-08-2014, 01:02 PM
jaslake Re: VBA Code to copy... 10-08-2014, 01:35 PM
eharwood Re: VBA Code to copy... 10-08-2014, 01:55 PM
jaslake Re: VBA Code to copy... 10-08-2014, 02:22 PM
  1. #1
    Registered User
    Join Date
    09-30-2014
    Location
    Atlanta
    MS-Off Ver
    2013
    Posts
    5

    VBA Code to copy different data from one sheet to multiple sheets based on criteria

    Hello,

    I am very new to VBA code and would greatly appreciate any help you are able to offer. I have a master contacts workbook and would like the ability to add new contacts to sheet 1 and then, based on criteria that was entered in specific columns, have that data automatically be added to the additional appropriate sheets within the workbook. I would like the spreadsheet to auto-update and I would also like for only the first couple of columns for each row to be copied over to additional sheets.

    I've attached an example spreadsheet. Eharwood_VBA code sample file.xlsx

    I have columns for first name, last name, and contact info. Then there are columns that tag the individual as a member/non-member and based on participation in various committees there is a yes or no for each committee column.

    For the first row of my example spreadsheet John Smith is a tagged as a member (per column H) and also a member of the Promotions committee (a yes in column K). Therefore, I would like the ability to have John Smith's information automatically added to the member sheet and also to the promotions sheet. I also only want to copy the info from columns A-G to each additional sheet in the workbook.

    Based on extensive googling below is the code that I am attempting to use, but I receive an error message and I also feel like there is a much more efficient way to accomplish the same thing. Any suggestions and help are greatly appreciated!

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim i, LastRow
    
    LastRow = Sheets("Master").Range("A" & Rows.Count).End(xlUp).Row
    Sheets("GRMCA Members").Range("A2:G500").ClearContents
    For i = 2 To LastRow
    If Sheets("Master").Cells(i, "H").Value = "Member" Then
    Sheets("Master").Cells(i, "H").EntireRow.Copy Destination:=Sheets("GRMCA Members").Range("A" & Rows.Count).End(xlUp).Offset(1)
    End If
    Next i
    
    LastRow = Sheets("Master").Range("A" & Rows.Count).End(xlUp).Row
    Sheets("Non-Members").Range("A2:G500").ClearContents
    For i = 2 To LastRow
    If Sheets("Master").Cells(i, "H").Value = "Non-Member" Then
    Sheets("Master").Cells(i, "H").EntireRow.Copy Destination:=Sheets("Non-Members").Range("A" & Rows.Count).End(xlUp).Offset(1)
    End If
    Next i
    
    LastRow = Sheets("Master").Range("A" & Rows.Count).End(xlUp).Row
    Sheets("EOS").Range("A2:G500").ClearContents
    For i = 2 To LastRow
    If Sheets("Master").Cells(i, "I").Value = "Yes" Then
    Sheets("Master").Cells(i, "I").EntireRow.Copy Destination:=Sheets("EOS").Range("A" & Rows.Count).End(xlUp).Offset(1)
    End If
    Next i
    
    LastRow = Sheets("Master").Range("A" & Rows.Count).End(xlUp).Row
    Sheets("TAC").Range("A2:G500").ClearContents
    For i = 2 To LastRow
    If Sheets("Master").Cells(i, "J").Value = "Yes" Then
    Sheets("Master").Cells(i, "J").EntireRow.Copy Destination:=Sheets("TAC").Range("A" & Rows.Count).End(xlUp).Offset(1)
    End If
    Next i
    
    LastRow = Sheets("Master").Range("A" & Rows.Count).End(xlUp).Row
    Sheets("Promotions").Range("A2:G500").ClearContents
    For i = 2 To LastRow
    If Sheets("Master").Cells(i, "K").Value = "Yes" Then
    Sheets("Master").Cells(i, "K").EntireRow.Copy Destination:=Sheets("Promotions").Range("A" & Rows.Count).End(xlUp).Offset(1)
    End If
    Next i
    
    LastRow = Sheets("Master").Range("A" & Rows.Count).End(xlUp).Row
    Sheets("Yardage").Range("A2:G500").ClearContents
    For i = 2 To LastRow
    If Sheets("Master").Cells(i, "L").Value = "Yes" Then
    Sheets("Master").Cells(i, "L").EntireRow.Copy Destination:=Sheets("Yardage").Range("A" & Rows.Count).End(xlUp).Offset(1)
    End If
    Next i
    
    End Sub
    Thank you,
    Last edited by eharwood; 10-06-2014 at 04:09 PM.

  2. #2
    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: VBA Code to copy different data from one sheet to multiple sheets based on criteria

    Hi eharwood

    Welcome to the Forum!!!

    How many members will you have in the Master Sheet?
    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.

  3. #3
    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: VBA Code to copy different data from one sheet to multiple sheets based on criteria

    Hi eharwood

    Try the Code in the attached. Please note, I've modified the Sheet Names of GRMCA Member and Non-Member Dropped the trailing "s"). This was done to make the Code less wieldy.

    I've also added Data Validation to Columns H through L. CTRL + x will fire the Code.
    Option Explicit
    
    Sub Move_Stuff()
      Dim ws As Worksheet
      Dim Rng As Range, cel As Range, cel2 As Range
      Dim LR As Long, LR1 As Long, LR2 As Long
      Dim mySheet As String
    
      For Each ws In ThisWorkbook.Sheets
        If Not ws.Name = "Master" And Not ws.Name = "Lists" Then
          ws.UsedRange.Offset(1, 0).ClearContents
        End If
      Next ws
    
      Set ws = Sheets("Master")
    
      With ws
        LR = .Range("B" & Rows.Count).End(xlUp).Row
        For Each cel In .Range("A2:A" & LR)
          Select Case .Range("H" & cel.Row).Value
          Case "GRMCA Member"
            With Sheets("GRMCA Member")
              LR1 = .Range("B" & Rows.Count).End(xlUp).Row + 1
              .Range(.Cells(LR1, "A"), .Cells(LR1, "G")).Value = ws.Range("A" & cel.Row).Resize(1, 7).Value
              Set Rng = ws.Range(ws.Cells(cel.Row, "I"), ws.Cells(cel.Row, "L"))
              For Each cel2 In Rng
                If cel2.Value = "Yes" Then
                  mySheet = ws.Cells(1, cel2.Column).Value
                  With Sheets(mySheet)
                    LR2 = .Range("B" & Rows.Count).End(xlUp).Row + 1
                    .Range(.Cells(LR2, "A"), .Cells(LR2, "G")).Value = ws.Range("A" & cel.Row).Resize(1, 7).Value
                  End With
                End If
              Next cel2
            End With
          Case "Non-Member"
            With Sheets("Non-Member")
              LR1 = .Range("B" & Rows.Count).End(xlUp).Row + 1
              .Range(.Cells(LR1, "A"), .Cells(LR1, "G")).Value = ws.Range("A" & cel.Row).Resize(1, 7).Value
              Set Rng = ws.Range(ws.Cells(cel.Row, "I"), ws.Cells(cel.Row, "L"))
              For Each cel2 In Rng
                If cel2.Value = "Yes" Then
                  mySheet = ws.Cells(1, cel2.Column).Value
                  With Sheets(mySheet)
                    LR2 = .Range("B" & Rows.Count).End(xlUp).Row + 1
                    .Range(.Cells(LR2, "A"), .Cells(LR2, "G")).Value = ws.Range("A" & cel.Row).Resize(1, 7).Value
                  End With
                End If
              Next cel2
            End With
          Case Else
            'Do nothing
          End Select
        Next cel
      End With
    End Sub
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    09-30-2014
    Location
    Atlanta
    MS-Off Ver
    2013
    Posts
    5

    Re: VBA Code to copy different data from one sheet to multiple sheets based on criteria

    That works perfectly. Thank you so much!

  5. #5
    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: VBA Code to copy different data from one sheet to multiple sheets based on criteria

    You're welcome...glad I could help.

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

  6. #6
    Registered User
    Join Date
    09-30-2014
    Location
    Atlanta
    MS-Off Ver
    2013
    Posts
    5

    Re: VBA Code to copy different data from one sheet to multiple sheets based on criteria

    Hi Jaslake,

    One additional question. If I wanted to add additional sheets or categories to the spreadsheet, how would I alter the code to include the additional tabs? Thank you so much!

  7. #7
    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: VBA Code to copy different data from one sheet to multiple sheets based on criteria

    Hi eharwood

    The Code was written to accommodate the Sample File. I'd need to see what you'd be adding in order to modify the Code for this
    If I wanted to add additional sheets or categories to the spreadsheet, how would I alter the code to include the additional tabs

  8. #8
    Registered User
    Join Date
    09-30-2014
    Location
    Atlanta
    MS-Off Ver
    2013
    Posts
    5

    Re: VBA Code to copy different data from one sheet to multiple sheets based on criteria

    I understand, I've attached a new sample file. I'm adding an additional column to the master titled "industry" and a corresponding new sheet titled "industry". If possible if you can show me what you change in order to accommodate an additional tab then I would be able to edit the spreadsheet in the future if I need to add additional columns, without having to bother someone like yourself
    Eharwood_VBA code sample file.xlsx

  9. #9
    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: VBA Code to copy different data from one sheet to multiple sheets based on criteria

    Hi eharwood

    If you're simply adding Yes/No questions the change appears rather simple...the new Column Heading needs to be the same as the new Sheet Name...
    Option Explicit
    
    Sub Move_Stuff()
      Dim ws As Worksheet
      Dim Rng As Range, cel As Range, cel2 As Range
      Dim LR As Long, LR1 As Long, LR2 As Long
      Dim mySheet As String
    
      For Each ws In ThisWorkbook.Sheets
        If Not ws.Name = "Master" And Not ws.Name = "Lists" Then
          ws.UsedRange.Offset(1, 0).ClearContents
        End If
      Next ws
    
      Set ws = Sheets("Master")
    
      With ws
        LR = .Range("B" & Rows.Count).End(xlUp).Row
        For Each cel In .Range("A2:A" & LR)
          Select Case .Range("H" & cel.Row).Value
          Case "GRMCA Member"
            With Sheets("GRMCA Member")
              LR1 = .Range("B" & Rows.Count).End(xlUp).Row + 1
              .Range(.Cells(LR1, "A"), .Cells(LR1, "G")).Value = ws.Range("A" & cel.Row).Resize(1, 7).Value
    
    '############## Change This ###################
              'Set Rng = ws.Range(ws.Cells(cel.Row, "I"), ws.Cells(cel.Row, "L"))
              Set Rng = ws.Range(ws.Cells(cel.Row, "I"), ws.Cells(cel.Row, "M"))
    '##############################################
    
              For Each cel2 In Rng
                If cel2.Value = "Yes" Then
                  mySheet = ws.Cells(1, cel2.Column).Value
                  With Sheets(mySheet)
                    LR2 = .Range("B" & Rows.Count).End(xlUp).Row + 1
                    .Range(.Cells(LR2, "A"), .Cells(LR2, "G")).Value = ws.Range("A" & cel.Row).Resize(1, 7).Value
                  End With
                End If
              Next cel2
            End With
          Case "Non-Member"
            With Sheets("Non-Member")
              LR1 = .Range("B" & Rows.Count).End(xlUp).Row + 1
              .Range(.Cells(LR1, "A"), .Cells(LR1, "G")).Value = ws.Range("A" & cel.Row).Resize(1, 7).Value
    
    '############## Change This ###################
              'Set Rng = ws.Range(ws.Cells(cel.Row, "I"), ws.Cells(cel.Row, "L"))
              Set Rng = ws.Range(ws.Cells(cel.Row, "I"), ws.Cells(cel.Row, "M"))
    '##############################################
    
              For Each cel2 In Rng
                If cel2.Value = "Yes" Then
                  mySheet = ws.Cells(1, cel2.Column).Value
                  With Sheets(mySheet)
                    LR2 = .Range("B" & Rows.Count).End(xlUp).Row + 1
                    .Range(.Cells(LR2, "A"), .Cells(LR2, "G")).Value = ws.Range("A" & cel.Row).Resize(1, 7).Value
                  End With
                End If
              Next cel2
            End With
          Case Else
            'Do nothing
          End Select
        Next cel
      End With
    End Sub

  10. #10
    Registered User
    Join Date
    09-30-2014
    Location
    Atlanta
    MS-Off Ver
    2013
    Posts
    5

    Re: VBA Code to copy different data from one sheet to multiple sheets based on criteria

    Ok got it, thank you so much for all your help! That worked perfectly and now I can add additional yes/no columns as needed.

  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: VBA Code to copy different data from one sheet to multiple sheets based on criteria

    You're welcome...glad I could help.

    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)

Similar Threads

  1. Code to copy a row from multiple sheets to a summary sheet per criteria
    By rmwindham in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 08-29-2014, 11:05 AM
  2. [SOLVED] VBA Code to copy data from one sheet and paste to multiple other sheets
    By JimmyG. in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-17-2013, 04:25 AM
  3. Search multiple sheets for value based on criteria and copy data accordingly
    By Zagadka in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-31-2012, 08:46 AM
  4. need help with code to copy data from multiple sheets to one sheet
    By daillest319 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-17-2012, 01:07 PM
  5. Copy data from multiple sheets based on certain criteria!!
    By missyk77 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 08-24-2010, 12:05 PM

Tags for this Thread

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