PDA

View Full Version : Solved: UserForm Import Form



TedMosby
03-24-2009, 07:57 AM
I have a workbook that is used as an input workbook by some users.
What I want to do is import a range from this workbook into the workbook attached. In the attached workbook on I have a Import Form where I can select the relevant workbook. I then want to be able to click import and it then goes to the source workbook and imports to the range specified.
The code below is what I have got, the problem I have is that it doesnt recognize the range of data to import.
Where have I gone worng?



Private Sub cmdImport_Click()
'Code for Import button
Dim fs As Variant, ImportFile As Workbook, ChosenFile As String
Application.ScreenUpdating = False
With Me
ChosenFile = .txtFilePath.Value
If .chkSetup = False Then
MsgBox "Please select something to import"
Exit Sub
End If
Set ImportFile = getImportFile(ChosenFile, 0)
If Not ImportFile Is Nothing Then
Application.StatusBar = "Importing Rota..."
If .chkSetup Then
ImportSetup ImportFilePath:=ChosenFile
End If
End If
ImportFile.Close savechanges:=False
MsgBox "Data import complete !", vbInformation, "Import Complete"
End With
Application.StatusBar = False
ThisWorkbook.Sheets("Rota").Activate
Application.ScreenUpdating = True
Unload Me
End Sub




Private Function getImportFile(ImportFilePath As String, FirstChoice As Integer) As Workbook
'code to get import file
Dim fs As Variant, boolOldUpdating As Boolean
boolOldUpdating = Application.ScreenUpdating
Application.ScreenUpdating = False
Set fs = CreateObject("Scripting.FileSystemObject")
If Not fs.FileExists(ImportFilePath) Then
Exit Function
End If
' Open file and check that the Engineer Sheet is inside
Application.EnableEvents = False
Set getImportFile = Workbooks.Open(ImportFilePath, updateLinks:=False, ReadOnly:=True)
Application.EnableEvents = True
Set getImportFile = getImportFile(Mid(ImportFilePath, 1, InStrRev(ImportFilePath, "\")) & "HospAtHome\StaffRota.xls", 1)
If getImportFile Is Nothing Then MsgBox "Unable to find the Staff Rota.xls file for this Rota Import."
If FirstChoice = 0 Then
getImportFile.Close savechanges:=False
MsgBox "Please choose the Rota Import file", vbExclamation, "Unable to open file."
Set getImportFile = Nothing
End If
Application.ScreenUpdating = boolOldUpdating
End Function




Private Sub ImportSetup(Optional ImportFilePath As String, Optional ImportFile As Workbook)
If ImportFile Is Nothing Then
Application.EnableEvents = False
Set ImportFile = Application.Workbooks.Open(Filename:=ImportFilePath, ReadOnly:=True)
Application.EnableEvents = True
End If
With ImportFile.Sheets("Rota")
Application.StatusBar = "Importing setup details...Importing new data"
.Range("B6:E303").Copy Destination:=ThisWorkbook.Sheets("Rota").Range("B5")
End With
ImportFile.Close savechanges:=False
Application.CutCopyMode = False
End Sub