+ Reply to Thread
Results 1 to 7 of 7

Copy range and paste to next empty row in a defined row or area

Hybrid View

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

    Copy range and paste to next empty row in a defined row or area

    I've been looking in various forums to help with this however there isn't anything exactly what I need.

    I have a report that I need users to enter information into, cells b3:f3. once all the data is added I need them to click a button to add that information to a report that is contained on the dame sheet. The tricky part is I need the macro to copy b3:f3 and paste it into the next empty row, however it needs to be within a range depending on what "Typ" is entered. In the example book attached, if the type selected was "General" I want the data to be copied and pasted into the "General" or rows 8:17. If the type is Specialist then it needs to be copied to the Specialist section or rows 20:29.

    I am trying to update a report that was created by someone else however they are no longer contactable and the book they created where this macro works is password protected so I cannot view their code to see how it was done.

    Thanks for any help, and please contact me if you need more of a description of what I am trying to achieve.
    Attached Files Attached Files
    Last edited by JimmyG.; 10-09-2013 at 05:59 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: Copy range and paste to next empty row in a defined row or area

    Hi JimmyG

    Will you ALWAYS have ONLY 10 Items under General and under Specialist?
    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: Copy range and paste to next empty row in a defined row or area

    Hi JimmyG

    Try this Code in the attached
    Option Explicit
    
    Private Sub Workbook_Open()
    
        'set protection using UserInterface to allow macros to work
        With Sheets("Sheet1")
            .Protect _
                    DrawingObjects:=True, _
                    Contents:=True, _
                    Scenarios:=True, _
                    UserInterfaceOnly:=True
            .EnableSelection = xlUnlockedCells
        End With
    End Sub
    and
    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim cel As Range
        Dim NR As Long
        If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
        If Target.Address = "$C$4" Then
            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
    
            Select Case Target.Value
            Case "General"
                With Range("General")
                    NR = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious).Row + 1
                    Application.EnableEvents = False
                    Cells(NR, "B").Resize(1, 5).Value = Range("B3").Resize(1, 5).Value
                    Application.EnableEvents = True
                End With
            Case "Specialist"
                With Range("Specialist")
                   NR = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious).Row + 1
                    Application.EnableEvents = False
                    Cells(NR, "B").Resize(1, 5).Value = Range("B3").Resize(1, 5).Value
                    Application.EnableEvents = True
                End With
            End Select
    
        End If
    End Sub
    Please notice the Named Ranges "General" and "Specialist"

    The Code assumes this as "Yes"
    Will you ALWAYS have ONLY 10 Items under General and under Specialist
    Attached Files Attached Files

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

    Re: Copy range and paste to next empty row in a defined row or area

    Hi Jaslake,
    I copied the code into the book but cant get it to run. The think I forgot to add is that there will be a button for the user to click to add the data to the cells below. In all there will be about 10 free lines each time.

  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: Copy range and paste to next empty row in a defined row or area

    Hi JimmyG

    The Code in the Previous File will run when Cell C4 is changed...no Button required.

    The Code in this attachment runs from a Button, as requested.

    Please note, without the Named Ranges the Code cannot and will not run.
    Please notice the Named Ranges "General" and "Specialist"
    Attached Files Attached Files

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

    Re: Copy range and paste to next empty row in a defined row or area

    This code works great and does exactly what I need it to, the next challenge though is for me to have multiple worksheets (all the same in content) as each one will be used for a member of staff. I have been able to use the same code across each sheet by using "With ActiveSheet". The one thing I can't figure out though is how to add something to all sheets at once.
    For example, there will be sheet called "Multi-Add". On this sheet it will just have cells B2:F4 in the attached books, when the information is added on here I want it to paste to every other sheet in the book (in total there will be 20 sheets - Agent 1, Agent 2, etc). When I have tried to do this with various Dim ws As Worksheet and For Each ws In ActiveWorkbook.Worksheets declarations it doesn't work. I'm hoping there is a simple way around this.

    Thanks for any help, James

  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: Copy range and paste to next empty row in a defined row or area

    Hi James

    As you've marked this Thread as "Solved" it would be in your best interest to start a new Thread for these new requirements.
    If you start a new Thread and, if you wish, you can PM me with a Link to your new Thread...I'll look at it.

+ 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] Paste a value in the next empty cell for a defined range
    By aadeshgandhi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-13-2013, 08:53 PM
  2. copy and paste to defined area
    By twofootgiant in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-29-2011, 08:28 AM
  3. EXCEL 2003 copy paste a defined range in a cell
    By EXCELNOOB123 in forum Excel General
    Replies: 1
    Last Post: 03-07-2011, 10:01 PM
  4. Finding a defined range of cells then Copy & Paste
    By bernborough in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 10-11-2010, 06:45 PM
  5. Replies: 0
    Last Post: 02-24-2005, 04:06 PM

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