Hello -
I am new to VBA programming and I have written a macro (see below) to export a range of Excel cells values to a .txt file
Specifically, what I am trying to do is create a macro that
Example Spreadsheet data Example of Desired Text File Output
-- removed inline image ---
-- removed inline image ---
While the below code seems to be selecting the range of data correctly (in the below example, I am trying to select a 4 column x 35 row section of data) the text file output looks like this:
-- removed inline image ---
I think the problem lies in the For loop structure - can anyone see what is causing the problem?
THANKS!
Sub WriteTextFile()
Dim myFile As String
Dim rng As Range
Dim cellValue As Integer
Dim i As Integer
Dim j As Integer
Dim WholeLine As String
Application.DefaultFilePath = "E:\Folder1\Folder2\Folder3\Folder4"
myFile = Application.DefaultFilePath & "\Output_Text_File.txt"
Set rng = Range("B51:E85")
Open myFile For Output As #1
For i = 1 To 35
WholeLine = ""
For j = 1 To 4
cellValue = cellValue + rng.Cells(i, j).Value
WholeLine = WholeLine & cellValue & " "
Next j
Write #1, WholeLine
Next i
Close #1
MsgBox ("Done")
End Sub
I am new to VBA programming and I have written a macro (see below) to export a range of Excel cells values to a .txt file
Specifically, what I am trying to do is create a macro that
- Selects a section of integer data from a spreadsheet
- Copy it to a text file so that each integer spreadsheet cell value is separated by a space (no commas).
Example Spreadsheet data Example of Desired Text File Output
-- removed inline image ---
-- removed inline image ---
While the below code seems to be selecting the range of data correctly (in the below example, I am trying to select a 4 column x 35 row section of data) the text file output looks like this:
-- removed inline image ---
I think the problem lies in the For loop structure - can anyone see what is causing the problem?
THANKS!
Sub WriteTextFile()
Dim myFile As String
Dim rng As Range
Dim cellValue As Integer
Dim i As Integer
Dim j As Integer
Dim WholeLine As String
Application.DefaultFilePath = "E:\Folder1\Folder2\Folder3\Folder4"
myFile = Application.DefaultFilePath & "\Output_Text_File.txt"
Set rng = Range("B51:E85")
Open myFile For Output As #1
For i = 1 To 35
WholeLine = ""
For j = 1 To 4
cellValue = cellValue + rng.Cells(i, j).Value
WholeLine = WholeLine & cellValue & " "
Next j
Write #1, WholeLine
Next i
Close #1
MsgBox ("Done")
End Sub