UserForms and Creating an Import Data Form

TedMosby

New Member
Joined
Jan 8, 2009
Messages
4
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?
I cant attach a copy of what I'm doing as this profile doesnt seem to let me attach a copy.

Code:
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

Code:
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

Code:
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
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Forum statistics

Threads
1,215,022
Messages
6,122,721
Members
449,093
Latest member
Mnur

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