Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

leggere file txt e popolare celle Excel

19 views
Skip to first unread message

Marco75

unread,
Dec 2, 2022, 9:43:33 AM12/2/22
to
Ciao,
ho un file txt con 3 informazioni poste su 3 righe.
vorrei leggere questo file e mettere:
- contenuto riga 1 in cella C2
- contenuto riga 2 in cella D2
- contenuto riga 3 in cella E2

ora ho provato così ma non è corretto...

Dim FilePath As String
Dim strFirstLine As String
Dim strSecondLine As String

Dim aFile As String
aFile = "C:\Interventi macina CQ\Ultima ricetta.txt"
If Len(Dir$(aFile)) > 0 Then

FilePath = "C:\Interventi macina CQ\Ultima ricetta.txt"

Open FilePath For Input As #1
Line Input #1, strFirstLine
MsgBox (strFirstLine)
Close #1

Open FilePath For Input As #2
Line Input #2, strFirstLine
MsgBox (strFirstLine)
Close #2

End If

in attesa di cortese risposta continuerò a provare
grazie
Marco

Marco75

unread,
Dec 2, 2022, 10:03:08 AM12/2/22
to
ho rivisto un po' il codice:
Dim my_file As Integer
Dim text_line As String
Dim file_name As String
Dim i As Integer

Dim aFile As String
aFile = "C:\Interventi macina CQ\Ultima ricetta.txt"
If Len(Dir$(aFile)) > 0 Then

file_name = "C:\Interventi macina CQ\Ultima ricetta.txt"

my_file = FreeFile()
Open file_name For Input As my_file

i = 1

While Not EOF(my_file)
Line Input #my_file, text_line
Cells(i, "H").Value = text_line
i = i + 1
Wend

ActiveSheet.Range("C2").Value = ActiveSheet.Range("H1").Value
ActiveSheet.Range("D2").Value = ActiveSheet.Range("H2").Value
ActiveSheet.Range("E2").Value = ActiveSheet.Range("H3").Value
ActiveSheet.Range("H1:H3").Select
ActiveSheet.Range("H1:H3").ClearContents
ActiveSheet.Range("C2").Select

End If

riesco a leggere il file e mi mette le 3 righe del file nella colonna H (da H1 a H3)
io però le voglio in riga quindi vado a popolarmi le celle poi cancello i dati della colonna H
non è bellissimo ma funziona, resto in attesa di suggerimenti per mettere i 3 dati in C2, D2 ed E2
grazie
Marco

Sauro

unread,
Dec 15, 2022, 11:00:36 AM12/15/22
to
Ciao Marco

Le funzioni che seguiranno non rispoondono esattamente
alle tue domande ma possono essere utili per raggiungere
i tuoi obbiettivi.

Leggi il testo di un file txt in una stringa (LeggiFileASCII)
Trovi quante righe a questa stringa (Memolines)

Col seguente ciclo leggi tutte le righe ad una ad una.
For r = 1 to Tutte_le_righe
riga = MemoLine ( StringaTesto , r )
Next r

Ciao


Function LeggiFileASCII(PathOrigine As String) As String
' Ritorna il testo di un file ASCII (txt)
Dim sTextLine As String, lRestoFile As Long
Dim nFileOrigine As Integer
Dim lDimensioneFile As Long, lCounter As Long
Dim Testo As String
On Error GoTo LeggiFileASCII_Error

nFileOrigine = FreeFile
Open PathOrigine For Binary As #nFileOrigine ' Apre il file.
' imposto variabili base
lCounter = 1
lDimensioneFile = LOF(nFileOrigine)
sTextLine = String(4000, ".") '
' restituisce la dimensione restante dopo tutti i
' blocchi di byte
lRestoFile = lDimensioneFile - Int(lDimensioneFile / 4000) * 4000
Do While Not EOF(nFileOrigine) ' Ripete fino alla fine del file.
sTextLine = String(4000, ".") '
If lCounter < Int(lDimensioneFile / 4000) * 4000 Then
' leggo e copio blocco dati
' BLOCCO INTERO
Get #nFileOrigine, lCounter, sTextLine
lCounter = lCounter + 4000
Testo = Testo & sTextLine
Else
' reimposto txtline sui bytes restanti
' BLOCCO ULTIMO PARZIALE
sTextLine = String(lRestoFile, ".")
Get #nFileOrigine, lCounter, sTextLine
Testo = Testo & sTextLine
Exit Do
End If
Loop
If InStr(Testo, Chr(26)) > 0 Then 'Aggiunto per files creati con clipper
Testo = Left(Testo, InStr(Testo, Chr(26)) - 1)
End If
Close #nFileOrigine ' Chiude il file.
LeggiFileASCII = Testo

On Error GoTo 0
Exit Function

LeggiFileASCII_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
LeggiFileASCII of Modulo Proc_Memo"
End Function




Public Sub ScriviFileASCII(PathDestinazione As String, TestoFile As String)
' Scrive un testo in un file ASCII (txt)
If Dir(PathDestinazione) <> "" Then Kill PathDestinazione
Dim nFileDestinazione As Integer
nFileDestinazione = FreeFile
' APRO FILE DI DESTINAZIONE
Open PathDestinazione For Binary Access Write As #nFileDestinazione '
Apre il file.
Put #nFileDestinazione, , TestoFile
Close #nFileDestinazione
End Sub




Public Function MemoLines(ilTesto As String) As Single
' Ritorna il numero di righe in una stringa
Dim VarVariant As Variant
VarVariant = Split(ilTesto, vbCrLf)
MemoLines = (UBound(VarVariant) - LBound(VarVariant)) + 1
End Function



Public Function MemoLine(ilTesto As String, QualeRiga As Variant) As String
' Ritorna la stringa corrispondente alla riga voluta
If Len(ilTesto) < 1 Then
MemoLine = ""
Exit Function
End If
Dim VarVariant As Variant
VarVariant = Split(ilTesto, vbcrflf)
MemoLine = VarVariant(QualeRiga - 1)
End Function





0 new messages