|
此の方が気分速いかな?
Option Explicit
Public Sub DataRead()
Dim i As Long
Dim vntFileName As Variant
Dim lngRow As Long
Dim strPath As String
Dim rngResult As Range
Dim strProm As String
'指定形式のファイル名を取得
strPath = ThisWorkbook.Path
vntFileName = "test"
If Not GetReadFile(vntFileName, strPath) Then
strProm = "マクロがキャンセルされました"
GoTo Wayout
End If
'◆出力先頭セル位置を設定(基準セル位置)
Set rngResult = ActiveSheet.Cells(1, "A")
'画面更新を停止
Application.ScreenUpdating = False
'データの読み込み
CSVRead vntFileName, rngResult, lngRow
strProm = "処理が完了しました"
Wayout:
Set rngResult = Nothing
'画面更新を再開
Application.ScreenUpdating = True
MsgBox strProm, vbInformation
End Sub
Private Sub CSVRead(ByVal strFileName As String, _
ByRef rngWrite As Range, _
Optional ByRef lngRow As Long = 1, _
Optional strDelim As String = ",")
'1回の出力行数
Const clngOutput As Long = 30
Dim i As Long
Dim vntResult As Variant
Dim lngCount As Long
Dim dfn As Integer
Dim vntField As Variant
Dim lngColumns As Long
Dim strBuff As String
Dim blnMulti As Boolean
Dim strRec As String
'出力用配列確保
ReDim vntResult(1 To clngOutput, 0)
'ファイルをOpen
dfn = FreeFile
Open strFileName For Input As dfn
Do Until EOF(dfn)
'1行読み込み
Line Input #dfn, strBuff
'論理レコードに物理レコードを追加
strRec = strRec & strBuff
'論理レコードをフィールドに分割
vntField = SplitCsv(strRec, strDelim, , , blnMulti)
'フィールド内で改行が無い場合
If Not blnMulti Then
lngColumns = UBound(vntField)
'出力行数に達した場合
If lngCount = clngOutput Then
'データを出力
With rngWrite.Offset(lngRow)
'出力範囲を文字列に設定
' .Resize(lngCount, UBound(vntResult, 2) + 1).NumberFormat = "@"
'データを出力
.Resize(lngCount, UBound(vntResult, 2) + 1).Value = vntResult
End With
lngRow = lngRow + clngOutput
lngCount = 0
End If
lngCount = lngCount + 1
If UBound(vntResult, 2) < lngColumns Then
ReDim Preserve vntResult(1 To clngOutput, lngColumns)
End If
For i = 0 To lngColumns
vntResult(lngCount, i) = vntField(i)
Next i
For i = lngColumns + 1 To UBound(vntResult, 2)
vntResult(lngCount, i) = Empty
Next i
strRec = ""
Else
'セル内改行として残す場合
' strRec = strRec & vbLf
strRec = strRec & " "
End If
Loop
If lngColumns > 0 Then
With rngWrite.Offset(lngRow)
'出力範囲を文字列に設定
' .Resize(lngCount, UBound(vntResult, 2) + 1).NumberFormat = "@"
'データを出力
.Resize(lngCount, UBound(vntResult, 2) + 1).Value = vntResult
End With
End If
Close #dfn
End Sub
Private Function SplitCsv(ByVal strLine As String, _
Optional strDelimiter As String = ",", _
Optional strQuote As String = """", _
Optional strRet As String = vbCrLf, _
Optional blnMulti As Boolean) As Variant
Dim i As Long
Dim lngDPos As Long
Dim vntData() As Variant
Dim lngStart As Long
Dim vntField As Variant
Dim lngLength As Long
i = 0
lngStart = 1
lngLength = Len(strLine)
blnMulti = False
Do
ReDim Preserve vntData(i)
If Mid$(strLine, lngStart, 1) <> strQuote Then
lngDPos = InStr(lngStart, strLine, _
strDelimiter, vbBinaryCompare)
If lngDPos > 0 Then
vntField = Mid$(strLine, lngStart, _
lngDPos - lngStart)
If lngDPos = lngLength Then
ReDim Preserve vntData(i + 1)
End If
lngStart = lngDPos + 1
Else
vntField = Mid$(strLine, lngStart)
lngStart = lngLength + 1
End If
Else
lngStart = lngStart + 1
Do
lngDPos = InStr(lngStart, strLine, _
strQuote, vbBinaryCompare)
If lngDPos > 0 Then
vntField = vntField & Mid$(strLine, _
lngStart, lngDPos - lngStart)
lngStart = lngDPos + 1
Select Case Mid$(strLine, lngStart, 1)
Case ""
Exit Do
Case strDelimiter
lngStart = lngStart + 1
Exit Do
Case strQuote
lngStart = lngStart + 1
vntField = vntField & strQuote
End Select
Else
blnMulti = True
vntField = Mid$(strLine, lngStart)
lngStart = lngLength + 1
Exit Do
End If
Loop
End If
vntData(i) = vntField
vntField = Empty
i = i + 1
Loop Until lngLength < lngStart
SplitCsv = vntData()
End Function
Private Function GetReadFile(vntFileNames As Variant, _
Optional strFilePath As String, _
Optional blnMultiSel As Boolean _
= False) As Boolean
Dim strFilter As String
'フィルタ文字列を作成
strFilter = "CSV File (*.csv),*.csv," _
& "Text File (*.txt),*.txt," _
& "CSV and Text (*.csv; *.txt),*.csv;*.txt," _
& "全て (*.*),*.*"
'読み込むファイルの有るフォルダを指定
If strFilePath <> "" Then
'ファイルを開くダイアログ表示ホルダに移動
ChDrive Left(strFilePath, 1)
ChDir strFilePath
End If
'もし、ディフォルトのファイル名が有る場合
If vntFileNames <> "" Then
SendKeys vntFileNames & "{TAB}", False
End If
'「ファイルを開く」ダイアログを表示
vntFileNames _
= Application.GetOpenFilename(strFilter, 1, , , blnMultiSel)
If VarType(vntFileNames) = vbBoolean Then
Exit Function
End If
GetReadFile = True
End Function
|
|