Hello,
I am new to vba coding. I have used excel a lot but not so much with macros.
Problem:
I have to write data into a txt file with specified spacing.
for example lets say the dotted boundaries represent the text file.
-------------------------------------------------------
101 1 10108 108 91010107
------------------------------------------------------
The numbers above are 1 10 10 8 10 8 9 10 10 10 7. Each of this number is within a cell in excel. The first number should write after 12 spaces from the left and each number has 2 spaces and so if the number within the excel cell a single digit then there should a space.
I hope I am not confusing.
Also for each line I have to insert an identifier within first 12 spaces.
I tried modifying this code but to no use .
Any help would be just awesome!!
Sub Totext()
Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer
myFile = Application.DefaultFilePath & "\test.prn"
Set rng = Selection
Open myFile For Output As #1
For i = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
cellValue = rng.Cells(i, j).Value
If j = rng.Columns.Count Then
Print #1, Round(cellValue, 0)
Else
Print #1, Round(cellValue, 0);
End If
Next j
Next i
Close #1
End Sub
I am new to vba coding. I have used excel a lot but not so much with macros.
Problem:
I have to write data into a txt file with specified spacing.
for example lets say the dotted boundaries represent the text file.
-------------------------------------------------------
101 1 10108 108 91010107
------------------------------------------------------
The numbers above are 1 10 10 8 10 8 9 10 10 10 7. Each of this number is within a cell in excel. The first number should write after 12 spaces from the left and each number has 2 spaces and so if the number within the excel cell a single digit then there should a space.
I hope I am not confusing.
Also for each line I have to insert an identifier within first 12 spaces.
I tried modifying this code but to no use .
Any help would be just awesome!!
Sub Totext()
Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer
myFile = Application.DefaultFilePath & "\test.prn"
Set rng = Selection
Open myFile For Output As #1
For i = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
cellValue = rng.Cells(i, j).Value
If j = rng.Columns.Count Then
Print #1, Round(cellValue, 0)
Else
Print #1, Round(cellValue, 0);
End If
Next j
Next i
Close #1
End Sub