我尝试使用VBA从Excel保存文本文件。文本文件中的数据有引号逗号引号

时间:2018-04-23 20:22:25

标签: vba excel-vba excel

如何实现这一目标?我需要最终结果如下:enter image description here

我尝试了以下几种变体,但没有任何效果:

Private Sub CommandButton1_Click()
  Dim FilePath As String
  Dim CellData As String

  StockCode = Range("H1").Value
  JobNumber = Range("H2").Value
  Comma = ","
  TheDateTime = Month(Now) & Day(Now) & Year(Now) & Hour(Now) & Minute(Now) & Second(Now)


  CellData = StockCode
  CellData = CellData & """,""" & JobNumber & """,""" & TheDateTime
  FilePath = Application.DefaultFilePath & "\TextFile.txt"

  Open FilePath For Output As #1

  Write #1, CellData
  CellData = ""

  Close #1

End Sub

其中给出以下内容: enter image description here

如何获取"数据","数据","数据"格式?

1 个答案:

答案 0 :(得分:1)

Write #语句在字符串写入文件时在字符串和引号之间插入逗号。您不必在列表中放置明确的分隔符。见这里:Write # Statement

尝试使用Write #1, StockCode, JobNumber, TheDateTime,而不是将所有这些内容连接到CellData