[ 初めての方へ | 一覧(最新更新順) | 全文検索 | 過去ログ ]
『タブ区切りのtxtを出力』(そた)
現在は↓のコードでTXT出力しておりますが、Openメソッドで処理をしたいのです。
ActiveWorkbook.SaveAs Filename:=path & "処理.txt", FileFormat:=xlText
CSVの作成はできました。
↓
Sub tesut()
Dim tmp As Variant Dim path As String Dim iCount As Long, jCount As Long, maxRow As Long, maxCol As Long Dim fileNo As Integer
Set tmp = CreateObject("WScript.Shell") 'デスクトップのパスを代入してデスクトップにデータを作成 path = tmp.SpecialFolders("Desktop") & "\データ" & Day(Date) & ".csv" Set tmp = Nothing
maxRow = ActiveSheet.Range("A1").End(xlDown).Row ' 最終行 maxCol = ActiveSheet.Range("A1").End(xlToRight).Column ' 最終列
fileNo = FreeFile ' ファイル番号の取得
Open path For Output As #fileNo ' ファイル開く
For iCount = 1 To maxRow ' 縦方向ループ(最終行まで) For jCount = 1 To maxCol - 1 ' 横方向ループ(最終列−1まで) ' ファイル出力(改行なし) If Cells(iCount, jCount).NumberFormatLocal = "yyyy/mm/dd" Then Write #fileNo, Format(Cells(iCount, jCount).Value, "yyyy/mm/dd"); Else Write #fileNo, Cells(iCount, jCount); End If Next jCount ' 最終列をファイル出力(改行付き) If Cells(iCount, maxCol).NumberFormatLocal = "yyyy/mm/dd" Then Write #fileNo, Format(Cells(iCount, maxCol).Value, "yyyy/mm/dd") Else Write #fileNo, Cells(iCount, maxCol) End If Next iCount
' ファイルを閉じる Close #fileNo
MsgBox "作成しました" End Sub
これを、タブ区切りにしたいので、↓の内容に変更致しました。
Sub tesutooo()
Dim tmp As Variant Dim path As String Dim iCount As Long, jCount As Long, maxRow As Long, maxCol As Long Dim fileNo As Integer
Set tmp = CreateObject("WScript.Shell") 'デスクトップのパスを代入してデスクトップにデータを作成 path = tmp.SpecialFolders("Desktop") & "\データ" & Day(Date) & ".txt" Set tmp = Nothing
maxRow = ActiveSheet.Range("A1").End(xlDown).Row ' 最終行 maxCol = ActiveSheet.Range("A1").End(xlToRight).Column ' 最終列
fileNo = FreeFile ' ファイル番号の取得
Open path For Output As #fileNo ' ファイル開く
For iCount = 1 To maxRow ' 縦方向ループ(最終行まで) For jCount = 1 To maxCol - 1 ' 横方向ループ(最終列−1まで) Print #fileNo, Cells(iCount, jCount) & Chr(9); Next jCount Print #fileNo, Cells(iCount, maxCol) Next iCount
' ファイルを閉じる Close #fileNo
MsgBox "作成しました"
End Sub
txtファイルはできてタブ区切りされているようなのですが、
タブが統一されておらず、一部スペースのような短い空欄だったりします。
何かコードを間違えておりますでしょうか?
最初のopenメソッドを使わない方法と同じ結果になるようにしたいのです。
< 使用 Excel:Excel2013、使用 OS:Windows8 >
Write # との違いといえば、データ前後のダブルクォートを出力していない点でしょうか。
(Print #の場合、区切り文字を自動付加しないのと同様、ダブルクォートも自動付加しません)
既にSaveAsとは、同じ結果になっていると思いますよ。
(???) 2014/10/22(水) 17:07
セル内の値の後ろにスペース等空白は入っていませんか ???さんの言うとおり、ファイルの内容は正しいように思います (hoge) 2014/10/22(水) 17:20
test test abc abc eee
ご要望, 090 2014/01/01 2014/01/01
(そた) 2014/10/22(水) 17:30
セルにどんなデータが入力されているのかいまいち分かりません 例を提示して頂ければ検証もしやすいです (hoge) 2014/10/22(水) 17:36
Print #fileNo, Trim(Cells(iCount, jCount)) & Chr(9);
(???) 2014/10/22(水) 17:37
エクセルのデータは以下の感じです。セルの内容を()で囲みました。
A B C D E 1 (test)(test )(abc)(abc)(eee) 2 (ご要望,)(090)()(2014/01/01)(2014/01/01)
先ほどの私の一番下のコード、でtxtを作ってもらえれば検証できますでしょうか?
空白をお尻に持たせたいときもあるので、空白をもたせたままできればベストなのですが。
(そた) 2014/10/22(水) 17:58
サクラエディタで確認したところ、きちんとタブは挿入されていました 生成したテキストファイルは別システム等に使用するのでしょうか そうでなければ、とりあえずは出来上がったタブ区切りのファイルを使用してみてはいかがでしょう >タブの長さって変わるのでしょうか?タブかどうか調べる事って出来るのでしょうか?? こちらに関しては分かりかねますので識者の意見をお待ちください (hoge) 2014/10/24(金) 16:36
[ 一覧(最新更新順) ]
YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki.
Modified by kazu.