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