Problem referencing 2 different workbooks

Mattestion

New Member
Joined
May 22, 2011
Messages
19
I'm working on a macro to open a new instance of Excel, hide it, open a workbook that the user selects and then import the data into the workbook that contains the macro. The problem I'm having is getting the names of the workbooks and sheets and then using that so I copy from the right workbook and put the data in the other one. I could hard-code it but I want to allow the user to be able to change the name of the workbook or sheet if need be. I also get the error "This object does not support this property or method" when I run the code I have so far:
Code:
Sub HomesImport()
    Dim appXL As Excel.Application
    Dim hBook As Workbook
    Dim hSheet As Worksheet
    Dim tBook As Workbook
    Dim tSheet As Worksheet
    
    Set tBook = ActiveWorkbook
    Set tSheet = ActiveSheet
    fileToOpen = Application.GetOpenFilename("Excel Files (*.xls), *.xls", , "Please select file to import", , False)
    
    If fileToOpen <> False Then
        Set appXL = New Excel.Application
        appXL.Visible = False
        appXL.Workbooks.Open fileToOpen
        Set hBook = ActiveWorkbook
        Set hSheet = ActiveSheet
    Else
        Set tBook = Nothing
        Set tSheet = Nothing
        Exit Sub
    End If
    
    key1 = "ABCDEFGHIJKL"
    key2 = "ABCDKGHIJFELM"
    
    For x = 2 To 98
        For y = 1 To 13
            If y <> 8 And y <> 13 Then
                tBook.tSheet.Range(Mid(key2, y, 1) & Trim(Str(x))) = hBook.hSheet.Range(Mid(key1, y, 1) & Trim(Str(x)))
            Else
                If y = 8 Then
                    tBook.tSheet.Range(Mid(key2, y, 1) & Trim(Str(x))) = TEL(hBook.hSheet.Range(Mid(key1, y, 1) & Trim(Str(x))))
                Else
                    tBook.tSheet.Range(Mid(key2, y, 1) & Trim(Str(x))) = "4"
                End If
            End If
        Next y
    Next x
    
    appXL.Quit
    Set appXL = Nothing
    Set tBook = Nothing
    Set tSheet = Nothing
    Set hBook = Nothing
    Set hSheet = Nothing
    Response = MsgBox("Done!", vbInformation, "File Import")
        
End Sub


Function TEL(telnum)
    If Left(telnum, 1) = "(" Then
        TEL = Mid(telnum, 2, 3) & "-" & Right(telnum, 8)
    Else
        TEL = telnum
    End If
End Function
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Why a new instance of Excel?

Can't you just open the workbook in the same instance of Excel that contains the workbook with the code in it?

If you turn off screen updating the user should never even notice it happening.

PS Where are you getting the error?
 
Upvote 0
I was opening a new instance because that was the solution I found, but I have no problem using your suggestion, it would be simpler. As to the error, it seems to be happening at this line
Code:
tBook.tSheet.Range(Mid(key2, y, 1) & Trim(Str(x))) = hBook.hSheet.Range(Mid(key1, y, 1) & Trim(Str(x)))
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,626
Members
452,933
Latest member
patv

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