Excel VBA質問箱 IV

当質問箱は、有志のボランティア精神のおかげで成り立っています。
問題が解決したら、必ずお礼をしましょうね。
本サイトの基本方針をまとめました。こちら をご一読ください。

投稿種別の選択が必要です。ご注意ください。
迷惑投稿防止のため、URLの入力を制限しています。ご了承ください。


7307 / 13645 ツリー ←次へ | 前へ→

【39882】CSVファイルを読み込むやり方 エクセルホーリー 06/7/3(月) 15:42 質問[未読]
【39883】Re:CSVファイルを読み込むやり方(ソース) エクセルホーリー 06/7/3(月) 15:48 質問[未読]
【39884】Re:CSVファイルを読み込むやり方(ソース) エクセルホーリー 06/7/3(月) 15:50 質問[未読]
【39885】Re:CSVファイルを読み込むやり方(ソース) エクセルホーリー 06/7/3(月) 15:51 質問[未読]
【39886】Re:CSVファイルを読み込むやり方(ソース) エクセルホーリー 06/7/3(月) 15:51 質問[未読]
【39900】Re:CSVファイルを読み込むやり方(ソース... 漂流民 06/7/4(火) 1:18 発言[未読]
【39907】Re:CSVファイルを読み込むやり方(ソース... エクセルホーリー 06/7/4(火) 9:22 質問[未読]
【39911】Re:CSVファイルを読み込むやり方(ソース... neptune 06/7/4(火) 9:40 回答[未読]
【39912】Re:CSVファイルを読み込むやり方(ソース... エクセルホーリー 06/7/4(火) 10:21 お礼[未読]

【39882】CSVファイルを読み込むやり方
質問  エクセルホーリー  - 06/7/3(月) 15:42 -

引用なし
パスワード
   お世話になります。

VC++ で作成したCSVファイルAをVC++でマクロを組んだエクセルBに
読み込ませて、エクセルBに書き込み名前をつけて保存する処理を考えています。

VC++からマクロを動かす事まで出来たのですがデータを読み込ませて書き込む
方法が分らず止まってしまいました。

過去ログを見たのですがVBA初心者の為良く理解できませんでした。

このケースの場合VBAの中で構造体を作りカンマ区切りで読み込ませて、
そのデータをセルに書き込むとゆうのが普通なのでしょうか?
過去ログを見るとCSVファイルのデータを直接エクセルに書き込んでるように
思えてしまいまして・・・

どなたかアドバイスなどありましたらよろしくお願い致します。
プログラムの書き方についてのアドバイスもありましたらお願いします。

Excelのバージョンは2000です。

一応マクロを組んだフォーマットのソースを次に付けておきます。

【39883】Re:CSVファイルを読み込むやり方(ソース...
質問  エクセルホーリー  - 06/7/3(月) 15:48 -

引用なし
パスワード
   <CSVのデータ>
1,"AAA"
2,"BBB"
L,aaa,"あいうえお(0"シングル"/1"ツイン")",0
L,bbb,"かきくけこ(0"指定OFF"/1"指定ON")",1
L,ccc,"さしすせそ(0"指定OFF"/1"指定ON")",0
2,"CCC"
3,"DDD"
L,ddd,"たちつてと(m/inch)(0"m"/1"inch")",1

1の時 AAAは "B2" に書き込む
2の時 BBBは "C3" に書き込む
Lの時 aaaは "E5", "あいうえお(0"シングル"/1"ツイン")"   "H5", 0 "AC5" に書き込む
Lの時 bbbは "E6", "かきくけこ(0"指定OFF"/1"指定ON")" "H6", 1 "AC6" に書き込む
Lの時 cccは "E7", "さしすせそ(0"指定OFF"/1"指定ON")", "H7", 0 "AC7" に書き込む
2の時,CCCは "C18" に書き込む
3の時,DDDは "D19" に書き込む
Lの時,dddは "E20" "たちつてと(m/inch)(0"m"/1"inch")" "H20", 1 AC20に書き込む

データがなくなるまで繰り返す

<マクロを組んだフォーマット>

Sub Macro1()

  Dim count1 As Integer   'カウンター
  Dim count2 As Integer   'カウンター
  Dim count3 As Integer   'カウンター

'***********************************************************************
'  本来は無限ループ試験の為3回
'***********************************************************************
  For count1 = 1 To 3
  
    Application.Goto Reference:="Macro1"
    Range("O4:O5").Select
    Range("O5").Activate
    ActiveWindow.SmallScroll Down:=-4
    
'*************************************************************************
'  Format 作成
'*************************************************************************
    For count2 = 1 To 2
'*************************************************************************
'  ("B2:AF2") と ("B17:AF17")
'*************************************************************************
      If count2 = 1 Then
        Range(Cells(2 + 31 * (count1 - 1), "B"), Cells(2 + 31 * (count1 - 1), "AF")).Select
      Else
        Range(Cells(17 + 31 * (count1 - 1), "B"), Cells(17 + 31 * (count1 - 1), "AF")).Select
      End If
      
      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
      Selection.Borders(xlDiagonalUp).LineStyle = xlNone
      With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
'*************************************************************************
'  ("C3:AF3") と ("C18:AF18")
'*************************************************************************
      Selection.Borders(xlInsideVertical).LineStyle = xlNone
      If count2 = 1 Then
        Range(Cells(3 + 31 * (count1 - 1), "C"), Cells(3 + 31 * (count1 - 1), "AF")).Select
      Else
        Range(Cells(18 + 31 * (count1 - 1), "C"), Cells(18 + 31 * (count1 - 1), "AF")).Select
      End If
      
      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
      Selection.Borders(xlDiagonalUp).LineStyle = xlNone
      With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
'*************************************************************************
'  ("D4:AF4") と ("D19:AF19")
'*************************************************************************
      If count2 = 1 Then
        Range(Cells(4 + 31 * (count1 - 1), "D"), Cells(4 + 31 * (count1 - 1), "AF")).Select
      Else
        Range(Cells(19 + 31 * (count1 - 1), "D"), Cells(19 + 31 * (count1 - 1), "AF")).Select
      End If

      Selection.Borders(xlInsideVertical).LineStyle = xlNone
      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
      Selection.Borders(xlDiagonalUp).LineStyle = xlNone
      With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
'*************************************************************************
'  ("E5:AF5") と ("E20:AF20")
'*************************************************************************
      Selection.Borders(xlInsideVertical).LineStyle = xlNone
      If count2 = 1 Then
        Range(Cells(5 + 31 * (count1 - 1), "E"), Cells(5 + 31 * (count1 - 1), "AF")).Select
      Else
        Range(Cells(20 + 31 * (count1 - 1), "E"), Cells(20 + 31 * (count1 - 1), "AF")).Select
      End If
      
      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
      Selection.Borders(xlDiagonalUp).LineStyle = xlNone
      With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
'*************************************************************************
'  ("E6:AF6") と ("E21:AF21")
'*************************************************************************
      Selection.Borders(xlInsideVertical).LineStyle = xlNone
      If count2 = 1 Then
        Range(Cells(6 + 31 * (count1 - 1), "E"), Cells(6 + 31 * (count1 - 1), "AF")).Select
      Else
        Range(Cells(21 + 31 * (count1 - 1), "E"), Cells(21 + 31 * (count1 - 1), "AF")).Select
      End If
    
      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
      Selection.Borders(xlDiagonalUp).LineStyle = xlNone
      With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
'*************************************************************************
'  ("E7:AF7") と ("E22:AF22")
'*************************************************************************
      Selection.Borders(xlInsideVertical).LineStyle = xlNone
      If count2 = 1 Then
        Range(Cells(7 + 31 * (count1 - 1), "E"), Cells(7 + 31 * (count1 - 1), "AF")).Select
      Else
        Range(Cells(22 + 31 * (count1 - 1), "E"), Cells(22 + 31 * (count1 - 1), "AF")).Select
      End If
        
      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
       Selection.Borders(xlDiagonalUp).LineStyle = xlNone
       With Selection.Borders(xlEdgeLeft)
         .LineStyle = xlContinuous
         .Weight = xlThin
         .ColorIndex = xlAutomatic
       End With
       With Selection.Borders(xlEdgeTop)
         .LineStyle = xlContinuous
         .Weight = xlThin
         .ColorIndex = xlAutomatic
       End With
       With Selection.Borders(xlEdgeBottom)
         .LineStyle = xlContinuous
         .Weight = xlThin
         .ColorIndex = xlAutomatic
       End With
       With Selection.Borders(xlEdgeRight)
         .LineStyle = xlContinuous
         .Weight = xlThin
         .ColorIndex = xlAutomatic
       End With
Sub

【39884】Re:CSVファイルを読み込むやり方(ソース...
質問  エクセルホーリー  - 06/7/3(月) 15:50 -

引用なし
パスワード
   '*************************************************************************
'  ("E8:AF8") と ("E23:AF23")
'*************************************************************************
      Selection.Borders(xlInsideVertical).LineStyle = xlNone
      If count2 = 1 Then
        Range(Cells(8 + 31 * (count1 - 1), "E"), Cells(8 + 31 * (count1 - 1), "AF")).Select
      Else
        Range(Cells(23 + 31 * (count1 - 1), "E"), Cells(23 + 31 * (count1 - 1), "AF")).Select
      End If

      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
      Selection.Borders(xlDiagonalUp).LineStyle = xlNone
      With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
'*************************************************************************
'  ("E9:AF9") と ("E24:AF24")
'*************************************************************************
      Selection.Borders(xlInsideVertical).LineStyle = xlNone
      If count2 = 1 Then
        Range(Cells(9 + 31 * (count1 - 1), "E"), Cells(9 + 31 * (count1 - 1), "AF")).Select
      Else
        Range(Cells(24 + 31 * (count1 - 1), "E"), Cells(24 + 31 * (count1 - 1), "AF")).Select
      End If
      
      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
      Selection.Borders(xlDiagonalUp).LineStyle = xlNone
      With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
'*************************************************************************
'  ("E10:AF10") と ("E25:AF25")
'*************************************************************************
      Selection.Borders(xlInsideVertical).LineStyle = xlNone
      ActiveWindow.SmallScroll Down:=3
      If count2 = 1 Then
        Range(Cells(10 + 31 * (count1 - 1), "E"), Cells(10 + 31 * (count1 - 1), "AF")).Select
      Else
        Range(Cells(25 + 31 * (count1 - 1), "E"), Cells(25 + 31 * (count1 - 1), "AF")).Select
      End If

      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
      Selection.Borders(xlDiagonalUp).LineStyle = xlNone
      With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
'*************************************************************************'  ("E11:AF11") と ("E26:AF26")
'*************************************************************************
      Selection.Borders(xlInsideVertical).LineStyle = xlNone
      If count2 = 1 Then
        Range(Cells(11 + 31 * (count1 - 1), "E"), Cells(11 + 31 * (count1 - 1), "AF")).Select
      Else
        Range(Cells(26 + 31 * (count1 - 1), "E"), Cells(26 + 31 * (count1 - 1), "AF")).Select
      End If
      
      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
      Selection.Borders(xlDiagonalUp).LineStyle = xlNone
      With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
'*************************************************************************
'  ("E12:AF12") と ("E27:AF27")
'*************************************************************************
      Selection.Borders(xlInsideVertical).LineStyle = xlNone
      If count2 = 1 Then
        Range(Cells(12 + 31 * (count1 - 1), "E"), Cells(12 + 31 * (count1 - 1), "AF")).Select
      Else
        Range(Cells(27 + 31 * (count1 - 1), "E"), Cells(27 + 31 * (count1 - 1), "AF")).Select
      End If
      
      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
      Selection.Borders(xlDiagonalUp).LineStyle = xlNone
      With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
'*************************************************************************
'  ("E13:AF13") と ("E28:AF28")
'*************************************************************************
      Selection.Borders(xlInsideVertical).LineStyle = xlNone
      If count2 = 1 Then
        Range(Cells(13 + 31 * (count1 - 1), "E"), Cells(13 + 31 * (count1 - 1), "AF")).Select
      Else
        Range(Cells(28 + 31 * (count1 - 1), "E"), Cells(28 + 31 * (count1 - 1), "AF")).Select
      End If
      
      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
      Selection.Borders(xlDiagonalUp).LineStyle = xlNone
      With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
'*************************************************************************
'  ("E14:AF14") と ("E29:AF29")
'*************************************************************************
      Selection.Borders(xlInsideVertical).LineStyle = xlNone
      If count2 = 1 Then
        Range(Cells(14 + 31 * (count1 - 1), "E"), Cells(14 + 31 * (count1 - 1), "AF")).Select
      Else
        Range(Cells(29 + 31 * (count1 - 1), "E"), Cells(29 + 31 * (count1 - 1), "AF")).Select
      End If
      
      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
      Selection.Borders(xlDiagonalUp).LineStyle = xlNone
      With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With

【39885】Re:CSVファイルを読み込むやり方(ソース...
質問  エクセルホーリー  - 06/7/3(月) 15:51 -

引用なし
パスワード
   '*************************************************************************
'  ("E15:AF15") と ("E30:AF30")
'*************************************************************************
      Selection.Borders(xlInsideVertical).LineStyle = xlNone
      If count2 = 1 Then
        Range(Cells(15 + 31 * (count1 - 1), "E"), Cells(15 + 31 * (count1 - 1), "AF")).Select
      Else
        Range(Cells(30 + 31 * (count1 - 1), "E"), Cells(30 + 31 * (count1 - 1), "AF")).Select
      End If
      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
      Selection.Borders(xlDiagonalUp).LineStyle = xlNone
      With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
'*************************************************************************
'  ("E16:AF16") と ("E31:AF31")
'*************************************************************************
      Selection.Borders(xlInsideVertical).LineStyle = xlNone
      If count2 = 1 Then
        Range(Cells(16 + 31 * (count1 - 1), "E"), Cells(16 + 31 * (count1 - 1), "AF")).Select
      Else
        Range(Cells(31 + 31 * (count1 - 1), "E"), Cells(31 + 31 * (count1 - 1), "AF")).Select
      End If
      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
      Selection.Borders(xlDiagonalUp).LineStyle = xlNone
      With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
'*************************************************************************
'  ("B3:B16") と ("B18:B31")
'*************************************************************************
      Selection.Borders(xlInsideVertical).LineStyle = xlNone
      Range("R17").Select
      ActiveWindow.SmallScroll Down:=-6
      If count2 = 1 Then
        Range(Cells(3 + 31 * (count1 - 1), "B"), Cells(16 + 31 * (count1 - 1), "B")).Select
      Else
        Range(Cells(18 + 31 * (count1 - 1), "B"), Cells(31 + 31 * (count1 - 1), "B")).Select
      End If
      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
      Selection.Borders(xlDiagonalUp).LineStyle = xlNone
      With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
'*************************************************************************
'  ("C4:C16") と ("C19:C31")
'*************************************************************************
      Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
      If count2 = 1 Then
        Range(Cells(4 + 31 * (count1 - 1), "C"), Cells(16 + 31 * (count1 - 1), "C")).Select
      Else
        Range(Cells(19 + 31 * (count1 - 1), "C"), Cells(31 + 31 * (count1 - 1), "C")).Select
      End If
      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
      Selection.Borders(xlDiagonalUp).LineStyle = xlNone
      With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With

【39886】Re:CSVファイルを読み込むやり方(ソース...
質問  エクセルホーリー  - 06/7/3(月) 15:51 -

引用なし
パスワード
   '*************************************************************************
'  ("D5:D16") と ("D20:D31")
'*************************************************************************
     Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
      If count2 = 1 Then
        Range(Cells(5 + 31 * (count1 - 1), "D"), Cells(16 + 31 * (count1 - 1), "D")).Select
      Else
        Range(Cells(20 + 31 * (count1 - 1), "D"), Cells(31 + 31 * (count1 - 1), "D")).Select
      End If
      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
      Selection.Borders(xlDiagonalUp).LineStyle = xlNone
      With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
'*************************************************************************
'  ("G5:G16") と ("G20:G31")
'*************************************************************************
        Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
      If count2 = 1 Then
        Range(Cells(5 + 31 * (count1 - 1), "G"), Cells(16 + 31 * (count1 - 1), "G")).Select
      Else
        Range(Cells(20 + 31 * (count1 - 1), "G"), Cells(31 + 31 * (count1 - 1), "G")).Select
      End If
      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
      Selection.Borders(xlDiagonalUp).LineStyle = xlNone
      Selection.Borders(xlEdgeLeft).LineStyle = xlNone
      With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
'*************************************************************************
'  ("AB5:AB16") と ("AB20:AB31")
'*************************************************************************
      If count2 = 1 Then
        Range(Cells(5 + 31 * (count1 - 1), "AB"), Cells(16 + 31 * (count1 - 1), "AB")).Select
      Else
        Range(Cells(20 + 31 * (count1 - 1), "AB"), Cells(31 + 31 * (count1 - 1), "AB")).Select
      End If
      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
      Selection.Borders(xlDiagonalUp).LineStyle = xlNone
      Selection.Borders(xlEdgeLeft).LineStyle = xlNone
      With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
      End With
    Next
'*************************************************************************
'  マクロのパターンを白にする
'*************************************************************************
      Range("AG32").Select
      ActiveWindow.SmallScroll Down:=-17
      Range("B2").Select
      ActiveWindow.SmallScroll Down:=15
      Range(Cells(2 + 31 * (count1 - 1), "B"), Cells(31 + 31 * (count1 - 1), "AF")).Select
      
      With Selection.Interior
        .ColorIndex = 2
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
      End With
      Range("AH15").Select
      Application.Goto Reference:="Macro1"
      ActiveWorkbook.Save
  Next
      End

【39900】Re:CSVファイルを読み込むやり方(ソース...
発言  漂流民  - 06/7/4(火) 1:18 -

引用なし
パスワード
    ▼エクセルホーリー さん:
こんばんわ


>VC++ で作成したCSVファイルAをVC++でマクロを組んだエクセルBに
>読み込ませて、エクセルBに書き込み名前をつけて保存する処理を考えています。
ちょっと良く分かりませんが、

>このケースの場合VBAの中で構造体を作りカンマ区切りで読み込ませて、
>そのデータをセルに書き込むとゆうのが普通なのでしょうか?
>過去ログを見るとCSVファイルのデータを直接エクセルに書き込んでるように
>思えてしまいまして・・・
>
>どなたかアドバイスなどありましたらよろしくお願い致します。
エクセルVBAの場合、セルが利用できれば構造体・配列を作成するのは二度手間になります。
特に処理が必要なければ、
列(A〜IV)を構造体、行(1〜65536)を配列とみなして
直接エクセルに書き込むのが普通です。
(列:1byte、行:2byte)
「カンマ区切り」については過去ログを参照してください。
Split関数を使うと良いでしょう。

>プログラムの書き方についてのアドバイスもありましたらお願いします。
ザっと見ただけですが、

>    For count2 = 1 To 2
>'*************************************************************************
>'  ("B2:AF2") と ("B17:AF17")
>'*************************************************************************
>      If count2 = 1 Then
>        Range(Cells(2 + 31 * (count1 - 1), "B"), Cells(2 + 31 * (count1 - 1), "AF")).Select
>      Else
>        Range(Cells(17 + 31 * (count1 - 1), "B"), Cells(17 + 31 * (count1 - 1), "AF")).Select
>      End If
せっかくループで回しているのにこの条件分岐はムダです。
count2を上手く利用しましょう。

あと、
>      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
>      Selection.Borders(xlDiagonalUp).LineStyle = xlNone
>      With Selection.Borders(xlEdgeLeft)
>        .LineStyle = xlContinuous
>        .Weight = xlThin
>        .ColorIndex = xlAutomatic
>      End With
>      With Selection.Borders(xlEdgeTop)
>        .LineStyle = xlContinuous
>        .Weight = xlThin
>        .ColorIndex = xlAutomatic
>      End With
>      With Selection.Borders(xlEdgeBottom)
>        .LineStyle = xlContinuous
>        .Weight = xlThin
>        .ColorIndex = xlAutomatic
>      End With
>      With Selection.Borders(xlEdgeRight)
>        .LineStyle = xlContinuous
>        .Weight = xlThin
>        .ColorIndex = xlAutomatic
>      End With
ループ中、マクロ記録されたと思われるこれが良く使われているので
関数化した方が良いでしょう。


やりたいことはこれで済むのでは?

Sub Macro1()
  Dim count1 As Integer   'カウンター
  Dim count2 As Integer   'カウンター
  Dim count3 As Integer   'カウンター
  Dim cellpos As Long

  For count1 = 1 To 3
    For count2 = 1 To 2
      cellpos = 15 * (count2 - 1) + 31 * (count1 - 1)
      Call setcell(Range(Cells(2 + cellpos, "B"), Cells(2 + cellpos, "AF")))
      Call setcell(Range(Cells(3 + cellpos, "C"), Cells(3 + cellpos, "AF")))
      Call setcell(Range(Cells(4 + cellpos, "D"), Cells(4 + cellpos, "AF")))
      For count3 = 5 To 16
        Call setcell(Range(Cells(count3 + cellpos, "E"), Cells(5 + cellpos, "AF")))
      Next count3
      Call setcell(Range(Cells(3 + cellpos, "B"), Cells(16 + cellpos, "B")))
      Call setcell(Range(Cells(4 + cellpos, "C"), Cells(16 + cellpos, "C")))
      Call setcell(Range(Cells(5 + cellpos, "D"), Cells(16 + cellpos, "D")))
      Call setcell(Range(Cells(5 + cellpos, "G"), Cells(16 + cellpos, "G")))
      Call setcell(Range(Cells(5 + cellpos, "AB"), Cells(16 + cellpos, "AB")))
    Next count2
    Range(Cells(2 + 31 * (count1 - 1), "B"), Cells(31 + 31 * (count1 - 1), "AF")).Interior.ColorIndex = 2
  Next count1
  ActiveWorkbook.Save
End Sub

Function setcell(cel As Range)
  With cel
    .Borders(xlEdgeLeft).Weight = xlThin
    .Borders(xlEdgeTop).Weight = xlThin
    .Borders(xlEdgeBottom).Weight = xlThin
    .Borders(xlEdgeRight).Weight = xlThin
  End With
End Function

※count3は使用されていないようなので再利用しました。


><CSVのデータ>
>1,"AAA"
>2,"BBB"
>L,aaa,"あいうえお(0"シングル"/1"ツイン")",0
>L,bbb,"かきくけこ(0"指定OFF"/1"指定ON")",1
>L,ccc,"さしすせそ(0"指定OFF"/1"指定ON")",0
>2,"CCC"
>3,"DDD"
>L,ddd,"たちつてと(m/inch)(0"m"/1"inch")",1
>
>1の時 AAAは "B2" に書き込む
>2の時 BBBは "C3" に書き込む
>Lの時 aaaは "E5", "あいうえお(0"シングル"/1"ツイン")"   "H5", 0 "AC5" に書き込む
>Lの時 bbbは "E6", "かきくけこ(0"指定OFF"/1"指定ON")" "H6", 1 "AC6" に書き込む
>Lの時 cccは "E7", "さしすせそ(0"指定OFF"/1"指定ON")", "H7", 0 "AC7" に書き込む
>2の時,CCCは "C18" に書き込む
>3の時,DDDは "D19" に書き込む
>Lの時,dddは "E20" "たちつてと(m/inch)(0"m"/1"inch")" "H20", 1 AC20に書き込む
>
>データがなくなるまで繰り返す
すいません、ちょっと条件判定が良く分かりません。

【39907】Re:CSVファイルを読み込むやり方(ソース...
質問  エクセルホーリー  - 06/7/4(火) 9:22 -

引用なし
パスワード
   漂流民 さんありがとうございます。

今色々調べてみて自分なりにファイルから読み込む処理を作っていますので
また聞くと思いますのでよろしくお願いします。

教えていただいたプログラムを解析しているのですが、一箇所分らない事が
あります。

Gの行とABの行は螺旋を右だけ引きたいのですが、私がマクロの記憶で書いた
ものを関数かして頂いたのに、なぜ左も引かれているのかがわかりません。

本題から外れてしまいましたがよろしくお願い致します。

【39911】Re:CSVファイルを読み込むやり方(ソース...
回答  neptune  - 06/7/4(火) 9:40 -

引用なし
パスワード
   ▼エクセルホーリー さん:

>Gの行とABの行は螺旋を右だけ引きたいのですが、私がマクロの記憶で書いた
>ものを関数かして頂いたのに、なぜ左も引かれているのかがわかりません。
漂流民 さんではありませんが、関数setcellではセルの4辺に罫線を引いて
いるだけです。従って、setcellにフラグなどの引数を作って、Gの行とABの行
の際は「右だけ」に引くように改造しなければダメです。勿論呼び出す方も
行を判断し、適切に引数を与えてやらなければなりません。

しかし、C++に慣れているのであれば、ロジックを見るのはどうと言うことないと
思うんですけど?
文法は違いますが、Helpは圧倒的にVBAの方が見やすいですし??

・・・と言う私はVCからExcel使ったことないですけどね。

【39912】Re:CSVファイルを読み込むやり方(ソース...
お礼  エクセルホーリー  - 06/7/4(火) 10:21 -

引用なし
パスワード
   漂流民 さん・neptune さん ありがとうございます。
感謝します。

neptune さんのアドバイスでわかりました。

Call setcell(Range(Cells(5 + cellpos, "AB"), Cells(16 + cellpos, "AB")), 1)

Function setcell(cel As Range, Flag)
  With cel
    If Flag = 0 Then
      .Borders(xlEdgeLeft).Weight = xlThin
      .Borders(xlEdgeTop).Weight = xlThin
      .Borders(xlEdgeBottom).Weight = xlThin
    End If
    .Borders(xlEdgeRight).Weight = xlThin
  End With
End Function


少ずつ頑張ります、質問の際にはよろしくお願いします。

7307 / 13645 ツリー ←次へ | 前へ→
ページ:  ┃  記事番号:
2610219
(SS)C-BOARD v3.8 is Free