【发布时间】:2022-01-06 13:33:37
【问题描述】:
我有下面的代码,我想只创建 .txt 文件而不是 excel 文件。下面将从 Sheet1 中获取记录,将它们分隔 47k 行,将其粘贴到模板中并创建 .txt 文件(格式不带逗号,打印方法,不覆盖文件夹中现有的 txt 文件,每个文件名为“Part”&“sequence number ”,例如“第 1 部分”),然后重复,直到 Sheet1 中的所有记录都进入 txt 文件。我需要帮助创建一个循环,该循环将创建 txt 文件并将记录粘贴到 txt 文件中
Sub FillTemplate(c As Long)
Dim Lrow1A, c, start, finish as Long
Dim TV As Variant
' this part divides all records by 47k and rounds up
Lrow1A = Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
TV = Lrow1A / 47000
TV = Application.WorksheetFunction.RoundUp(TV, 0)
' this part copies records (divided by batches of 47k) into template
start = ((c - 1) * 47000) + 2
finish = (c * 47000) + 1
Worksheets("Sheet1").Range(Cells(start, 1), Cells(finish, 1)).Copy
Worksheets("Template").Cells(1, 9).PasteSpecial Paste:=xlPasteValues
End Sub
Sub new_template(c As Long)
' Need help here: How to make macro create new text files?
'at the moment it overwrites existing text files in a folder
Dim wb As Workbook
Dim WBname, WBname1 As String
Workbooks.Add
Set wb = ActiveWorkbook
WBname = wb.FullName 'create new workbook to rename previous one
WBname1 = ActiveWorkbook.Name
Workbooks("Sum.xlsm").Activate
Worksheets("Template").Select
Range("A:R").Select
Selection.Copy
Workbooks(WBname1).Activate
Columns(1).Select
ActiveSheet.Paste
ActiveWorkbook.SaveAs "\\D\folder\Part " & c & ".txt", FileFormat:=xlTextWindows
ActiveWorkbook.Close
End Sub
Sub Finalcode()
Dim c As Long
For c = 1 To TV
Call FillTemplate(c)
Call new_template(c)
Next c
End Sub
【问题讨论】:
-
到目前为止你尝试过什么?你在哪里遇到了麻烦?请在您的问题中包含这一点。