VBA open file to pull data from, if unable ask what file to pull from

pookexvi

New Member
Joined
Mar 9, 2022
Messages
11
I'm trying to have it pull data from a separate file got that working fine. when I try to get it to ask what file if it cant automatically find the fie is when I run in to problems. (i cant get it to say with 'else' in the code)

VBA Code:
 If Range("'cure data'!$A$1").Value = "" Then Workbooks.Open "F:\TMA01\TMA01002.xls"
    Set wkbSourceBook = ActiveWorkbook
    Set rngSourceRange = Range("A1:L2000")
    wkbCrntWorkBook.Activate
    Set rngDestination = Range("'cure data'!$A$1")
    rngSourceRange.Copy rngDestination
    rngDestination.CurrentRegion.EntireColumn.AutoFit
    wkbSourceBook.Close False
    
    else
     With Application.FileDialog(msoFileDialogOpen)
    .Filters.Clear
    .Filters.Add "Excel 2007-13", "*.xlsx; *.xlsm; *.xlsa"
    .AllowMultiSelect = False
    .Show
    If SelectedItems.Count > 0 Then
    Workbooks.Open .SelectedItems(1)
    Set wkbSourceBook = ActiveWorkbook
    Set rngSourceRange = Range("A1:L2000")
    wkbCrntWorkBook.Activate
    Set rngDestination = Range("'cure data'!$A$1")
    rngSourceRange.Copy rngDestination
    rngDestination.CurrentRegion.EntireColumn.AutoFit
    wkbSourceBook.Close False
 
for anyone still looking way in the future. here is the code that I managed to get to work.

VBA Code:
    Dim DefaultFileExists   As Boolean
    Dim DefaultFileName     As String
    Dim wkbCrntWorkBook As Workbook
    Dim wkbSourceBook As Workbook
    Dim rngSourceRange As Range
    Dim rngDestination As Range
    Set wkbCrntWorkBook = ActiveWorkbook

'
On Error GoTo ErrH
                Workbooks.Open "F:\TMA01\TMA01002.xls"
                
                    
            Set wkbSourceBook = ActiveWorkbook
            Set rngSourceRange = Range("A1:L2000")
            wkbCrntWorkBook.Activate
            Set rngDestination = Range("'cure data'!$A$1")
            rngSourceRange.Copy rngDestination
            rngDestination.CurrentRegion.EntireColumn.AutoFit
            wkbSourceBook.Close False
            

    DefaultFileExists = (Dir(DefaultFileName) <> "")
    Exit Sub
 
ErrH:


        With Application.FileDialog(msoFileDialogOpen)
            .Filters.Clear
            .Filters.Add "Excel 2007-13", "*.xlsx; *.xlsm; *.xls"
            .AllowMultiSelect = False
            .Show
            If .SelectedItems.Count > 0 Then
                Workbooks.Open .SelectedItems(1)
                Set wkbSourceBook = ActiveWorkbook
                 Set rngSourceRange = Range("A1:L2000")
            wkbCrntWorkBook.Activate
            Set rngDestination = Range("'cure data'!$A$1")
                rngSourceRange.Copy rngDestination
                rngDestination.CurrentRegion.EntireColumn.AutoFit
                wkbSourceBook.Close False
            End If
        End With
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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