Opening an Excel file from VBA code

Solved
TraderAS Posted messages 119 Status Member -  
pijaku Posted messages 13513 Registration date   Status Moderator Last intervention   -
Hello,

I want to open an Excel file using the following code:

Sub Ouvrir()
Workbooks.Open Filename:="C:\Documents and Settings\Administrateur\Bureau\TRADING\Action.xlsx"
End Sub

However, when I run it, I get this error
==> Incorrect statement outside of a procedure, and it highlights this part in blue:
C:\Documents and Settings\Administrateur\Bureau\TRADING\Action.xlsx

Thank you.

2 answers

  1. pijaku Posted messages 13513 Registration date   Status Moderator Last intervention   2 773
     
    Hello,

    In VBA Excel, try this:
    Sub Open() Dim Path As String, FileName As String Path = "C:\Documents and Settings\Administrator\Desktop\TRADING\" FileName = "Action.xlsx" Workbooks.Open Filename:= Path & FileName End Sub

    Sources
    --
    Best regards,
    Franck P
    10