+ Reply to Thread
Results 1 to 7 of 7

VBA Code to copy data from one sheet and paste to multiple other sheets

Hybrid View

  1. #1
    Registered User
    Join Date
    06-04-2012
    Location
    Sheffield, UK
    MS-Off Ver
    Excel 2003-2010
    Posts
    56

    VBA Code to copy data from one sheet and paste to multiple other sheets

    In the attached file I have a macro that will copy the data entered in B3:F3 and paste it into a range below depending on what is selected in C4. This code works great, however I want to see if I can have a stand alone data entry sheet (Multi-Add) that when the button is clicked it will copy the data on that sheet and paste it into every other sheet in the work book but again based on the "Type" selected in C4.

    The final thing I want to do is in the "General" and "Specialist" ranges - when there is no data in the row I would like the row to be hidden and then when data is added through the above it would un-hide.

    Any help on this would be great, thank you.
    Attached Files Attached Files
    Last edited by JimmyG.; 10-17-2013 at 04:27 AM.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: VBA Code to copy data from one sheet and paste to multiple other sheets

    Try something like this...

    Option Explicit
    Sub Post_ItAll()
        
        Dim cel As Range
        Dim NR As Long
        Dim ws As Worksheet
        Dim strType As String
        
        For Each cel In Range("B3:F3")
            If cel.Value = "" Then
                MsgBox "Missing Data"
                Range(cel.Address).Activate
                Application.EnableEvents = False
                Range("C4").Value = ""
                Application.EnableEvents = True
                Exit Sub
            End If
        Next cel
        
        strType = Range("C4").Value
            
        For Each ws In Worksheets
            If ws.Name Like "Agent*" Then
                With ws.Range(strType).Resize(, 1)
                    NR = Application.CountA(.Cells) + 1
                    If NR > .Rows.Count Then
                        MsgBox "The '" & strType & "' range on sheet '" & ws.Name & "' is full. ", , "Copy Canceled"
                    Else
                        .Cells(NR).Resize(, 5).Value = ActiveSheet.Range("B3:F3").Value
                        .Cells.EntireRow.Hidden = True
                        .Cells.Resize(NR).EntireRow.Hidden = False
                    End If
                End With
            End If
        Next ws
        
        MsgBox "Post It complete. "
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    06-04-2012
    Location
    Sheffield, UK
    MS-Off Ver
    Excel 2003-2010
    Posts
    56

    Re: VBA Code to copy data from one sheet and paste to multiple other sheets

    Thanks Alphafrog that works great, which part of that code would I copy into the existing code on the attached book to achieve the hidden/show row execution?
    Thanks, James

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: VBA Code to copy data from one sheet and paste to multiple other sheets

    Quote Originally Posted by JimmyG. View Post
    Thanks Alphafrog that works great, which part of that code would I copy into the existing code on the attached book to achieve the hidden/show row execution?
    Thanks, James
    You're welcome.

    All of it. Replace your previous Post_ItAll procedure with this new one. I'm confused though. If it "works great", apparently you already implemented the new code properly.

  5. #5
    Registered User
    Join Date
    06-04-2012
    Location
    Sheffield, UK
    MS-Off Ver
    Excel 2003-2010
    Posts
    56

    Re: VBA Code to copy data from one sheet and paste to multiple other sheets

    Hi, yes that code works great for the postitall function, if you look in the book I attached there is a postit code which works only on the sheet you are on - I wanted to be able to incorporate the hide town function within that code too

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: VBA Code to copy data from one sheet and paste to multiple other sheets

    Sub Post_It()
        
        Dim cel As Range
        Dim NR As Long
        Dim strType As String
        
        For Each cel In Range("B3:F3")
            If cel.Value = "" Then
                MsgBox "Missing Data ", vbExclamation, "Invalid Entry"
                Range(cel.Address).Activate
                Application.EnableEvents = False
                Range("C4").Value = ""
                Application.EnableEvents = True
                Exit Sub
            End If
        Next cel
        
        strType = Range("C4").Value
            
        With Range(strType).Resize(, 1)
            NR = Application.CountA(.Cells) + 1
            If NR > .Rows.Count Then
                MsgBox "The '" & strType & "' range is full. ", , "Copy Canceled"
            Else
                .Cells(NR).Resize(, 5).Value = Range("B3:F3").Value
                .Cells.EntireRow.Hidden = True
                .Cells.Resize(NR).EntireRow.Hidden = False
            End If
        End With
        
    End Sub

  7. #7
    Registered User
    Join Date
    06-04-2012
    Location
    Sheffield, UK
    MS-Off Ver
    Excel 2003-2010
    Posts
    56

    Re: VBA Code to copy data from one sheet and paste to multiple other sheets

    Thank you, that solves the code

+ 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] copy range from multiple sheets and paste to one sheet
    By David_Hiss in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 04-05-2013, 09:48 PM
  2. Code to copy data from multiple sheets into one final output sheet.
    By kenadams378 in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 08-03-2012, 08:51 AM
  3. 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
  4. macro copy/paste lists data from multiple sheets to one comparison sheet
    By johnnieboi89 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-29-2011, 04:22 PM
  5. Copy data in multiple sheets and paste into one sheet.
    By Slomaro2000 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-05-2008, 05:27 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