728x90
CSV파일로 자료를 다운받은 경우
Excel의 행 번호를 넘어가서 Excel에서 자료를 로딩하기 어려울 때
파일을 열어 볼 수 있도록 적정 크기로 분할할 수 있습니다.
Sub SplitCSVFile()
Dim FSO As Object, oFile As Object
Dim sPath As String, sFullName As String, sFileName As String, sLine As String, sExt As String
Dim iR As Currency, iMax As Variant, iC As Long
sFullName = Application.GetOpenFilename(Title:="File을 선택하세요.", FileFilter:="Text Files (*.csv;*.txt), *.csv;*.txt", MultiSelect:=False)
If sFullName = "False" Or sFullName = "" Then Exit Sub
sPath = Left(sFullName, InStrRev(sFullName, "\"))
sFileName = Replace(sFullName, sPath, "")
If InStr(sFileName, ".") Then sExt = Split(sFileName, ".")(UBound(Split(sFileName, ".")))
If sExt <> "" Then sFileName = Replace(sFileName, "." & sExt, "")
iMax = Application.InputBox("분할 할 최대 행수를 입력하세요.", "분할할 행", "10000")
If TypeName(iMax) = "Boolean" Then If iMax = False Then Exit Sub
If IsNumeric(iMax) Then iMax = CCur(iMax) Else Exit Sub
Debug.Print Now()
Set FSO = CreateObject("Scripting.FileSystemObject")
Open sFullName For Input As #1
Do Until EOF(1)
iR = iR + 1
If iR Mod iMax = 1 Then
iC = iC + 1
If Not oFile Is Nothing Then oFile.Close
Set oFile = FSO.CreateTextFile(sPath & sFileName & "-" & Format(iC, "000") & "." & sExt)
Debug.Print sPath & "\" & sFileName & "-" & Format(iC, "000") & "." & sExt
End If
Line Input #1, sLine
oFile.WriteLine sLine
'oFile.WriteLine Format(iR, "00000000") & "," & sLine '// 행번호 추가시
Loop
If Not oFile Is Nothing Then oFile.Close
Close #1
Debug.Print Now()
Set FSO = Nothing
Set oFile = Nothing
MsgBox "분할된 파일 " & iC & "개를 저장하였습니다."
End Sub
혹시 전체 라인수만 확인하고 싶을 때에는 파일을 ForAppending 모드로 열어서 현재 행 위치를 확인합니다.
Sub LastLineNumber()
Dim FSO As Object, oFile As Object, sFullName As String
Const ForAppending = 8
sFullName = Application.GetOpenFilename(Title:="File을 선택하세요.", FileFilter:="Text Files (*.csv;*.txt), *.csv;*.txt", MultiSelect:=False)
If sFullName = "False" Or sFullName = "" Then Exit Sub
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFile = FSO.OpenTextFile(sFullName, ForAppending, Create:=True)
MsgBox Format(oFile.Line, "#,##0") & " Lines"
Set FSO = Nothing
End Sub
728x90
'Excel - VBA' 카테고리의 다른 글
VBA Module Source Backup Tool (0) | 2024.06.08 |
---|---|
VBA Project 소스코드 백업하기 (0) | 2024.05.18 |
ChromeDriver를 자동으로 업데이트 하기 (1) | 2024.04.26 |
[VBA] JSONParse 함수 (1) | 2024.03.28 |
[VBA] JSON 을 배열로 변환하기 (JSONtoArray) (0) | 2024.03.28 |