Macro syntax

alainfranco

New Member
Joined
Mar 7, 2012
Messages
38
Hello,

I am trying to set a variable that I can use throughout my macro. The variable is the filename with its associated path.

So far, Ive been succesfull in setting the variable and using it in certain objects, but not in others. The code is as follows:


Sub ScorecardImporter()
Dim SelectedFile As String
Dim ProjectClient As String


With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False"


If .Show = -1 Then
SelectedFile = .SelectedItems(1)
Else
End If
End With




Workbooks.Open (SelectedFile)


Windows("SelectedFile").Activate
Worksheets("SelectedFile").Activate
Range("B3:C3").Select
Selection.Copy
.....


The variable works well with "Workbooks.Open (SelectedFile)", however I get an error with the Windows and Worksheets objects....

Would anyone have a clue as to why this is not working?

Any help would be appreciated,

Thanks!
 

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
Hi alainfranco,

The error is because the SelectedFile variable comprises both the path and filename where once a workbook is open you select it only by its name.

To use your original code you need to extract the filename from the path and then select it like so:

Code:
Option Explicit
Sub ScorecardImporter()
    
    Dim SelectedFile As String
    Dim ProjectClient As String
    Dim strSelectedFileName As String
    Dim objFSO As Object
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        If .Show = -1 Then
            SelectedFile = .SelectedItems(1)
        End If
    End With
    
    Workbooks.Open (SelectedFile)
    
    strSelectedFileName = objFSO.GetFileName(SelectedFile)
    
    Workbooks(strSelectedFileName).Activate

End Sub

If we introduce a workbook variable you could do it like this:

Code:
Option Explicit
Sub ScorecardImporter()
    
    Dim SelectedFile As String
    Dim ProjectClient As String
    Dim wbSelectedFileName As Workbook
    
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        If .Show = -1 Then
            SelectedFile = .SelectedItems(1)
        End If
    End With
    
    Set wbSelectedFileName = Workbooks.Open(SelectedFile)
    
    wbSelectedFileName.Activate
    
End Sub

You still have to select which tab you want to copy the data from.

HTH

Robert
 
Last edited:
Upvote 0
Hi alainfranco,

The error is because the SelectedFile variable comprises both the path and filename where once a workbook is open you select it only by its name.

To use your original code you need to extract the filename from the path and then select it like so:

Code:
Option Explicit
Sub ScorecardImporter()
    
    Dim SelectedFile As String
    Dim ProjectClient As String
    Dim strSelectedFileName As String
    Dim objFSO As Object
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        If .Show = -1 Then
            SelectedFile = .SelectedItems(1)
        End If
    End With
    
    Workbooks.Open (SelectedFile)
    
    strSelectedFileName = objFSO.GetFileName(SelectedFile)
    
    Workbooks(strSelectedFileName).Activate

End Sub

If we introduce a workbook variable you could do it like this:

Code:
Option Explicit
Sub ScorecardImporter()
    
    Dim SelectedFile As String
    Dim ProjectClient As String
    Dim wbSelectedFileName As Workbook
    
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        If .Show = -1 Then
            SelectedFile = .SelectedItems(1)
        End If
    End With
    
    Set wbSelectedFileName = Workbooks.Open(SelectedFile)
    
    wbSelectedFileName.Activate
    
End Sub

You still have to select which tab you want to copy the data from.

HTH

Robert


Ah. Yes.

Many thanks Robert. Works like a charm.

Much appreciated, friend.
 
Upvote 0
Thanks for letting us know and I'm glad it all worked out :)
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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