Condividi tramite

importare in automatico tabelle da file di testo con larghezza fissa

Anonimo
2011-04-01T09:25:55+00:00

buongiorno, scusate la domanda forse stupida.

Mi ritrovo a dover importare in excel molti file di testo, delimitando le colonne con larghezza fissa.

Ogni volta devo ridargli a mano il punto in cui delimitare le colonne. Anche se è sempre il solito.

C'e un modo in automatico per far si che importi direttamente i file di testo con colonne definite.

Tipo prima colonna a 10 , seconda a 14 , terza a 16 e così via.

ho sia excel 2003, 2007 e 2010 a disposizione.

Grazie per le risposte.

Microsoft 365 e Office | Excel | Per la casa | Windows

Domanda bloccata. Questa domanda è stata eseguita dalla community del supporto tecnico Microsoft. È possibile votare se è utile, ma non è possibile aggiungere commenti o risposte o seguire la domanda.

0 commenti Nessun commento

Risposta accettata dall'autore della domanda

Anonimo
2011-04-01T10:36:09+00:00

copio e incollo quello che mi è venuto fuori, scusate la non conoscenza.

 

allora sono arrivato a creare una macro così.

<cut>

 

ma mi da errore .

Se(se) quella che ho messo come sPath è la path dove recuperi il tuo file .txt:

 Public Sub m()

    Dim vNome As Variant

    Dim sPath As String

    sPath = "\\cecchi.loc\dati\UserDoc\fabrizio\documenti\CENTRI DI COSTO\DICEMBRE 2011"

    vNome = Application.InputBox("Inserire il nome del file.", "Attenzione!")

    If vNome <> False And vNome <> "" Then

        With ActiveSheet.QueryTables.Add(Connection:= _

             "TEXT;" & sPath & vNome & ".txt", Destination:=Range("A1"))

             .Name = vNome

             .FieldNames = True

             .RowNumbers = False

             .FillAdjacentFormulas = False

             .PreserveFormatting = True

             .RefreshOnFileOpen = False

             .RefreshStyle = xlInsertDeleteCells

             .SavePassword = False

             .SaveData = True

             .AdjustColumnWidth = True

             .RefreshPeriod = 0

             .TextFilePromptOnRefresh = False

             .TextFilePlatform = 1252

             .TextFileStartRow = 1

             .TextFileParseType = xlFixedWidth

             .TextFileTextQualifier = xlTextQualifierDoubleQuote

             .TextFileConsecutiveDelimiter = False

             .TextFileTabDelimiter = True

             .TextFileSemicolonDelimiter = False

             .TextFileCommaDelimiter = False

             .TextFileSpaceDelimiter = False

             .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)

             .TextFileFixedColumnWidths = Array(9, 3, 4, 8, 4, 32, 41, 2, 7, 9, 12, 2, 7, 9, 21, 1, 27)

             .TextFileTrailingMinusNumbers = True

             .Refresh BackgroundQuery:=False

         End With

    End If

End Sub

La risposta è stata utile?

0 commenti Nessun commento

2 risposte aggiuntive

Ordina per: Più utili
  1. Anonimo
    2011-04-01T10:07:30+00:00

    copio e incollo quello che mi è venuto fuori, scusate la non conoscenza.

    allora sono arrivato a creare una macro così.

    Sub IMPORTAZIONE()

    '

    ' IMPORTAZIONE Macro

    '

    Public Sub m()

        Dim vNome As Variant

        vNome = Application.InputBox("Inserire il nome del file.", "Attenzione!")

        If vNome <> False And vNome <> "" Then

            With ActiveSheet.QueryTables.Add(Connection:= _

                "TEXT;C:\Prova" & vNome & ".txt", Destination:=Range("A1"))

                .Name = vNome

                .FieldNames = True

                .RowNumbers = False

                .FillAdjacentFormulas = False

                .PreserveFormatting = True

                .RefreshOnFileOpen = False

                .RefreshStyle = xlInsertDeleteCells

                .SavePassword = False

                .SaveData = True

                .AdjustColumnWidth = True

                .RefreshPeriod = 0

                .TextFilePromptOnRefresh = False

                .TextFilePlatform = 1252

                .TextFileStartRow = 1

                .TextFileParseType = xlFixedWidth

                .TextFileTextQualifier = xlTextQualifierDoubleQuote

                .TextFileConsecutiveDelimiter = False

                .TextFileTabDelimiter = True

                .TextFileSemicolonDelimiter = False

                .TextFileCommaDelimiter = False

                .TextFileSpaceDelimiter = False

                .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)

                .TextFileFixedColumnWidths = Array(9, 3, 4, 8, 4, 32, 41, 2, 7, 9, 12, 2, 7, 9, 21, 1, 27)

                .TextFileTrailingMinusNumbers = True

                .Refresh BackgroundQuery:=False

            End With

        End If

    End Sub

    '

        ActiveWindow.SmallScroll Down:=3

        Application.Goto Reference:="IMPORTAZIONE"

        Application.WindowState = xlMinimized

        ChDir _

            "\\cecchi.loc\dati\UserDoc\fabrizio\documenti\CENTRI DI COSTO\DICEMBRE 2011"

    End Sub

    ma mi da errore .

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Anonimo
    2011-04-01T09:45:47+00:00

    buongiorno, scusate la domanda forse stupida.

    Mi ritrovo a dover importare in excel molti file di testo, delimitando le colonne con larghezza fissa.

    Ogni volta devo ridargli a mano il punto in cui delimitare le colonne. Anche se è sempre il solito.

    C'e un modo in automatico per far si che importi direttamente i file di testo con colonne definite.

    Tipo prima colonna a 10 , seconda a 14 , terza a 16 e così via.

     

    ho sia excel 2003, 2007 e 2010 a disposizione.

     

    Grazie per le risposte.

     

    Puoi creare una macro che faccia questo per te. Più o meno una cosa del genere:

    Public Sub m()

        Dim vNome As Variant

        vNome = Application.InputBox("Inserire il nome del file.", "Attenzione!")

        If vNome <> False And vNome <> "" Then

            With ActiveSheet.QueryTables.Add(Connection:= _

                "TEXT;C:\Prova" & vNome & ".txt", Destination:=Range("A1"))

                .Name = vNome

                .FieldNames = True

                .RowNumbers = False

                .FillAdjacentFormulas = False

                .PreserveFormatting = True

                .RefreshOnFileOpen = False

                .RefreshStyle = xlInsertDeleteCells

                .SavePassword = False

                .SaveData = True

                .AdjustColumnWidth = True

                .RefreshPeriod = 0

                .TextFilePromptOnRefresh = False

                .TextFilePlatform = 1252

                .TextFileStartRow = 1

                .TextFileParseType = xlFixedWidth

                .TextFileTextQualifier = xlTextQualifierDoubleQuote

                .TextFileConsecutiveDelimiter = False

                .TextFileTabDelimiter = True

                .TextFileSemicolonDelimiter = False

                .TextFileCommaDelimiter = False

                .TextFileSpaceDelimiter = False

                .TextFileColumnDataTypes = Array(1, 1, 1, 1)

                .TextFileFixedColumnWidths = Array(28, 11, 8, 6)

                .TextFileTrailingMinusNumbers = True

                .Refresh BackgroundQuery:=False

            End With

        End If

    End Sub

    Qui il file .txt deve trovarsi in C:\Prova e ha 4 colonne. Inpratica, path e nome file a parte, le righe che ti interessano sono queste:

                .TextFileColumnDataTypes = Array(1, 1, 1, 1)

                .TextFileFixedColumnWidths = Array(28, 11, 8, 6)

    Dove la prima indica il numero delle colonne(qui 4) e la seconda la relativa larghezza di ciascuna colonna.

    La risposta è stata utile?

    0 commenti Nessun commento