
해당 값을 포함하는 .txt file
각 값에 대해 생성하려고합니다 .column A
columns B and C
답변1
이것을 더 많이 볼수록 유용한 작은 매크로라는 것을 알게 되었습니다. 빈 행을 처리하고 Excel을 잠그는 것을 방지하기 위해 사용 가능한 데이터가 있는 동안에만 파일을 생성하도록 코드를 다시 작성했습니다. 또한 를 사용하면 인용문 없이 텍스트가 생성됩니다 print
. write
다음은 동일한 작업을 수행하는 데 사용한 것입니다.
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
원하는 대로 자유롭게 사용하고 수정하세요. 좋은 아이디어에 감사드립니다.
답변2
이 매크로는 의 각 값을 가져와서 값 이름에 column A
를 생성 하고 에서 해당 정보를 삽입합니다..txt document
columns 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
4년 후 저는 VBA의 시작점인 첫 번째 SU 질문으로 돌아가기로 결정했습니다. 이것이 더 좋지만 이미 존재하는 파일을 덮어쓰게 됩니다.
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