Home > Software engineering >  Excel in data according to the original format (row, column, blank space to separate) output data to
Excel in data according to the original format (row, column, blank space to separate) output data to

Time:10-23

Hi ~
I in Excel macro, run the following code will have the run time error "54" bad file mode



Sub Outputfile

Dim num As Double
Dim row As Integer
Dim col As Integer




The row=1

Open "Output. TXT" For Input As # 2



Print # 2, num

Cells (row, 1)=num
Input # 2, num
Cells (row, 2)=num
Input # 2, num
Cells (row, 3)=num
The row row + 1=




Close # 2

MsgBox "Finished Writing"


End Sub

Below is before I write from TXT read data to excel cousin, and the ranks of the interchange code, (col to row, row to col), it can run and normal work, everyone can be used to consult,
Below is excel has converted data, need to use before the VBA Output to the Output. The TXT
2.9 1.5 7.6 6.5 2.6
2.1 1.2 6.5 9.12 5.4
12-10.4 13.5 8.6 0.23


CodePudding user response:

 
Sub Outputfile ()
Dim arr, ss
Open "D: \ Output. TXT" For Append As # 2
Arr=Range (" A1: E3 ")
For I=1 To 3
Ss=""
For j=1 To 5
Ss=ss & amp; Arr (I, j) & amp; "
"Next j
Print # 2, ss
Next I
Close # 2
MsgBox "Finished Writing"
End Sub

CodePudding user response:

Open "Output. TXT" For Input As # 2
Change for the output!

Prompt you "the wrong file pattern", also don't know how to change ah,
Input mode is used to read the file "" (Input), not to" write files "(output),

CodePudding user response:

reference 1st floor a814153a response:
 
Sub Outputfile ()
Dim arr, ss
Open "D: \ Output. TXT" For Append As # 2
Arr=Range (" A1: E3 ")
For I=1 To 3
Ss=""
For j=1 To 5
Ss=ss & amp; Arr (I, j) & amp; "
"Next j
Print # 2, ss
Next I
Close # 2
MsgBox "Finished Writing"
End Sub
thanks ~ this is available,
Can I have two small problems, I and j represents the ranks, ss and arr is representative of what?
I am a VB for beginners, so there are many don't understand,

CodePudding user response:

reference u012779420 reply: 3/f
Quote: refer to 1st floor a814153a response:

 
Sub Outputfile ()
Dim arr, ss
Open "D: \ Output. TXT" For Append As # 2
Arr=Range (" A1: E3 ")
For I=1 To 3
Ss=""
For j=1 To 5
Ss=ss & amp; Arr (I, j) & amp; "
"Next j
Print # 2, ss
Next I
Close # 2
MsgBox "Finished Writing"
End Sub
thanks ~ this is available,
Can I have two small problems, I and j represents the ranks, ss and arr is representative of what?
I am a VB for beginners, so there are many don't understand,


Arr is two-dimensional arrays, arr=Range (" A1: E3 ") is the cell (A1: E3) assigned to the array arr;
Ss, it is a temporary variable,
  •  Tags:  
  • VBA