I would like to import 2 worksheets form another workbook but I am having some trouble toggling between the source and destination workbooks. How can I get the file name of the workbook that I am opening so that I can switch between the two and also close it at the end? I am repurposing this code form an earlier project where I had the source workbook information in one of the cells, but that is not available this time around. any thoughts on what I need to add to this?
thanks,
thanks,
Code:
Sub Returner()
Dim tabname1 As String
Dim sheetname1 As String
Dim SaveName As String
Dim openfile As String
'On Error GoTo Errhandle '<---- Diabled while testing
Application.DisplayAlerts = False
Application.AskToUpdateLinks = False
SaveName = ""
sheetname1 = "temp" '<---- Destination 1
sheetname2 = "temp2" '<---- Destination 2
tabname1 = "IOC_Import" '<---- Source 1
tabname2 = "Financial Summary EV4" '<---- Source 2
'open the survey
'MsgBox "Pls select a survey to import", vbOKOnly
'openfile = Application.GetOpenFilename("Excel Files (*.xlsm),*.xlsm", , "Open a spreadsheet...")
openfile = Application.GetOpenFilename("Excel Files (*.xls*),*.xls*", , "Open a spreadsheet...")
Workbooks.Open FileName:=openfile, ReadOnly:=True
'SaveName = Range("a1").Value & ".xlsx"
SaveName = ThisWorkbook.Name '<---- This gives me the destination not the source name
'copy the contents of the survey
Sheets(tabname1).Select
Cells.Select
Selection.Copy
Windows(WorkbookName).Activate
Sheets(sheetname1).Activate
Range("A1").Select
ActiveSheet.Paste
Range("A1").Select
Windows(SaveName).Activate '<---- Activate the Source file again
Sheets(tabname2).Select
Cells.Select
Selection.Copy
Windows(WorkbookName).Activate
Sheets(sheetname2).Activate
Range("A1").Select
ActiveSheet.Paste
Range("A1").Select
'close the survey
Windows(SaveName).Activate '<---- Close the Source file
ActiveWindow.Close
Application.DisplayAlerts = True
Application.AskToUpdateLinks = True
'Errhandle:
Exit Sub
End Sub
Public Function WorkbookName() As String
'Finds the name of this spreadsheet
WorkbookName = ThisWorkbook.Name
End Function