Da excel a txt



  • Da excel a txt
    di A.bury82 (utente non iscritto) data: 22/07/2014 12:50:14

    Buongiorno,
    ho un foglio excel in cui ho 5 colonne: Id, Authors, Year, Journals and Abstract. 350 sono le righe
    Vorrei "carpire" il contenuto di Abstract e creare 350 file txt che contengono solo l'abstract.
    Il nome del file può essere qualsiasi ma dovrebbe contenere l'ID in modo tale che possa ricollegare il file alla riga.
    Come posso fare ?



  • di lepat (utente non iscritto) data: 22/07/2014 13:03:51

    allega un file di esempio con 10 righe e un file txt



  • di Grograman (utente non iscritto) data: 22/07/2014 15:08:15

    Da adattare, OCCHIO ALLA LIBRERIA!!!
     
    Option Explicit
    
    
    Sub Esporta_TXT()
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''' ATTENZIONE RICHIEDE L'ATTIVAZIONE DELLA LIBRERIA MICROSOFT SCRIPTING RUNTIME '''
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      Dim wbPFV As Workbook
      Dim wsPFV As Worksheet, wsData As Worksheet
      
      Dim objFSYS As FileSystemObject 'leggasi sopra, attiva la libreria
      Dim objTxt As TextStream 'Tipo per file di testo
      Dim strTxt As String, strData As String
      Dim x As Long, i As Long
      Set objFSYS = New FileSystemObject
      Set wbPFV = ThisWorkbook
      Set wsPFV = wbPFV.Sheets("Per Upload")
      Set wsData = wbPFV.Sheets("Riepilogo")
      
      strData = Format(wsData.Cells(2, 5), "dd-mm-yyyy")
      Set objTxt = objFSYS.CreateTextFile(wbPFV.Path & "CAPITALE PRIMARIO al " & strData & ".txt", True) 'creo file txt, sovrascrivo precedenti
      
      With wsPFV
        x = .Range("A" & Rows.Count).End(xlUp).Row
        For i = 1 To x
          strTxt = .Cells(i, 1).Text
          If strTxt = "" Then strTxt = "-"
          With objTxt
            .Write strTxt
            .Write vbNewLine
          End With
        Next i
      End With
      
      Call Shell("explorer.exe " & wbPFV.Path, vbNormalFocus)
      
      Set wbPFV = Nothing
      Set wsPFV = Nothing
      Set wsData = Nothing
      Set objTxt = Nothing
      
    End Sub
    



  • di A.bury82 (utente non iscritto) data: 22/07/2014 15:12:00

    Grazie, ma come e dove lo devo adattare lo script ?
    Non conosco bene il VBA !



  • di Grograman data: 22/07/2014 16:44:50

    Prova il file che ho allegato, di cui riporto il codice:


     
    Option Explicit
    
    
    Sub Esporta_TXT()
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''' ATTENZIONE RICHIEDE L'ATTIVAZIONE DELLA LIBRERIA MICROSOFT SCRIPTING RUNTIME '''
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      Dim wbPFV As Workbook
      Dim wsPFV As Worksheet, wsData As Worksheet
      
      Dim objFSYS As FileSystemObject 'leggasi sopra, attiva la libreria
      Dim objTxt As TextStream 'Tipo per file di testo
      Dim strTxt As String, strData As String
      Dim x As Long, i As Long
      Set objFSYS = New FileSystemObject
      Set wbPFV = ThisWorkbook
      Set wsPFV = wbPFV.Sheets("Foglio1")
      
      With wsPFV
        x = .Range("F" & Rows.Count).End(xlUp).Row
        For i = 2 To x
          Set objTxt = objFSYS.CreateTextFile(wbPFV.Path & "V" & i - 1 & ".txt", True) 'creo file txt, sovrascrivo precedenti
          strTxt = .Cells(i, 6).Text
          If strTxt = "" Then strTxt = "-"
          With objTxt
            .Write strTxt
          End With
        Next i
      End With
      
      Call Shell("explorer.exe " & wbPFV.Path, vbNormalFocus)
      
      Set wbPFV = Nothing
      Set wsPFV = Nothing
      Set wsData = Nothing
      Set objTxt = Nothing
      
    End Sub



  • di A.bury82 (utente non iscritto) data: 22/07/2014 22:29:02

    MITICO !!!!
    I LOVE YOU !!!
    SENZA PAROLE !!!

    Ultima cosa...
    Se volessi modificare il campo d'esportanzione, basta che modifico il codice dove c'è il 6 ?



  • di A.bury82 (utente non iscritto) data: 24/07/2014 09:37:21

    Yeah



  • di Vecchio Frac data: 24/07/2014 10:26:02

    Hai dichiarato amore a Grograman e lui c'è rimasto secco ^_^

    Sulla modifica:
    1) hai provato?
    2) il numero 6 indica la sesta colonna del foglio1 del file corrente ed è quella da cui vengono pescati i dati, cella per cella; se devi recuperare i dati da altre colonne devi modificare quel numero (ma attento anche alla variabile "x" precedentemente impostata).