Transferring sheets to another workbook

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,304
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I am getting an error (Object doesn't support this property or method) in the following code in the line:-

Code:
objExcelAppTo.Activate

Code:
Private Sub cmdCombineWorkbooks2_Click()
    
    Dim strPathFrom As String
    Dim strPathTo As String
    Dim strExcelFileFrom As String
    Dim strExcelFileTo As String
    Dim strConn As String
    Dim objExcelAppFrom As Object
    Dim objExcelAppTo As Object
    Dim ws As Object
    Dim blnFrom As Boolean
    Dim blnTo As Boolean
    Dim rst As ADODB.Recordset
    Dim i As Integer
    
    On Error GoTo ErrorHandler
    
    strPathFrom = "C:\Users\Aziz\Desktop\MS Excel Files\"
    strPathTo = "C:\Users\Aziz\Desktop\MS Excel Files\"
    strExcelFileFrom = "Book3.xlsx"
    strExcelFileTo = "CombineWorkbooks.xlsx"
    strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strPathFrom & strExcelFileFrom & ";Extended Properties='Excel 12.0 Xml;HDR=Yes'"
    i = 1
    
    blnFrom = OpenExcelFile2(strPathFrom & strExcelFileFrom, True, False, "Nadeem356")
    blnTo = OpenExcelFile2(strPathTo & strExcelFileTo, True, False, "")

    Set objExcelAppFrom = GetObject(strPathFrom & strExcelFileFrom).Application
    Set objExcelAppTo = GetObject(strPathTo & strExcelFileTo).Application

    For Each ws In objExcelAppFrom.Sheets
        strSQL = "SELECT * FROM [" & ws.Name & "$]"
        Set rst = New ADODB.Recordset
        rst.Open strSQL, strConn, adOpenUnspecified, adLockUnspecified
        objExcelAppTo.Activate
'        AppActivate objExcelAppTo.Windows(1).Caption
        objExcelAppTo.Sheets(i).Range("A7").CopyFromRecordset rst
        rst.Close
        Set rst = Nothing
        i = i + 1
    Next ws

ErrorHandler:
    If Err.Number <> 0 Then
        MsgBox Err.Number & ": " & Err.Description
'        Resume
    End If
    
End Sub
 
VBA Code:
objExcelAppTo.Activate

this line should not be necessary at all.
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
OK I will remove that. I have created a test MS Access db to show where the problem may be. Is it possible to upload the file here or is that not possible?
 
Upvote 0

Forum statistics

Threads
1,214,608
Messages
6,120,500
Members
448,968
Latest member
screechyboy79

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top