Results 1 to 14 of 14

copy from another workbook's worksheet based on column name.

Threaded View

  1. #1
    Registered User
    Join Date
    07-18-2013
    Location
    Cincinnati
    MS-Off Ver
    2010
    Posts
    35

    copy from another workbook's worksheet based on column name.

    I am trying to search through a workbook that only will have 1 tab. I want to search for column names, as sometimes they will have less columns depending on how the reports are generated.

    I want to copy that column, minus the header, into a table on a worksheet in a completely different workbook. I use the OpenFileDialog to select the report to copy from. The VBA code is on the workbook that is being pasted into, so it will be open.

    Here is the code I have tried. I get a "Run-time error '13': Type Mistmatch" Error
    Sub Button4_Click()
       '
       ' CopyOpenItems Macro
       ' Copy open items to sheet.
       '
       Dim fDialog As Office.FileDialog
       Dim varFile As Variant
       
       Dim wbExport            As Workbook 'workbook from where the data is to Copied
       Dim wbImport            As Workbook 'workbook where the data is to be Pasted
       
       Dim wsExport            As Worksheet 'worksheet from where the data is to Copied
       Dim wsImport            As Worksheet 'workbook where the data is to be Pasted
       
       Dim strExportName             As String   'name of the workbook from where data is copied
       Dim strImportName             As String   'name of the workbook where the data is to be Pasted
        
        
       'open a workbook that has same name as the sheet name
       Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
       
       With fDialog
          If .Show = True Then
     
             'Add file chosen's name to a string.
             For Each varFile In .SelectedItems
                strExportName = varFile
             Next
     
          Else
             MsgBox "You clicked Cancel in the file dialog box."
             Exit Sub
          End If
       End With
       
       Set wbExport = Workbooks.Open(strExportName)
       Set wsExport = wbExport.Sheets(1)
       
       Set wbImport = ThisWorkbook
       Set wsImport = wbImport.Sheets(1)
       
       'Find Publisher column and copy
       Sheets(wsExport).Select
       Cells.Find("Publisher", , xlValues, xlWhole).EntireColumn.SpecialCells(xlCellTypeConstants).Offset(1).SpecialCells(xlCellTypeConstants).Copy
    
    
    'Select Secondary Report sheet, column E and paste
    Sheets(wsImport).Select
    Range("A2:A" & lngLastRow).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
        
       wbExport.Close
    End Sub
    Also tried this
    ........Same code etc.
       
    
        Sheets(wsExport).Select
        Publisher = WorksheetFunction.Match("Publisher", Rows("1:1"), 0)
         
        Sheets(wsExport).Columns(Publisher).Copy Destination:=Sheets(wsImport).Range("A2")
        
       wbExport.Close
    End Sub
    this code is in a module on the workbook being copied into.
    Last edited by Jhail83; 07-31-2013 at 03:33 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. How do i select lines based on value in one Column and copy to other Workbook
    By dappelma in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-18-2013, 05:38 PM
  2. Copy a row if an X is in a column to a worksheet in a workbook
    By Bazmama in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-20-2013, 04:20 AM
  3. Replies: 2
    Last Post: 12-26-2012, 09:36 AM
  4. Replies: 5
    Last Post: 06-08-2012, 11:26 PM
  5. Replies: 3
    Last Post: 01-15-2005, 11:06 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