VanderBoon
New Member
- Joined
- Aug 19, 2021
- Messages
- 4
- Office Version
- 365
- Platform
- Windows
Hi,
I have an excel file of which I need to export into multiple text files. I need to export columns B onwards into a text file (ignoring blank cells) and finish with 2 specific text strings, then save as the value in column A. It then needs to do the same for each row underneath (up to 500), saving a unique file each time.
Below is how the final text file should look, the last 2 lines are constant.
R000001.txt:
SCAN,Part Number,FINAL LABEL
COMPARE,Part Number ,PLBLTRNS.txt
COMPARE,partnumber1,PLBLTPK.txt
COMPARE,Partnumber2,PLBLTPK.txt
END,END,END,
END,,
I'm a complete novice at VBA and so far have managed to get a code to a point where it will create the file with one line but with a fixed name. I'm sure there's a simple solution to this, my current code is below:
Any help would be much appreciated!
Ewan
I have an excel file of which I need to export into multiple text files. I need to export columns B onwards into a text file (ignoring blank cells) and finish with 2 specific text strings, then save as the value in column A. It then needs to do the same for each row underneath (up to 500), saving a unique file each time.
Below is how the final text file should look, the last 2 lines are constant.
R000001.txt:
SCAN,Part Number,FINAL LABEL
COMPARE,Part Number ,PLBLTRNS.txt
COMPARE,partnumber1,PLBLTPK.txt
COMPARE,Partnumber2,PLBLTPK.txt
END,END,END,
END,,
I'm a complete novice at VBA and so far have managed to get a code to a point where it will create the file with one line but with a fixed name. I'm sure there's a simple solution to this, my current code is below:
VBA Code:
Sub WriteToRecipes()
Dim Path As String
Dim filename As String
Dim FileNumber As Integer
Dim LC As Integer
Dim i As Integer
LC = Worksheets("Sheet4").Cells(1, Columns.Count).End(xlToLeft).Column + 1 'columns
Path = "filepath\test.txt"
FileNumber = FreeFile
Open Path For Output As FileNumber
'---------------------- Below code writes a recipe
For i = 1 To LC
If i <> LC Then
Print #FileNumber, Cells(i); vbNewLine;
Else
Print #FileNumber, "END," & "END,"; "END,"
End If
Next i
Print #FileNumber, "END,,";
Close FileNumber
'----------------------
Shell "notepad.exe " & Path, vbNormalFocus
End Sub
Any help would be much appreciated!
Ewan