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