Selezione file in cartella
Hai un problema con Excel? 
Selezione file in cartella
di Lucatop data: 09/07/2015 23:27:59
xx
Selezione file in cartella
di Lucatop data: 09/07/2015 23:28:52
Avrei bisogno di modificare la macro sotto in modo che mi selezioni un file txt nella stessa cartella in cui ho l'excel da cui sto lanciando la macro.
Il file che mi interessa selezionare è l'unico txt nella cartella.
Sotto, quella che ho al momento, che seleziona file di un determinato nome in una determinata cartella.
Grazie
Sub Macro5()
Sheets("1").Select
Range("A1").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:UsersLucaDesktopProva.txt" _
, Destination:=Range("$A$1"))
.Name = "Prova_1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Sheets("MACRO").Select
Range("A1").Select
End Sub |
di Luca73 data: 10/07/2015 09:25:39
Ciao
Prova a modificare così il Codice.
La parte iniziale trova il file con estensione.txt nel folder del file excel che lancia la macro.
attenzione che ho aggiunto un paio di righe anche in fondo
[...] significa che non ho trascritto il tuo codice.
Fammi sapere
Ciao
Luca
Sub Macro5()
Dim FileSysLT, FoldLT, FileLT, MioFile
Dim Vett As Variant
Set FileSysLT = CreateObject("Scripting.FileSystemObject")
Set FoldLT = FileSysLT.GetFolder(ActiveWorkbook.Path)
For Each FileLT In FoldLT.Files
Vett = Split(FileLT.Name, ".")
If Vett(UBound(Vett)) = "txt" Then
Set MioFile = FileLT
End If
Next
Sheets("1").Select
Range("A1").Select
With ActiveSheet.QueryTables.Add(Connection:= _
MioFile.Path _
, Destination:=Range("$A$1"))
.Name = "Prova_1"
[...]
End With
Sheets("MACRO").Select
Range("A1").Select
Set FileSysLT = Nothing
Set FoldLT = Nothing
End Sub |
di Lucatop data: 11/07/2015 16:12:06
Mi dà errore qui
With ActiveSheet.QueryTables.Add(Connection:= _
MioFile.Path _
, Destination:=Range("$A$1"))
Sub Macro5()
Dim FileSysLT, FoldLT, FileLT, MioFile
Dim Vett As Variant
Set FileSysLT = CreateObject("Scripting.FileSystemObject")
Set FoldLT = FileSysLT.GetFolder(ActiveWorkbook.Path)
For Each FileLT In FoldLT.Files
Vett = Split(FileLT.Name, ".")
If Vett(UBound(Vett)) = "txt" Then
Set MioFile = FileLT
End If
Next
Sheets("TXT").Select
Range("A1").Select
With ActiveSheet.QueryTables.Add(Connection:= _
MioFile.Path _
, Destination:=Range("$A$1"))
.Name = "Prova_1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Sheets("MACRO").Select
Range("A1").Select
Set FileSysLT = Nothing
Set FoldLT = Nothing
End Sub |
di Lucatop data: 18/07/2015 18:18:38
Qualcuno mi riesce a verificare il problema che ho segnalato nel messaggio precedente?
Grazie
...forse manca un pezzo..
di cromagno data: 18/07/2015 19:26:09
Credo che dovresti aggiungere "TEXT;" alla parte di codice dove ti da errore:
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & MioFile, Destination:=Range("$A$1")) |
di Lucatop data: 20/07/2015 23:11:19
Con il codice sotto mi importa l'ultimo TXT in ordine alfabetico presente nella cartella del file excel su sui sto lavorando.
Mi va bene lo stesso però sono curioro di capire l'errore.
Sub AAAImportazioneTxt()
Dim FileSysLT, FoldLT, FileLT, MioFile
Dim Vett As Variant
Set FileSysLT = CreateObject("Scripting.FileSystemObject")
Set FoldLT = FileSysLT.GetFolder(ActiveWorkbook.Path)
For Each FileLT In FoldLT.Files
Vett = Split(FileLT.Name, ".")
If Vett(UBound(Vett)) = "txt" Then
Set MioFile = FileLT
End If
Next
Sheets("TXT").Select
Range("A1").Select
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & MioFile, Destination:=Range("$A$1"))
.Name = "Prova_1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Sheets("MACRO").Select
Range("A1").Select
Set FileSysLT = Nothing
Set FoldLT = Nothing
Range("A4").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 5296274
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("A1").Select
End Sub
|
di cromagno data: 21/07/2015 00:03:18
Io di solito seguo la "guida in linea" di VBA e secondo quanto ho capito, la parte:
Connection:="TEXT; etc...
serve a specificare che stai effettuando una connessione ad un file di testo. Nella guida c'è scritto questo:
"The data source for the query table. Can be one of the following:
A text file. A string in the form "TEXT;", where TEXT is required but not localized."
di Vecchio Frac data: 21/07/2015 10:45:10
cit. "Il file che mi interessa selezionare è l'unico txt nella cartella. "
---> Senza scomodare il FileSystemObject basterebbe Dir() per recuperare l'unico file txt nella cartella.
s = Dir(percorso & "*.txt")
La procedura riportata carica l'ultimo file txt perchè ad ogni passaggio del ciclo For Each viene caricato un file di estensione txt; viene settato MioFile al file txt trovato, e quindi quando esci dal ciclo questa variabile è impostata sull'ultimo trovato. Funziona naturalmente se come hai detto hai un solo file ma se ne hai di più viene caricato l'ultimo. La soluzione con Dir() è più aleatoria perchè non è detto che peschi l'ultimo file :)
@cromagno
La parte che hai evidenziato significa solo che nella stringa di connessione la parola TEXT va inserita proprio così e non deve essere cambiata (non puoi scrivere "TESTO") perchè è una parola chiave. Il testo italiano della guida che riporti dice "[Connection] Specifica l'origine dati per la tabella query. Può essere uno degli elementi seguenti: [...] un file di testo. Una stringa nella forma "TEXT;< text file path and name >", dove TEXT è necessario ma non localizzato."
Edit: @cromagno, non avevi chiuso correttamente i tag "b" e "u" così la mia risposta (che sto modificando ora) stava continuando con la formattazione di carattere impostata
Vuoi Approfondire?