Notifications
Clear all

Macro buscar arquivos no pc

8 Posts
2 Usuários
0 Reactions
1,907 Visualizações
(@charlie-81)
Posts: 290
Reputable Member
Topic starter
 

Pessoal... preciso de uma macro que faça busca no pc, como se eu estivesse abrindo o explorer para encontrar algum arquivo.

 
Postado : 17/09/2012 5:36 pm
(@fernandofernandes)
Posts: 43750
Illustrious Member
 

Boa noite!!

Não entendi, procurar somente arquivos de Excel??

Tente adaptar...

Sub Macro1()
   Dim objFSO, fso
   Set objFSO = CreateObject("Scripting.FileSystemObject")

   Set mainFolder = objFSO.GetFolder("Seu_Diretório")
   For Each sFold In mainFolder.subfolders
      For Each myFile In sFold.Files
         If myFile.Name Like "*xyz*" Then
            myFile.Copy "Destino"
         End If
      Next
   Next
   
   Set objFSO = Nothing
   Set mainFold = Nothing
  
End Sub
 
Postado : 17/09/2012 5:49 pm
(@charlie-81)
Posts: 290
Reputable Member
Topic starter
 

não somente excel... Bom, para ser mais direto: Uma macro que abra o explorer para que eu possa navegar entre as pastas do computador.

 
Postado : 17/09/2012 5:51 pm
(@fernandofernandes)
Posts: 43750
Illustrious Member
 

Boa noite!!

Tente adaptar.
Fonte: http://www.vbaexpress.com/kb/getarticle.php?kb_id=800

Option Explicit
 
Sub SrchForFiles()
     ' Searches the selected folders and sub folders for files with the specified
     'extension.  .xls, .doc, .ppt, etc.
     'A new worksheet is produced called "File Search Results".  You can click on the link and go directly
     'to the file you need.
    Dim i As Long, z As Long, Rw As Long
    Dim ws As Worksheet
    Dim y As Variant
    Dim fLdr As String, Fil As String, FPath As String
     
    y = Application.InputBox("Por favor entre com a extenção do arquivo", "Info Request")
    If y = False And Not TypeName(y) = "String" Then Exit Sub
    Application.ScreenUpdating = False
     '*******************************************************************
     'fLdr = BrowseForFolderShell
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Show
        fLdr = .SelectedItems(1)
    End With
     '*******************************************************************
    With Application.FileSearch
        .NewSearch
        .LookIn = fLdr
        .SearchSubFolders = True
        .Filename = y
        Set ws = ThisWorkbook.Worksheets.Add(Sheets(1))
        On Error GoTo 1
2:                      ws.Name = "FileSearch Results"
        On Error GoTo 0
        If .Execute() > 0 Then
            For i = 1 To .FoundFiles.Count
                Fil = .FoundFiles(i)
                 'Get file path from file name
                FPath = Left(Fil, Len(Fil) - Len(Split(Fil, "")(UBound(Split(Fil, "")))) - 1)
                If Left$(Fil, 1) = Left$(fLdr, 1) Then
                    If CBool(Len(Dir(Fil))) Then
                        z = z + 1
                        ws.Cells(z + 1, 1).Resize(, 4) = _
                        Array(Dir(Fil), _
                        FileLen(Fil) / 1000, _
                        FileDateTime(Fil), _
                        FPath)
                        ws.Hyperlinks.Add Anchor:=Cells(z + 1, 1), _
                        Address:=.FoundFiles(i)
                    End If
                End If
            Next i
        End If
    End With
     
    ActiveWindow.DisplayHeadings = False
     
    With ws
        Rw = .Cells.Rows.Count
        With .[A1:D1]
            .Value = [{"Full Name","Kilobytes","Last Modified", "Path"}]
            .Font.Underline = xlUnderlineStyleSingle
            .EntireColumn.AutoFit
            .HorizontalAlignment = xlCenter
        End With
        .[E1:IV1 ].EntireColumn.Hidden = True
        On Error Resume Next
        Range(Cells(Rw, "A").End(3)(2), Cells(Rw, "A")).EntireRow.Hidden = True
        Range(.[A2 ], Cells(Rw, "C")).Sort [A2 ], xlAscending, Header:=xlNo
    End With
     
    Application.ScreenUpdating = True
    Exit Sub
1:          Application.DisplayAlerts = False
    Worksheets("FileSearch Results").Delete
    Application.DisplayAlerts = True
    GoTo 2
End Sub
 
 
Postado : 17/09/2012 6:18 pm
(@charlie-81)
Posts: 290
Reputable Member
Topic starter
 

Vou lhe agradecer pela resposta, mas ainda não é o que quero. Acho que deve existir algum comando que ao executar a macro ela abra o explorer.

 
Postado : 17/09/2012 6:31 pm
(@charlie-81)
Posts: 290
Reputable Member
Topic starter
 

encontrei... vou fechar o tópico

 
Postado : 17/09/2012 6:52 pm
(@fernandofernandes)
Posts: 43750
Illustrious Member
 

Charlie-81

manda aí... quero ver.

 
Postado : 17/09/2012 6:54 pm
(@charlie-81)
Posts: 290
Reputable Member
Topic starter
 

opa... em um módulo coloque:

Sub Abrir_Explorer()
    Dim Charlie81_File As String
    Charlie81_File = Application.GetOpenFilename( _
    "Charlie81 - Buscar (*.*), *.*", _
    , "Suporte: valdiney-duarte@hotmail.com", _
    , _
    True)
    Exit Sub
End Sub

Depois é só atribuir à algum botão...

 
Postado : 17/09/2012 7:00 pm