+ Reply to Thread
Results 1 to 4 of 4

Copy column based on partial word match in first row and paste into existing workbook

  1. #1
    Registered User
    Join Date
    10-24-2012
    Location
    Perth
    MS-Off Ver
    Excel 2010
    Posts
    4

    Copy column based on partial word match in first row and paste into existing workbook

    Hello friends,

    This is my first post, but I have been a member of xl help forum for a long time, slowly learning VBA by picking up and understanding chunks of code.
    I have recently written some code with the little knowledge I have to open an existing workbook, extract a column range and paste it into a seperate workbook.
    I am looking to take this a step further by using text fragments to select, copy and paste individual columns, which meet the text criteria into a new workbook where the data can be analysed.
    Essentially I am aiming to interrogate and extract information from a master workbook into a seperate but existing workbook and have the columns pasted so they are next to eachother.
    At the moment my code looks like this:

    Sub COPYCELL()

    Dim wbk As Workbook
    Dim i As Integer

    strFirstFile = "C:\Workbook1.xlsx"
    strSecondFile = "C:\workbook2.xlsx"

    Set wbk = Workbooks.Open(strFirstFile)
    With wbk.Sheets("Master Sheet")
    For i = ActiveSheet.UsedRange.Columns.Count To 1 Step -1
    If Application.CountIf(Columns(i), "*Total*") = 1 Then
    Columns(i).Copy
    End If
    Next i
    End With

    Set wbk = Workbooks.Open(strSecondFile)
    With wbk.Sheets("Sheet1")
    Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    End With
    End Sub

    The problem I am having is that it will only copy and paste the first row before cutting out.
    Ideally it would be good to have a loop offset that would perform the task until the activecell becomes blank or up to a specified column range such as ZZ1.

    The original code I used for a basic range copy operation looked like this:

    Dim wbk As Workbook

    intFirstFile = "C:\Workbook1.xlsx.xlsx"
    intSecondFile = "C:\Workbook2.xlsx.xlsx"

    Set wbk = Workbooks.Open(intFirstFile)
    With wbk.Sheets("Master Sheet")
    Range("G1:G1000").Copy
    End With

    Set wbk = Workbooks.Open(intSecondFile)
    With wbk.Sheets("Sheet1")
    Range("G1:G1000").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    End With

    End Sub

    Your help would be very greatly appreciated.
    Thank you very much,

    Dave

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy column based on partial word match in first row and paste into existing workbook

    Maybe:

    Please Login or Register  to view this content.
    Not sure this does what you want, so try on a copy first.

  3. #3
    Registered User
    Join Date
    10-24-2012
    Location
    Perth
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Copy column based on partial word match in first row and paste into existing workbook

    Hi John,

    I gave this a shot on a test sheet and got the following error message:

    Run time error 9
    Subscript out of range.

    Here is the script I used, slightly adapted of course.
    Any ideas on how to proceed?

    Sub DavidHarris1987()
    Dim wbk As Workbook
    Dim wbk2 As Workbook
    Dim i As Integer

    strFirstFile = "C:\Excel\Workbook1.xlsx"
    strSecondFile = "C:\Excel\Workbook2.xlsx"

    Set wbk = Workbooks.Open(strFirstFile)
    Set wbk = Workbooks.Open(strSecondFile)

    With wbk.Sheets("Master Sheet")
    For i = 1 To ActiveSheet.UsedRange.Columns.Count
    If Range("A" & i) Like "*Total*" Then
    Columns(i).Copy
    wbk2.Sheets("Sheet1").Cells(1, wbk2.Sheets("Sheet1").UsedRange.Columns.Count + 1).PasteSpecial xlPasteAll
    End If
    Next i
    End With

    End Sub

    Cheers,

    Dave
    Attached Files Attached Files
    Last edited by David Harris 1987; 09-18-2013 at 10:18 AM.

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy column based on partial word match in first row and paste into existing workbook

    Maybe:

    Please Login or Register  to view this content.

+ 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 column based on partial word match in first row and paste into existing workbook
    By David Harris 1987 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-11-2013, 01:02 PM
  2. [SOLVED] Copy/Paste from partial match between worksheets
    By ussenterprise in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 06-19-2013, 02:20 PM
  3. Copying row based on partial cell match and paste into existing worksheets
    By colinh69 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-25-2011, 02:42 PM
  4. Copy/Paste row to another sheet based on word in column
    By mulcahcf in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-20-2010, 12:55 PM
  5. Replies: 1
    Last Post: 10-17-2005, 04:05 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