【发布时间】:2016-01-27 05:21:06
【问题描述】:
是否可以将工作表中的每一列保存为自己的 CSV 文件?这是我想要完成的主要事情,尽管还有更多细节。
编辑:代码几乎可以工作,但由于某种原因,它似乎只循环了大约 30 个工作表中的两个。它输出 125-135 个 csv 文件(不知道为什么会变化?),但是它应该输出接近 1000 个 csv 文件。
知道为什么代码没有在所有工作表中循环吗?(底部代码 + 更新的工作簿)
我发现的所有其他解决方案都涉及 python 或其他脚本语言,我找不到任何特定于自动从 Excel 工作表中提取列并将其保存为单独的 CSV 的内容。
目标:
(在所有工作表中,“AA”和“词频”除外)
将每列(从 E 列开始)保存为自己的 CSV 文件
目的:
创建单独的数据 CSV 文件以供其他程序进一步处理。 (这个程序需要这样组织的数据)
条件/约束:
1. 每个工作表的列数会有所不同。第一列将始终是 E 列
2. 为每个单独的 CSV 编号(1.csv、2.csv、3.csv……9999.csv),并保存在 excel 文件的工作文件夹中。迭代数字 (+1),以免覆盖其他 CSV
3.格式化新的CSV文件,使第一行(标题)保持原样,其余单元格(标题下方)用引号括起来,并粘贴到第2列的第一个单元格中
资源:
Link to worksheet
Link to updated workbook
Link to 3.csv(样本输出 CSV)
视觉示例:
查看工作表数据的组织方式
我如何尝试保存 CSV 文件(数值迭代,因此其他程序很容易通过循环加载所有 CSV 文件)
每个 CSV 文件内容的外观示例 - (单元格 A1 是“标题”值,单元格 B1 是所有关键字(存在于主 excel 表中的标题下方)聚集到一个单元格中,包含用引号 "")
几乎可以工作的代码,但只循环 2 个工作表,而不是“AA”和“词频”之外的所有工作表:
Newest workbook I'm working with
Option Explicit
Public counter As Integer
Sub Create_CSVs_AllSheets()
Dim sht 'just a tmp var
counter = 1 'this counter will provide the unique number for our 1.csv, 2.csv.... 999.csv, etc
appTGGL bTGGL:=False
For Each sht In Worksheets ' for each sheet inside the worksheets of the workbook
If sht.Name <> "AA" And sht.Name <> "Word Frequency" Then
'IF sht.name is different to AA AND sht.name is diffent to WordFrecuency THEN
'TIP:
'If Not sht.Name = noSht01 And Not sht.Name = noSht02 Then 'This equal
'IF (NOT => negate the sentence) sht.name is NOT equal to noSht01 AND
' sht.name is NOT equal to noSht02 THEN
sht.Activate 'go to that Sheet!
Create_CSVs_v3 (counter) 'run the code, and pass the counter variable (for naming the .csv's)
End If '
Next sht 'next one please!
appTGGL
End Sub
Sub Create_CSVs_v3(counter As Integer)
Dim ws As Worksheet, i As Integer, j As Integer, k As Integer, sHead As String, sText As String
Set ws = ActiveSheet 'the sheet with the data, _
'and we take the name of that sheet to do the job
For j = 5 To ws.Cells(1, Columns.Count).End(xlToLeft).Column
If ws.Cells(1, j) <> "" And ws.Cells(2, j) <> "" Then
sHead = ws.Cells(1, j)
sText = ws.Cells(2, j)
If ws.Cells(rows.Count, j).End(xlUp).Row > 2 Then
For i = 3 To ws.Cells(rows.Count, j).End(xlUp).Row 'i=3 because above we defined that_
'sText = ws.Cells(2, j) above_
'Note the "2" above and the sText below
sText = sText & Chr(10) & ws.Cells(i, j)
Next i
End If
Workbooks.Add
ActiveSheet.Range("A1") = sHead
'ActiveSheet.Range("B1") = Chr(34) & sText & Chr(34)
ActiveSheet.Range("B1") = Chr(10) & sText 'Modified above line to start with "Return" character (Chr(10))
'instead of enclosing with quotation marks (Chr(34))
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & counter & ".csv", _
FileFormat:=xlCSV, CreateBackup:=False 'counter variable will provide unique number to each .csv
ActiveWorkbook.Close SaveChanges:=True
'Application.Wait (Now + TimeValue("0:00:01"))
counter = counter + 1 'increment counter by 1, to make sure every .csv has a unique number
End If
Next j
Set ws = Nothing
End Sub
Public Sub appTGGL(Optional bTGGL As Boolean = True)
Debug.Print Timer
Application.ScreenUpdating = bTGGL
Application.EnableEvents = bTGGL
Application.DisplayAlerts = bTGGL
Application.Calculation = IIf(bTGGL, xlCalculationAutomatic, xlCalculationManual)
End Sub
知道最新代码有什么问题吗?
任何帮助将不胜感激。
【问题讨论】:
-
加一 - 好久没看到
appTGGL了。 -
“不锈钢丝-分配器之类的关键字”是如何派生的?
-
没那么长@Jeeped,这是你的代码,stackoverflow.com/questions/34845719/…
-
@Jeeped 感谢 Jeeped,我一直在尝试为其他一些进程修改您的代码,它运行良好。 '关键字如不锈钢丝 - 分配器' 将使用我将实现的单独脚本派生,最初该单元格只会说 'dispenser'。单元格 A1(不锈钢丝 - 分配器等关键字)将成为 Excel 工作表中'不锈钢丝 - 分配器等关键字' 列的标题,单元格 B1 的内容将是该特定标题单元格下方列出的所有关键字。
-
@Jeeped - 代码几乎 有效,但仅适用于其中两张,而不是全部 30 张……您对代码中的错误有任何意见吗?