+ Reply to Thread
Results 1 to 6 of 6

Thread: Copy Column Error

  1. #1
    Registered User
    Join Date
    12-08-2011
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    27

    Question Copy Column Error

    The Goal:
    • Open a workbook from an existing blank workbook with the VBA Code
    • Copy cells in "D" column, then close workbook
      Eventually, I'd like to just copy the info that varies in the column week by week, instead of the whole dang column
    • Paste that column in column A of blank workbook, starting at "A2"

    The Problem:
    I don't know what my problem is, but I keep getting an error (varying between 5, and 1004), or nothing happens when I run the code.

    Any assistance would be appreciated.

    Sub CopyFromWKB()
    
        Dim Wkb1 As Workbook
        Dim fileAddy As Variant
        Dim xlAddy As String
        
        ' OpenDialogue to chose a file
        fileAddy = Application.GetOpenFilename(, , "Chose a File", , False)
        
        Application.ScreenUpdating = False
        
        Set Wkb1 = Workbooks.Open(Filename:=fileAddy)
        Call CombineTextColumns
        Call CopyRange
        ActiveWorkbook.Close False
        
        Range("A1").Select
        Selection.PasteSpecial
        Application.CutCopyMode = False
        
        Application.ScreenUpdating = True
    
    End Sub
    
    Private Sub CombineTextColumns()
    ' Task: Take two columns and combine the data
    
        Const sDELIM As String = ", "
        Dim vTxt As String
        Dim vTxt2 As String
        Dim finalText As String
        Dim i As Long
        i = 2
    
        Range("D2").Select
        Application.ScreenUpdating = False
        
        Do Until IsEmpty(ActiveCell)
            vTxt = Cells(i, "d").Text
            vTxt2 = Cells(i, "E").Text
            finalText = vTxt & sDELIM & vTxt2
            Cells(i, "D").Value = finalText
            ActiveCell.Offset(1, 0).Select
            i = i + 1
         Loop
        Application.ScreenUpdating = False
    
    End Sub
    
    Private Sub CopyRange()
    
        ' ThisWorkbook.Sheets("Sheet1").Columns("D:D").Copy
    
        With ActiveSheet
            Sheets("Sheet2").Columns("D:D").Copy
        End With
    
    End Sub

  2. #2
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,399

    Re: Copy Column Error

    Why dont you attach a sample workbook?
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    12-08-2011
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: Copy Column Error

    Yeah, no problem. Sorry I didn't post one before.

    VBA Testing is the blank Workbook I have the VBA code in. VBA Test Data contains the list of names that I wish to combine, then copy into VBA Testing.
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    12-08-2011
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: Copy Column Error

    Hate spamming this thread, but I've been working on this stupid thing and have a working Code. Problem is, it's not very pretty. I'm pretty sure there must be a more efficient way.

    Is there someone that could spot check this code and suggest a more efficient way? I don't like the fact i have to take the whole column, I'd rather just take the range of the cells that I need from the second workbook from D2 to the end of the values.

    Any suggestions would be helpful.

    Sub CopyFromWKB()
    
        Dim Wkb1 As Workbook
        Dim ws As Worksheet
        Dim OrgWork As Workbook
        Dim fileAddy As Variant
        Dim rRange As Range
        
        On Error GoTo errHandler
        
        Set OrgWork = ActiveWorkbook
        Set ws = ActiveSheet
        ' OpenDialogue to chose a file
        Set Wkb1 = Workbooks.Open(Application.GetOpenFilename(, , "Chose a File", , False))
        
        Application.ScreenUpdating = False
        
        ' Set Wkb1 = Workbooks.Open(Filename:=fileAddy)
        wkb1Name = ThisWorkbook.Name
        Call CombineTextColumns
        Wkb1.Worksheets("Sheet1").Range("D3:D200, O3:O200").Copy
        OrgWork.Activate
        Range("A2").PasteSpecial xlPasteAllExceptBorders
        Application.CutCopyMode = False
        Wkb1.Close False
        Columns("A:A").EntireColumn.AutoFit
        Application.ScreenUpdating = True
        
        
    Exit Sub
    
    errHandler:
        MsgBox "Error copying data - error " & Err.Number & _
    " - " & Err.Description
    
    End Sub
    
    Private Sub CombineTextColumns()
    ' Task: Take two columns and combine the data
    
        Const sDELIM As String = ", "
        Dim vTxt As String
        Dim vTxt2 As String
        Dim finalText As String
        Dim i As Long
        i = 2
    
        Range("D2").Select
        Application.ScreenUpdating = False
        
        Do Until IsEmpty(ActiveCell)
            vTxt = Cells(i, "d").Text
            vTxt2 = Cells(i, "E").Text
            finalText = vTxt & sDELIM & vTxt2
            Cells(i, "D").Value = finalText
            ActiveCell.Offset(1, 0).Select
            i = i + 1
         Loop
        Application.ScreenUpdating = False
    
    End Sub
    Last edited by JHizzal; 02-01-2012 at 12:47 AM. Reason: Updated my code to what it shows now...

  5. #5
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,399

    Re: Copy Column Error

    You have this line of code which does not have a proper syntax -
      Wkb1.Worksheets("Sheet1").Range("D3:D200, O3:O200").Copy
    You can find the last used row using this -
     lrow=Wkb1.worksheets("Sheet1").range("A" & rows.count).end(xlup).row
    Wkb1.worksheets("Sheet1").range("D3:D" & lrow).copy
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  6. #6
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Copy Column Error

    This probably suffices

    Sub snb()
      Workbooks.Open(Application.GetOpenFilename(, , "Choose a File", , False))
        
      with thisworkbook.activesheet.cells(2,1).resize(197)
       .value=[index(D3:D200&","&E3:E200,)]
       .offset(,2)=[O3:O200]
      end with
    End Sub



+ 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