作者windknife18 (windknife18)
标题Re: [问题] VBA_转档csv,多一列空白
时间Sat Apr 22 21:54:07 2023
1.工具中设定引用项目加入
a. Microsoft Script Control 1.0
b. Microsoft Scripting Runtime
2.新增函数
Function eliminateEmptyRow(fullName As String) As Boolean
Dim fso As New FileSystemObject, txtStr As TextStream, objOutputFile As _
TextStream, strText As String
If Dir(fullName) <> "" Then
Set txtStr = fso.OpenTextFile(fullName)
strText = txtStr.ReadAll
txtStr.Close
Else
eliminateEmptyRow = False: Exit Function
End If
strText = Left(strText, Len(strText) - 2)
Set objOutputFile = fso.CreateTextFile(fullName)
objOutputFile.Write strText
objOutputFile.Close
eliminateEmptyRow = True
End Function
3. 在你原始的副程式 .Close 下面加入下面呼叫
If Not eliminateEmptyRow(ThisWorkbook.Path & "\" & k(i) & ".csv") Then Stop
※ 引述《gone19 (gone19)》之铭言:
: *[1;33m(若是和其他不同软体互动之问题 请记得一并填写)*[m
: 软体:Office Excel
: 版本:2021
: 我从网路上找到VBA CODE并更改一下
: 以某一行当基准分割为多个档案
: 把AH栏位删除,转换成CSV档
: 但转出来的CSV档都会多一列空白列
: 可否用VBA把这列去除?或在这个CODE里多写什麽?
: 谢谢!
: ----------------------------------------
: Sub 保留表头拆分资料为若干新工作簿()
: Dim arr, d As Object, k, t, i&, lc%, rng As Range, c%
: c = Application.InputBox("请输入拆分列号", , 4, , , , , 1)
: If c = 0 Then Exit Sub
: Application.ScreenUpdating = False
: Application.DisplayAlerts = False
: arr = [a1].CurrentRegion
: lc = UBound(arr, 2)
: Set rng = [a1].Resize(, lc)
: Set d = CreateObject("scripting.dictionary")
: For i = 2 To UBound(arr)
: If Not d.Exists(arr(i, c)) Then
: Set d(arr(i, c)) = Cells(i, 1).Resize(1, lc)
: Else
: Set d(arr(i, c)) = Union(d(arr(i, c)), Cells(i, 1).Resize(1, lc))
: End If
: Next
: k = d.Keys
: t = d.Items
: For i = 0 To d.Count - 1
: Range("AH:AH").Delete
: With Workbooks.Add(xlWBATWorksheet)
: rng.Copy .Sheets(1).[a1]
: t(i).Copy .Sheets(1).[a2]
: .SaveAs Filename:=ThisWorkbook.Path & "\" & k(i), FileFormat:=xlCSV, CreateBackup:=Fales
: .Saved = True
: .Close
: End With
: Next
: Application.DisplayAlerts = True
: Application.ScreenUpdating = True
: MsgBox "完毕"
: End Sub
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 1.34.58.193 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Office/M.1682171649.A.37F.html
1F:推 gone19: 超级感谢,已成功! 04/24 15:49
※ 编辑: windknife18 (140.131.84.79 台湾), 04/24/2023 16:04:13