VERIFICARE PRESENZA FILE DINAMICO

Anonimo
2015-10-20T13:38:19+00:00

Ciao a tutti,

non riesco a completare questa macro di controllo se un file dinamico esiste, nella directory devo verificare se il file 2015.xlsm è presente oppure no, per evitare l'errore di debug se non lo trova.

Sub VerificaPresenzaFile()

    Dim Nome As String

    Dim aa As Worksheet

    Set aa = Worksheets("AB1")

    Dim FileInQuestion As String

    Nome = aa.Cells(6, 7).Value ' CELLA G6 CHE CONTIENE IL NOME DEL FILE (2015.xlsm)

    FileInQuestion = Dir("C:\AB\FATTURE\Nome")

  If FileInQuestion = "" Then   ' non trova il file ed esce

     MsgBox "IL FILE NON ESISTE!"

     GoTo FineVerifica

  Else   ' il file esiste e lo apre

      Workbooks.Open Filename:=Nome & ".xlsm", UpdateLinks:=0

  End If

FineVerifica:

End Sub

Grazie

Roberto

Microsoft 365 e Office | Excel | Per la casa | Windows

Domanda bloccata. Questa domanda è stata eseguita dalla community del supporto tecnico Microsoft. È possibile votare se è utile, ma non è possibile aggiungere commenti o risposte o seguire la domanda.

0 commenti Nessun commento
Risposta accettata dall'autore della domanda
Anonimo
2015-10-20T16:22:40+00:00

Ciao Roberto,

Sempre utilizzando la funzione Dir, prova qualcosa del genere:

'=========>>

Option Explicit

'--------->>

Public Sub VerificaPresenzaFile()

    Dim WB As Workbook, WB2 As Workbook

    Dim SH As Worksheet

    Dim Rng As Range

    Dim sNome As String, sFilename As String

    Dim FileInQuestion As String

    Const sEstensione As String = ".xlsm"

    Const sPercoroso = "C:\AB\FATTURE"

    Set WB = ThisWorkbook

    Set SH = WB.Worksheets("AB1")

    Set Rng = SH.Cells(6, 7)

    sNome = Rng.Value

    sFilename = sPercoroso & sNome & sEstensione

    If Dir(sFilename) <> vbNullString Then

        '// File trovato

        Set WB2 = Workbooks.Open(FileName:=sFilename, _

                                 UpdateLinks:=0)

    Else

        GoTo FineVerifica

    End If

    Exit Sub

FineVerifica:

'// File non trovato

    MsgBox sFilename & " non e' stato trovato"

End Sub

'<<=========

Nota: Vorrei discorragiarti dal uso di AB1 come nome di un foglio in quanto sia un riferimento valido ad una cella su ogni foglio.

===

Regards,

Norman

La risposta è stata utile?

0 commenti Nessun commento

5 risposte aggiuntive

Ordina per: Più utili
  1. Anonimo
    2015-10-22T09:05:50+00:00

    Ciao Sandro,

    grazie per il supporto,

    ho comunque già risolto il problema,

    la tua macro è diversa da quella che ho adattato ma è comunque utile per imparare una modalità diversa per raggiungere un'obiettivo.

    Ciao e alla prossima.

    Roberto

    La risposta è stata utile?

    0 commenti Nessun commento