M
M
MinasM2016-08-07 23:47:07
Visual Basic
MinasM, 2016-08-07 23:47:07

Split data into multiple files?

A number is given. It is necessary to write data to the file in the order of integers from 1 to a given number, if there is a lot of data, then you can write it to different files
, for example. It is given to write all numbers from 1 to 999 into four different files. Since there are 4 files, I will write 999/4 = 249 values ​​to each file.
Here is the code

Private Sub Command1_Click()
Dim MyFile As Long
MyFile = FreeFile
 
file_kolvo = Int(Val(Text2.Text))
txt = Int(Val(Text1.Text))
 
If Check1.Value = 0 Then
file_kolvo = 0
For i = 1 To txt
Open ("C:\TEST.txt") For Append As #MyFile
Print #MyFile, i
Close #MyFile
If i = txt Then
MsgBox ("Все цифры в файле C:\TEST.txt")
End If
 
Next
 
Else
n = 1
z = 0
chislo_v_faile = Int(txt / file_kolvo)
 
 
For iter_file_kolvo = 1 To file_kolvo
    
    
    
    z = z + (chislo_v_faile)
  
    
    For i = n To z
     Open ("C:\TEST" & iter_file_kolvo & ".txt") For Append As #MyFile
    Print #MyFile, i
    Close #MyFile
 
    Next i
    
    n = n + (chislo_v_faile)
    
Next iter_file_kolvo
 
End If
End Sub

The problem is in the last file. Because the situation is with integers, then 999-249*4=3 values ​​are not added to the last file.
Question. How to organize the loop more correctly so that all values ​​\u200b\u200bare entered in the last file

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xmoonlight, 2016-08-08
@xmoonlight

chislo_v_faile = Int((txt+file_kolvo) / file_kolvo)

because with Int() we cut off all the fractional part, so we have to compensate for that.

M
MinasM, 2016-08-08
@MinasM

I solved the problem using crutches and the CInt function, which does the rounding.
Maybe someone will have an idea to make the code better? Advise

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question