XL 2019 Changement de chemin

tokou

XLDnaute Nouveau
Bonjour , j'ai trouvé un code de Jacky67 que je remercie ,( pour extraire le contenu d'un autre fichier dans un même dossier ), je cherche à extraire les contenus dans un autre répertoire .

Je vous remercie pour toutes aide .


VB:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address <> "$C$2" Then Exit Sub
    If Target = "" Then Exit Sub
    Application.EnableEvents = False: Application.ScreenUpdating = False
    Range("a3:i" & Rows.Count).Clear
    If Dir(ThisWorkbook.Path & "\nom" & Space(1) & [c2] & ".xlsx") <> "" Then
        Workbooks.Open Filename:=ThisWorkbook.Path & "\nom" & Space(1) & [c2] & ".xlsx"
        ActiveWorkbook.Sheets("feuil1").Range("a3:i" & ActiveWorkbook.Sheets("feuil1").Cells.Find("*", , , , xlByRows, xlPrevious).Row).Copy ThisWorkbook.Sheets("Planning").Range("a3")
        ActiveWindow.Close
    Else
        MsgBox "nom" & Space(1) & [c2] & vbLf & "Classeur inexistant", vbInformation, "Information"
    End If
    Application.EnableEvents = True
End Sub


j'ai essayé ceci mais j'arrive pas

ThisWorkbook.Path & "S:\dossier1\""nom" & Space(1) & [c2] & ".xlsx"
 
Solution
Bonjour.
Vous y verrez sûrement plus clair en faisant comme ça :
VB:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
   Dim RefFic As String, Wbk As Workbook, Wsh As Worksheet
   If Target.Address <> "$C$2" Then Exit Sub
   If Target = "" Then Exit Sub
   Application.EnableEvents = False: Application.ScreenUpdating = False
'   RefFic = ThisWorkbook.Path & "\nom" & Space(1) & [C2] & ".xlsx"  '  ?
   RefFic = "S:\dossier1\nom " & [C2] & ".xlsx"
   Range("A3:I" & Rows.Count).Clear
   If Dir(RefFic) <> "" Then
      Set Wbk = Workbooks.Open(Filename:=RefFic)
      Set Wsh = Wbk.Sheets("feuil1")
      Wsh.Range("A3:I" & Wsh.Cells.Find("*", , , , xlByRows, xlPrevious).Row).Copy Me.Range("A3")
      Wbk.Close
   Else...

ChTi160

XLDnaute Barbatruc
Bonjour tokou
je pense sans avoir testé que cela ne peut fonctionner
ThisWorkbook.Path & "S:\dossier1\""nom" & Space(1) & [c2] & ".xlsx"

ThisWorkbook.Path étant le chemin du Dossier ouvert .

et tu y ajoutes le chemin vers le Fichier Cible soit un Mix des deux chemins.

Il faudrait un truc du genre.
VB:
If Dir("S:\dossier1\" & nom & Space(1) & [c2] & ".xlsx") <> "" Then
        Workbooks.Open Filename:="S:\dossier1\" & nom & Space(1) & [c2] & ".xlsx"

si nom est une variable !

Bonne journée
Jean marie
 

Dranreb

XLDnaute Barbatruc
Bonjour.
Vous y verrez sûrement plus clair en faisant comme ça :
VB:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
   Dim RefFic As String, Wbk As Workbook, Wsh As Worksheet
   If Target.Address <> "$C$2" Then Exit Sub
   If Target = "" Then Exit Sub
   Application.EnableEvents = False: Application.ScreenUpdating = False
'   RefFic = ThisWorkbook.Path & "\nom" & Space(1) & [C2] & ".xlsx"  '  ?
   RefFic = "S:\dossier1\nom " & [C2] & ".xlsx"
   Range("A3:I" & Rows.Count).Clear
   If Dir(RefFic) <> "" Then
      Set Wbk = Workbooks.Open(Filename:=RefFic)
      Set Wsh = Wbk.Sheets("feuil1")
      Wsh.Range("A3:I" & Wsh.Cells.Find("*", , , , xlByRows, xlPrevious).Row).Copy Me.Range("A3")
      Wbk.Close
   Else
      MsgBox "Casseur introuvable :" & vbLf & RefFic, vbInformation, "Information"
      End If
   Application.EnableEvents = True
   End Sub
 
Dernière édition:

tokou

XLDnaute Nouveau
Ma variable SEM il prend ça comme une erreur , je comprends pas

VB:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address <> "$C$2" Then Exit Sub
    If Target = "" Then Exit Sub
    Application.EnableEvents = False: Application.ScreenUpdating = False
    Range("a3:i" & Rows.Count).Clear
    If Dir("S:\02 Planning\" & SEM & Space(1) & [c2] & ".xlsx") <> "" Then
        Workbooks.Open Filename:="S:\02 Planning\" & SEM & Space(1) & [c2] & ".xlsx"
        ActiveWorkbook.Sheets("feuil1").Range("a3:i" & ActiveWorkbook.Sheets("feuil1").Cells.Find("*", , , , xlByRows, xlPrevious).Row).Copy ThisWorkbook.Sheets("Planning").Range("a3")
        ActiveWindow.Close
    Else
        MsgBox "SEM" & Space(1) & [c2] & vbLf & "Classeur inexistant", vbInformation, "Information"
    End If
    Application.EnableEvents = True
End Sub
 

Dranreb

XLDnaute Barbatruc
Elle n'est ni déclarée ni initialisée.
Non vraiment utilisez une variable NomFic: outre que ça ne vous obligera plus à écrire 3 fois la même expression, et même parfois, à tort, différemment, ça vous garantira que vous servez bien partout la même chose, aussi bien au Dir et à l'Open qu'au MsgBox. De plus si elle devait changer vous n'auriez à la modifier qu'à un seul endroit.
VB:
RéfFic = "S:\02 Planning\SEM " & [C2] & ".xlsx"
 
Dernière édition:

tokou

XLDnaute Nouveau
le truc c'est que dans le code de base je ne vois pas de variable déclaré ( c'est un dossier SEM 1 , SEM 2 ect...) il ouvre direct


VB:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address <> "$C$2" Then Exit Sub
    If Target = "" Then Exit Sub
    Application.EnableEvents = False: Application.ScreenUpdating = False
    Range("a3:i" & Rows.Count).Clear
    If Dir(ThisWorkbook.Path & "\SEM" & Space(1) & [c2] & ".xlsx") <> "" Then
        Workbooks.Open Filename:=ThisWorkbook.Path & "\SEM" & Space(1) & [c2] & ".xlsx"
        ActiveWorkbook.Sheets("feuil1").Range("a3:i" & ActiveWorkbook.Sheets("feuil1").Cells.Find("*", , , , xlByRows, xlPrevious).Row).Copy ThisWorkbook.Sheets("Planning").Range("a3")
        ActiveWindow.Close
    Else
        MsgBox "SEM" & Space(1) & [c2] & vbLf & "Classeur inexistant", vbInformation, "Information"
    End If
    Application.EnableEvents = True
End Sub
 

Dranreb

XLDnaute Barbatruc
Faut savoir: vous reprenez ThisWorkbook.Path. Ce classeur est-il bien sur ce fameux "S:\02 Planning" dont vous avez parlé plus haut et qui est d'ailleurs différent de ce que vous avez cité fin du poste #1 ?
Avez vous vu mon conseil ?
 
Dernière édition:

Discussions similaires

Réponses
2
Affichages
252
  • Résolu(e)
XL 2021 macro
Réponses
9
Affichages
428

Statistiques des forums

Discussions
311 723
Messages
2 081 932
Membres
101 844
dernier inscrit
pktla