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
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Maybe this:

VBA Code:
    If Range("'cure data'!$A$1").Value = "" Then
        Workbooks.Open "F:\TMA01\TMA01002.xls"
    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)
        End With
    End If
'
    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
 
Upvote 0
Maybe this:

VBA Code:
    If Range("'cure data'!$A$1").Value = "" Then
        Workbooks.Open "F:\TMA01\TMA01002.xls"
    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)
        End With
    End If
'
    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
comes up with run time errror '1004'
couldnt find "F:\TMA01\TMA01002.xls"
 
Upvote 0
Now I understand your question.

VBA Code:
    Dim DefaultFileExists   As Boolean
    Dim DefaultFileName     As String
'
    DefaultFileName = "F:\TMA01\TMA01002.xls"
    On Error Resume Next
    DefaultFileExists = (Dir(DefaultFileName) <> "")
    On Error GoTo 0
'
    If Range("'cure data'!$A$1").Value = "" And DefaultFileExists Then
        Workbooks.Open DefaultFileName
    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)
        End With
    End If
'
    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
 
Upvote 0
Now I understand your question.

VBA Code:
    Dim DefaultFileExists   As Boolean
    Dim DefaultFileName     As String
'
    DefaultFileName = "F:\TMA01\TMA01002.xls"
    On Error Resume Next
    DefaultFileExists = (Dir(DefaultFileName) <> "")
    On Error GoTo 0
'
    If Range("'cure data'!$A$1").Value = "" And DefaultFileExists Then
        Workbooks.Open DefaultFileName
    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)
        End With
    End If
'
    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
THANK YOU. this is exactly what I was looking for.
 
Upvote 0
it worked a few times now I'm getting a new error. i hadn't changed the code at all.

run time error '424'

highlights 'wkbCrntWorkBook.Activate' near the botttom of the code you gave
 
Upvote 0
That is your code, that you provided on post #1. I only added code at the top to check if the default file exists.
 
Upvote 0
here is the entire code

VBA Code:
Sub Ctest()

Sheets("Analysis").Range("B2:B2", "B6:B6").ClearContents
Sheets("Cure Data").Range("A1:J1900", "A1: L2000").ClearContents
Sheets("Analysis").Range("B8:B8", "B8:B8").ClearContents

Sheets("Cure Data").Select
Cells(1, 1).Select
Sheets("Analysis").Select

    Dim DefaultFileExists   As Boolean
    Dim DefaultFileName     As String
'
    DefaultFileName = "F:\TMA01\TMA01002.xls"
    On Error Resume Next
    DefaultFileExists = (Dir(DefaultFileName) <> "")
    On Error GoTo 0
'
    If Range("'cure data'!$A$1").Value = "" And DefaultFileExists Then
        Workbooks.Open DefaultFileName
    Else
        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)
        End With
    End If
'
    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 Sub
 
Upvote 0
I don't see where you have told excel what 'wkbCrntWorkBook' is.
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,148
Members
452,615
Latest member
bogeys2birdies

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