Excel - Crie um arquivo de texto com o nome de uma célula contendo outros dados da célula

Excel - Crie um arquivo de texto com o nome de uma célula contendo outros dados da célula

Estou procurando criar um .txt filepara cada valor column Acontendo os valores correspondentes emcolumns B and C

Responder1

Quanto mais eu olhava para isso, mais achava que era uma pequena macro útil. Para evitar que ele processasse linhas em branco (e bloqueasse meu Excel), reescrevi o código para criar um arquivo apenas enquanto houver dados disponíveis. Além disso, o uso de printem vez de writecria texto sem aspas. Aqui está o que usei para realizar a mesma coisa.

Sub CreateFile()
Do While Not IsEmpty(ActiveCell.Offset(0, 1))
    MyFile = ActiveCell.Value & ".txt"
    'set and open file for output
    fnum = FreeFile()
    Open MyFile For Output As fnum
    'use Print when you want the string without quotation marks
    Print #fnum, ActiveCell.Offset(0, 1) & " " & ActiveCell.Offset(0, 2)
Close #fnum
ActiveCell.Offset(1, 0).Select
Loop
End Sub

Sinta-se à vontade para usar e modificar como desejar. Obrigado pela ótima idéia.

Responder2

Esta macro pegará cada valor em column A, produzirá um .txt documentno nome do valor e inserirá as informações correspondentes decolumns B and C

Sub CreateTxt()
'
For Each ce In Range("A1:A" & Cells(Rows.Count, 1).End(xlDown).Row)
    Open ce & ".txt" For Output As #1
    Write #1, ce.Offset(0, 1) & " " & ce.Offset(0, 2)
    Close #1
Next ce
End Sub

Quatro anos depois, decidi voltar à minha primeira pergunta sobre SU - meu início no VBA. Isso é melhor, mas substituirá qualquer arquivo que já exista

Option Explicit

Sub CreateFileEachLine()

    Dim myPathTo As String
    myPathTo = "C:\Users\path\to\"
    Dim myFileSystemObject As Object
    Set myFileSystemObject = CreateObject("Scripting.FileSystemObject")
    Dim fileOut As Object
    Dim myFileName As String

    Dim lastRow As Long
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row
    Dim i As Long

        For i = 1 To lastRow
            If Not IsEmpty(Cells(i, 1)) Then
                myFileName = myPathTo & Cells(i, 1) & ".txt"
                Set fileOut = myFileSystemObject.CreateTextFile(myFileName)
                fileOut.write Cells(i, 2) & "    " & Cells(i, 3)
                fileOut.Close
            End If
        Next

    Set myFileSystemObject = Nothing
    Set fileOut = Nothing
End Sub

informação relacionada