Skip to content

Instantly share code, notes, and snippets.

@froop
Created May 9, 2011 15:17
Show Gist options
  • Select an option

  • Save froop/962712 to your computer and use it in GitHub Desktop.

Select an option

Save froop/962712 to your computer and use it in GitHub Desktop.
[Excel] CSVファイルを文字列書式で開く
Sub OpenCsv()
Const MAX_COLS = 256 '最大列数
Dim strFileName As String
Dim vrnFieldInfo(MAX_COLS - 1) As Variant
Dim i As Integer
'ファイル名入力
strFileName = Application.GetOpenFilename( _
"テキストファイル,*.txt,CSVファイル,*.csv")
If strFileName = "False" Then
Exit Sub
End If
'拡張子「.csv」だと書式設定が無視されるので「.txt」に変更
'参考: すぐに役立つエクセルVBAマクロ集
'CSVファイルを文字列として開きたいが?
'http://www.happy500z.com/YNxv914.html
If Right(strFileName, 4) = ".csv" Then
Dim NewName As String
NewName = Left(strFileName, Len(strFileName) - 4) + ".txt"
FileCopy strFileName, NewName
strFileName = NewName
End If
'全列の書式=2(文字列)
For i = 1 To MAX_COLS
vrnFieldInfo(i - 1) = Array(i, 2)
Next
'ファイルOpen
Workbooks.OpenText Filename:=strFileName, StartRow:=1, _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter _
:=False, Tab:=False, Semicolon:=False, Comma:=True, _
Space:=False, Other:=False, FieldInfo:=vrnFieldInfo
End Sub
@mysol-brang

Copy link
Copy Markdown

Kachin

ミャンマーから本当にありがとうございました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment