0

Olá, tenho uma macro que abre a última planilha(pela data mais recente) da minha pasta, porém não funciona. Na teoria era pra ela estar puxando da minha pasta base a última planilha e abrindo, alguém poderia me ajudar? segue código da macro que estou utilizando

Sub AbrirRecentes()

'Abre o arquivo mais recente da pasta "base" (Último arquivo)

Dim MyPath As String

Dim MyFile As String

Dim LatestFile As String

Dim LatestDate As Date

Dim LMD As Date


'Encaminha usuário a pasta onde tem os arquivos "originais" que serão abertos e formatados

MyPath = "\\sad154000\Corporativo\Gepej\#NOVA_ESTRUTURA\Estagiarios\Álax\TaxaBacen\base"



If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"

MyFile = Dir(MyPath & "* .xls", vbNormal)

If Len(MyFile) = 0 Then

    MsgBox "Nenhum arquivo foi encontrado …", vbExclamation
    
    Exit Sub

End If


Do While Len(MyFile) > 0

    LMD = FileDateTime(MyPath & MyFile)
    
    If LMD > LatestDate Then

        LatestFile = MyFile
        
        LatestDate = LMD

End If

    MyFile = Dir

Loop

    Workbooks.Open MyPath & LatestFile

End Sub

1 Resposta 1

0

Na linha abaixo há uma falha, representada pelo espaço entre o asterisco e o ponto:

MyFile = Dir(MyPath & "* .xls", vbNormal)

Complementando, sugiro alterar a máscara para:

MyFile = Dir(MyPath & "*.xl?*", vbNormal)

de forma a permitir que sejam listados, também, os arquivos do Excel 2007+

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .