作者windknife18 (windknife18)
标题Re: [算表] 依分类资料产生csv档案
时间Sat Jul 6 16:57:54 2024
刚好有空闲,写一下很久没玩的VBA,看看合不合用罗
Sub splitCsv()
Dim lRow As Long, i As Integer
Dim fileName As String, textData As String, fileNo As Integer
Dim preString As String
lRow = Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
preString = ""
For i = 2 To lRow
If preString <> Cells(i, 1) Then
If preString <> "" Then
Close #fileNo
End If
fileName = ActiveWorkbook.Path & "/" & Cells(i, 1) & ".csv"
fileNo = FreeFile
Open fileName For Output As #fileNo
textData = "ID,名称"
Print #fileNo, textData
End If
textData = Cells(i, 2) & "," & Cells(i, 3)
Print #fileNo, textData
If preString <> Cells(i, 1) Then
End If
preString = Cells(i, 1)
Next i
Close #fileNo
Application.ScreenUpdating = True
MsgBox "完成"
End Sub
※ 引述《abc1234586 (青叶ABC)》之铭言:
: 软体:Excel
: 版本:2016
: 目前手上有这笔资料
: 项目 ID 名称
: 1 AAAA AA
: 1 BBBB BB
: 1 CCCC CC
: 1 DDDD DD
: 2 AAAA AA
: 3 QQQQ QQ
: 4 AAAA AA
: 4 BBBB BB
: 5 AAAA AA
: 5 BBBB BB
: 6 AAAA AA
: 7 AAAA AA
: 8 AAAA AA
: 9 AAAA AA
: 想要依照「项目」拆分成不同的csv档案(例子:1.csv/2.csv/3.csv等)
: 然後内容如下:
: [1.csv]
: ID 名称
: AAAA AA
: BBBB BB
: CCCC CC
: DDDD DD
: [2.csv]
: ID 名称
: AAAA AA
: [3.csv]
: ID 名称
: QQQQ QQ
: 依此类推
: 有什麽方便拆档案的方法吗,感谢
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 122.116.138.85 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Office/M.1720256276.A.04E.html
※ 编辑: windknife18 (122.116.138.85 台湾), 07/06/2024 17:04:45
※ 编辑: windknife18 (140.131.84.79 台湾), 07/08/2024 09:57:36