Open a sheet from an existing workbook

gmazza76

Well-known Member
Joined
Mar 19, 2011
Messages
767
Office Version
  1. 365
Platform
  1. Windows
Good morning

I am trying to open a spreadsheet via vba to copy pieces of data into my current workbook.
I have a formula that changes the name based on the date etc.. that shows in the "telstats" reference below. (this changes daily)
I am getting an error on the "Set wb2" saying I have a syntax error, but I am unsure what I have missed as I normally use a static name instead of one that changes.

thanks in advance

Code:
Dim wb1 As Workbook, wb2 As Workbook
Dim telstats As String, Dim r As Range

Application.DisplayAlerts = False
Application.ScreenUpdating = False

telstats = Worksheets("Running System").Range("C9").Value

Set wb1 = ThisWorkbook
Set wb2 = Workbooks.Open("C:\Users\" & Environ("username") & "\OneDrive - Verastar\" & telstats & "))
[code/]
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I'm guessing the value in the 'telstats' variable is a workbook name.

Your issue is the last part.:

Without testing I think it should be:

VBA Code:
Set wb2 = Workbooks.Open("C:\Users\" & Environ("username") & "\OneDrive - Verastar\" & telstats)

If it still has issues, let me know
 
Upvote 0
that has worked, thank you.
Is there a way I can put a message if the file isnt found an error message i.e "file not open"
 
Upvote 0
Is there a way I can put a message if the file isnt found an error message i.e "file not open"

One way would be to use Dir Function to check filepath / Filename existence before trying to open it

something like

VBA Code:
Dim wb1 As Workbook, wb2 As Workbook
    Dim telstats As String, FolderPath As String
    Dim r As Range
    

     With Application
        .DisplayAlerts = False: .ScreenUpdating = False
     End With
    
    Set wb1 = ThisWorkbook
    
    telstats = wb1.Worksheets("Running System").Range("C9").Value
    FolderPath = "C:\Users\" & Environ("username") & "\OneDrive - Verastar\"
    
    If Not Dir(FolderPath & telstats, vbDirectory) = vbNullString Then
    Set wb2 = Workbooks.Open(FolderPath & telstats, False, True)
    
    ' do copy stuff
    
    
    'close without saving
    wb2.Close False
    
    Else
    'inform user
        MsgBox FolderPath & telstats & Chr(10) & Chr(10) & "File Not Found", 48, "Not Found"
    End If
    
    With Application
        .DisplayAlerts = True: .ScreenUpdating = True
    End With

another way would be to trap the error generated when trying to open the file that cannot be found.

Dave
 
Upvote 0
If I have the workbook I am copying from do I need to "Open" or can I just "activate" the other wb?
 
Upvote 0
If I have the workbook I am copying from do I need to "Open" or can I just "activate" the other wb?

You can only "Activate" an open workbook. Ideally in code, you'd check the file is open first, if it isn't you open it
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
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