+ Reply to Thread
Results 1 to 5 of 5

Copy 2 Columns of Data from Sheet1 and Paste in Sheet2

Hybrid View

  1. #1
    Registered User
    Join Date
    04-08-2016
    Location
    California
    MS-Off Ver
    windows 7
    Posts
    28

    Copy 2 Columns of Data from Sheet1 and Paste in Sheet2

    Hi,

    I am a novice in VBA and still learning and would appreciate any help I can get.

    Issue: I need to write a macro that will copy two columns of data if a condition is met. The condition is "Tall" in column 2 in Sheet1. If "Tall" exist in Column2 then copy Column 4("Name") and paste it in Column 16("Name") on Sheet2. Then copy column 14 ("Amt") and paste it on column 22("Amt") on Sheet2.

    Then I want to keep doing this until I have copied columns 24-29("Amt") into Sheet2 Column22.

    I have tried to write a macro but have had no success. I have written the macro to copy two different Amt columns but no success.When I run the macro nothing happens. Not even a message error appears. I have attached my workbook.

    Please let me know if you have any questions.

    Thanks!
    Attached Files Attached Files
    laryhaty

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Copy 2 Columns of Data from Sheet1 and Paste in Sheet2

    Try
    Sub test()
        Dim a, e, p, v, x, i As Long, ii As Long, n As Long
        With Sheets("sheet1").Cells(1).CurrentRegion
            x = Filter(.Parent.Evaluate("transpose(if(" & .Columns(2).Address & "=""Tall"",row(" & .Address & ")))"), False, 0)
            If UBound(x) > -1 Then a = Application.Index(.Value, Application.Transpose(x), [{4,14,18,24,25,26,27,28,29}])
        End With
        If UBound(x) = -1 Then MsgBox "No Tall in Col.B": Exit Sub
        ReDim p(1 To UBound(a, 1) * 9, 1 To 1), v(1 To UBound(p, 1), 1 To 1)
        For ii = 2 To UBound(a, 2)
            For i = 1 To UBound(a, 1)
                n = n + 1
                p(n, 1) = a(i, 1): v(n, 1) = a(i, ii)
        Next i, ii
        Sheets("sheet2").[p2].Resize(n).Value = p
        Sheets("sheet2").[v2].Resize(n).Value = v
    End Sub

  3. #3
    Registered User
    Join Date
    04-08-2016
    Location
    California
    MS-Off Ver
    windows 7
    Posts
    28

    Re: Copy 2 Columns of Data from Sheet1 and Paste in Sheet2

    Is there anyway to add the text in cell N1(Column14) in Sheet1 to the Column20 (T) in Sheet2 rows 2-3. So for instance, lets say the text in cell N1 in Sheet1 was "Oranges Fee". Then in Column 20 (T) in Sheet2, Rows 2-3 have Orange Fees. So for each column name in Sheet 1 have that name in Column20. I have attached the workbook again to show you what I am looking for!
    Thanks so much
    Attached Files Attached Files

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Copy 2 Columns of Data from Sheet1 and Paste in Sheet2

    Change to
    Sub oranges_fee()
     Dim a, e, p, t, v, x, i As Long, ii As Long, n As Long
        With Sheets("sheet1").Cells(1).CurrentRegion
            x = Filter(.Parent.Evaluate("transpose(if(" & .Columns(2).Address & "=""Tall"",row(" & .Address & ")))"), False, 0)
            If UBound(x) > -1 Then a = Application.Index(.Value, Application.Transpose(Split("1," & Join(x, ","), ",")), [{4,14,18,24,25,26,27,28,29}])
        End With
        If UBound(x) = -1 Then MsgBox "No Tall in Col.B": Exit Sub
        ReDim p(1 To UBound(a, 1) * 9, 1 To 1), v(1 To UBound(p, 1), 1 To 1), t(1 To UBound(p, 1), 1 To 1)
        For ii = 2 To UBound(a, 2)
            For i = 2 To UBound(a, 1)
                n = n + 1: p(n, 1) = a(i, 1)
                t(n, 1) = a(1, ii): v(n, 1) = a(i, ii)
        Next i, ii
        Sheets("sheet2").[p2].Resize(n).Value = p
        Sheets("sheet2").[t2].Resize(n).Value = t
        Sheets("sheet2").[v2].Resize(n).Value = v
    End Sub

  5. #5
    Registered User
    Join Date
    04-08-2016
    Location
    California
    MS-Off Ver
    windows 7
    Posts
    28

    Re: Copy 2 Columns of Data from Sheet1 and Paste in Sheet2

    Great, thanks!!

+ 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. Copy/Paste Data from Sheet1 to Sheet2
    By h.noor in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-11-2015, 11:39 PM
  2. Macro to copy the data from sheet1 and to paste it in sheet2 with conditions.
    By Thinker8 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-17-2014, 09:55 AM
  3. Replies: 6
    Last Post: 07-25-2013, 02:58 PM
  4. [SOLVED] Faster way to copy and paste multiple columns in Sheet1 to corresponding columns in Sheet2
    By babbeleme1 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-19-2013, 12:55 AM
  5. Copy Range of data from sheet1 and sheet2 paste it in sheet3
    By hassan khansa in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-18-2013, 05:47 AM
  6. [SOLVED] Copy data from column A and B Sheet1 and paste into Sheet2
    By rabra in forum Excel General
    Replies: 2
    Last Post: 02-03-2013, 11:39 AM
  7. Copy data under specific columns in sheet1 n paste to sheet2 under the same headers
    By ShyamEXcel in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-08-2012, 02:15 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