
Estoy buscando crear un .txt file
para cada valor que column A
contenga los valores correspondientes encolumns B and C
Respuesta1
Cuanto más miraba esto, más me parecía una pequeña macro útil. Para evitar que procese filas en blanco (y bloquee mi Excel), reescribí el código para crear un archivo solo mientras haya datos disponibles. Además, el uso de print
en lugar de write
crea texto sin comillas. Esto es lo que usé para lograr lo mismo.
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
Siéntase libre de utilizarlo y modificarlo como desee. Gracias por la gran idea.
Respuesta2
Esta macro tomará cada valor en column A
, producirá un .txt document
en el nombre del valor e insertará la información correspondiente 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
Cuatro años más tarde decidí volver a mi primera pregunta sobre SU: mis inicios en VBA. Esto es mejor, pero sobrescribirá cualquier archivo que ya 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