Notifications
Clear all

VBA invertendo mês e dia (data de modificação de um arquivo)

4 Posts
2 Usuários
0 Reactions
1,238 Visualizações
(@robo8268)
Posts: 73
Trusted Member
Topic starter
 

Pessoal, tudo bem?

Eu preciso popular uma planilha com o nome do arquivo e a data de modificação.

Para fazer isso, eu primeiro populo um array com estes dados, e depois copio para a planilha.

Quando eu populo o Array, funciona corretamente, porém quando eu coloco o valor na célula, o excel está invertendo o dia e o mês da data de modificação.

Infelizmente não consigo upar uma planilha de exemplo, pois aqui no meu trabalho é bloqueado, porém posso fazer isso quando chegar em casa se necessário.

Este é o trecho em que ocorre o erro


For i = 0 To contagemArquivos
j = i + 2
'a data fica na coluna 0 do array listaArquivos
   arquivos.Range("A" & j) = listaArquivos(i, 0)
   arquivos.Range("B" & j) = listaArquivos(i, 1)
Next i

E o mais estranho, é que isso acontece apenas com um arquivo. A data de modificação dele é 07/06/2016 e ele inverte para 06/07/2016.

Segue código completo.

Option Explicit

Sub Buscar()
Dim fso As Object
Dim pasta As Object
Dim file As Object
Dim listaArquivos() As Variant, listaFinal As Variant
Dim caminho1 As String, caminho2 As String
Dim buscador As Worksheet, arquivos As Worksheet
Dim GCANI As Workbook
Dim infra As Worksheet
Dim funcional As String
Dim dataReferencia As Date
Dim tipo As String
Dim contagemArquivos As Integer, i As Integer, contagemLista As Integer, j As Integer
Dim rng As Range, rngFind As Range, rngOrdenado As Range
Dim dados(1 To 1, 1 To 5)
Dim linha As Double
Dim final As Integer
Dim msg As VbMsgBoxResult
Dim segundaBusca As Boolean

segundaBusca = False


Set buscador = ThisWorkbook.Sheets("Buscador")
Set arquivos = ThisWorkbook.Sheets("arquivos")
funcional = buscador.Range("B2").Value
dataReferencia = buscador.Range("B3").Value
tipo = buscador.Range("B4").Value


If funcional = "" Then
   MsgBox "O campo funcional é obrigatório"
   Exit Sub
End If
If IsEmpty(dataReferencia) Then
   MsgBox "O campo data de referência é obrigatório"
   Exit Sub
End If
If tipo = "" Then
   MsgBox "O campo tipo é obrigatório"
   Exit Sub
End If


If tipo = "PF" Then
        caminho1 = "\fswcorpceicaccmonitqualGCANI - Cadastro UnificadoGCANI_BANCO_PF"
Else
        caminho1 = "\fswcorpceicaccmonitqualGCANI - Cadastro Unificado"
End If


Set fso = CreateObject("Scripting.FileSystemObject")
Set pasta = fso.GetFolder(caminho1)
novaBusca:
For Each file In pasta.Files
If FormatDateTime(file.DateLastModified, vbShortDate) <= dataReferencia Then
   If Left(file.Name, 12) = "GESTAO_GCANI" Then
      contagemArquivos = contagemArquivos + 1
   End If
End If
Next

ReDim listaArquivos(contagemArquivos, 1)
i = 0
For Each file In pasta.Files
If FormatDateTime(file.DateLastModified, vbShortDate) <= dataReferencia Then
   If Left(file.Name, 12) = "GESTAO_GCANI" Then
      listaArquivos(i, 0) = FormatDateTime(file.DateLastModified, vbShortDate)
      listaArquivos(i, 1) = file.Name
      i = i + 1
   End If
End If


Next

arquivos.Range("A1").CurrentRegion.Value = ""
arquivos.Range("A1:B1") = Array("Data", "Arquivo")
For i = 0 To contagemArquivos
j = i + 2
   arquivos.Range("A" & j) = listaArquivos(i, 0)
   arquivos.Range("B" & j) = listaArquivos(i, 1)
Next i


contagemLista = arquivos.Cells(Rows.Count, "A").End(xlUp).Row

Set rng = arquivos.Range("A1:B" & arquivos.Cells(Rows.Count, "A").End(xlUp).Row)
final = arquivos.Cells(Rows.Count, "A").End(xlUp).Row
Call organizar(final)
If arquivos.Range("A" & arquivos.Cells(Rows.Count, "A").End(xlUp).Row).Value = "Data" Then
   dataReferencia = Date
   segundaBusca = True
   
   GoTo novaBusca
Else
  If segundaBusca Then
        msg = MsgBox("O GCANI mais antigo da rede, foi modificado em " & arquivos.Range("A" & arquivos.Cells(Rows.Count, "A").End(xlUp).Row).Value & ". Deseja buscar nas versões mais recentes?", vbYesNo)
        If msg = vbNo Then
           Exit Sub
        End If
   End If
   
End If

listaFinal = rng
buscador.Range("B5:B10").Value = ""
For i = 2 To contagemArquivos + 1
caminho2 = caminho1
caminho2 = caminho2 & listaFinal(i, 2)
   Set GCANI = Workbooks.Open(caminho2)
   Set infra = GCANI.Sheets("BASE")
  If tipo = "CARTÕES" Then
   With infra.Range("B:B")
   On Error Resume Next

   Set rngFind = .Find(What:=funcional, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
    End With
    Else
     With infra.Range("A:A")
     On Error Resume Next

     Set rngFind = .Find(What:=funcional, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
    End With
    End If
    If Not rngFind Is Nothing Then
       MsgBox "Operador encontrado no GCANI " & listaFinal(i, 2) & " Modificado em: " & listaFinal(i, 1)
       buscador.Range("B5") = rngFind.Offset(0, 2)
       buscador.Range("B6") = rngFind.Offset(0, 3)
       buscador.Range("B7") = rngFind.Offset(0, 7)
       buscador.Range("B8") = rngFind.Offset(0, 4)
       buscador.Range("B9") = rngFind.Offset(0, 1)
       buscador.Range("B10") = rngFind.Offset(0, 5)
       Exit For
    Else: GoTo proximaIteração
    End If
proximaIteração:
GCANI.Close SaveChanges:=False
Next i
If rngFind Is Nothing Then
  MsgBox "Operador não encontrado"
End If
On Error Resume Next
GCANI.Close SaveChanges:=False
End Sub

Obrigado.

 
Postado : 14/06/2016 1:47 pm
(@messiasmbm)
Posts: 223
Estimable Member
 

Vê se atende...

 
Postado : 14/06/2016 3:44 pm
(@robo8268)
Posts: 73
Trusted Member
Topic starter
 

Messias, obrigado por responder

Não entendi o que foi feito nesta planilha, mas acredito que não resolve o meu problema.

Obrigado.

 
Postado : 15/06/2016 5:40 am
(@robo8268)
Posts: 73
Trusted Member
Topic starter
 

Galera, acresentei estas linhas e resolvi...

If arquivos.Range("A" & j) = listaArquivos(i, 0) Then
arquivos.Range("A" & j).NumberFormat = "dd/mm/yyyy"
Else
arquivos.Range("A" & j).NumberFormat = "mm/dd/yyyy"
End If

 
Postado : 15/06/2016 6:08 am