+ Reply to Thread
Results 1 to 13 of 13

Thread: pasting values to first free row in a column

  1. #1
    Registered User
    Join Date
    02-09-2012
    Location
    wellington, new zealand
    MS-Off Ver
    Excel 2010
    Posts
    7

    Thumbs up pasting values to first free row in a column

    Hi

    I have a workbook where I need the values from column A workseet 1 to appear in column A on worksheet 2. I manually enter different values into column A on worksheet 2 so I need the values being copied from worksheet 1 into worksheet 2 to go into the first free row. Also, if a macro is the correct solution, is it possible for the macro to not copy values that have already been copied across?

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,224

    Re: pasting values to first free row in a column

    Sure, put this macro into the sheet module of Sheet1, it will watch column A. Anytime you enter a new value it will add it to sheet2 column A if it is new.

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    
    For Each cell In Target
        If cell.Column = 1 And cell <> "" Then
            On Error Resume Next
            If Application.WorksheetFunction.Match(cell.Value, Sheets("Sheet2").Range("A:A"), 0) = 0 Then
                Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1).Value = cell.Value
            End If
        End If
    Next cell
            
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    02-09-2012
    Location
    wellington, new zealand
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: pasting values to first free row in a column

    cool, works perfectly, thanks very much

  4. #4
    Registered User
    Join Date
    02-09-2012
    Location
    wellington, new zealand
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: pasting values to first free row in a column

    what changes to the code would i need to make to get column b to copy as well as column a? thanks

  5. #5
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,224

    Re: pasting values to first free row in a column

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    
    For Each cell In Target
        If (cell.Column = 1 Or cell.Column = 2) And cell <> "" Then
            On Error Resume Next
            If Application.WorksheetFunction.Match(cell.Value, Sheets("Sheet2").Range("A:A"), 0) = 0 Then
                Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1).Value = cell.Value
            End If
        End If
    Next cell
            
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  6. #6
    Registered User
    Join Date
    02-09-2012
    Location
    wellington, new zealand
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: pasting values to first free row in a column

    thanks JBeaucaire but I need column A to go to column A and column B to go to column B... if i added a data validation to column 1, would it change the code a all?

  7. #7
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,224

    Re: pasting values to first free row in a column

    So, to do only columns 1 and 2:
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    
    For Each cell In Target
        On Error Resume Next
        Select Case cell.Column
            Case 1      'column A
                If Application.WorksheetFunction.Match(cell.Value, Sheets("Sheet2").Range("A:A"), 0) = 0 Then
                    Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1).Value = cell.Value
            Case 2      'column B
                End If
                If Application.WorksheetFunction.Match(cell.Value, Sheets("Sheet2").Range("B:B"), 0) = 0 Then
                    Sheets("Sheet2").Range("B" & Rows.Count).End(xlUp).Offset(1).Value = cell.Value
                End If
        End Select
    Next cell
            
    End Sub
    Last edited by JBeaucaire; 02-10-2012 at 09:27 PM. Reason: Missing END IFs
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  8. #8
    Registered User
    Join Date
    02-09-2012
    Location
    wellington, new zealand
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: pasting values to first free row in a column

    Running this code I get the following 'Compile error: Case without Select Case' and the 'Case' in 'Case 2' is highlighted...

  9. #9
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,224

    Re: pasting values to first free row in a column

    Corrected the code above, added the missing END IFs... sorry about that.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  10. #10
    Registered User
    Join Date
    02-09-2012
    Location
    wellington, new zealand
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: pasting values to first free row in a column

    thanks, getting close now! the code will only copy a value across from sheet1 to sheet2 once. I use names in one of the columns and they need to appear many times in sheet 2... help is much appreciated

  11. #11
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,224

    Re: pasting values to first free row in a column

    The macro I gave you is intentionally designed to copy each unique value to sheet2. How odd that you would want multiples on sheet2 if you already have multiples on sheet1.....

    Anyway, that's significantly easier if we don't have to check anything:

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    
    For Each cell In Target
        Select Case cell.Column
            Case 1, 2      'column A or B only
                Sheets("Sheet2").Cells(Rows.Count, cell.Column).End(xlUp).Offset(1).Value = cell.Value
        End Select
    Next cell
            
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  12. #12
    Registered User
    Join Date
    02-09-2012
    Location
    wellington, new zealand
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: pasting values to first free row in a column

    cool, thats what I was after. If I wanted column B to go to say Column D in sheet2, how would I change the code?

  13. #13
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,224

    Re: pasting values to first free row in a column

    go back to the original layout where each "Case" was separate, and change the destination to something specific instead of dynamic...
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    
    For Each cell In Target
        Select Case cell.Column
            Case 1          'column A
                Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1).Value = cell.Value
            Case 2          'column B
                Sheets("Sheet2").Range("D" & Rows.Count).End(xlUp).Offset(1).Value = cell.Value
        End Select
    Next cell
            
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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