ラブびあ

ビール。ときどきラブ

Export

Sub Export(ws As Worksheet)
  Dim f As Variant
  f = Application.GetSaveAsFilename(ws.Name & ".txt", "テキストファイル (*.txt), *.txt")
  If f = False Then Exit Sub
 
  Open f For Output As #1
  
  Const topx = 2
  Const topy = 5
  Dim endx As Long
  Dim endy As Long
  endx = ws.Cells(topy, ws.Columns.Count).End(xlToLeft).Column
  endy = ws.Cells(ws.Rows.Count, topx).End(xlUp).Row
 
  Dim y As Long
  For y = topy To endy
    Application.StatusBar = (y - topy + 1) & "/" & (endy - topy + 1)
  
    If Left(ws.Cells(y, topx), 1) <> "#" Then ' "#"始まりはコメント行としてスキップする
      Dim arr As Variant
      arr = ws.Range(ws.Cells(y, topx), ws.Cells(y, endx))
 
      Dim i As Long
      Dim buf As String
      buf = arr(1, 1)
      For i = LBound(arr, 2) + 1 To UBound(arr, 2)
        buf = buf & "," & arr(1, i)
      Next
 
      Print #1, "insert into tablename values(" & buf & ");"
    End If
  Next y
 
  Close #1
 
  Application.StatusBar = False
  MsgBox f & Chr(13) & "を保存しました。"
End Sub