【问题标题】:Loop through records and create .txt files from batches循环记录并从批次创建 .txt 文件
【发布时间】: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

【问题讨论】:

  • 到目前为止你尝试过什么?你在哪里遇到了麻烦?请在您的问题中包含这一点。

标签: excel vba macos


【解决方案1】:
Set fs = CreateObject("scripting.filesystemobject")
file1 = ThisWorkbook.Name

file1 = Replace(file1, ".xlsm", "")
Debug.Print file1

filepath = "C:\Billing3\"    '<<< change me
j1 = 1
file2 = filepath & file1 & j1 & ".txt"
Set outfile = fs.OpenTextFile(file2, 8)
lrow = 250000
Sheets(1).Select

For i1 = 1 To lrow
    text1 = Cells(i1, 1)
    outfile.WriteLine text1

    If Int(i1 / 47000) = i1 / 47000 Then
        outfile.Close
        j1 = j1 + 1
        file2 = filepath & file1 & j1 & ".txt"
        Set outfile = fs.CreateTextFile(file2, 8)
    End If
    
Next
outfile.Close`

Filesystemobject 可用于处理文本和 csv 文件 上面的代码将从第一张表中的 A 列分批复制 47000 到最大 lrow(设置为 250000,可以更改)

【讨论】:

  • 您好,非常感谢,但是来自第一张工作表的输入数据应分批复制到作为模板的第二张工作表中,并根据该第二张工作表创建附加数据和文本文件,如何我们可以改变它吗?
  • 所以第一张表有整堆数据 100k 加上第一列中的行,代码应该分批从第一张表中获取记录 - 每 47k 并将其粘贴到从单元格 I1 开始的第二张表中 - 这是带有第一张表记录的模板,还有其他数据。将 47k 记录的数据复制到第二张表后,应创建文本文件,直到第一张表中的所有记录完成。如果您查看我的初始代码,您可以看到逻辑
  • 对不起,我的第二个帖子可能比第一个更有帮助
【解决方案2】:
Sub new_template(c As Long)

Set fs = CreateObject("scripting.filesystemobject")
file2 = "\\D\folder\Part " & c & ".txt"
Set Outfile = fs.OpenTextFile(file2, 8)

Worksheets("Template").Select
For r1 = 1 to 47000
    Text1 = cells(r1, 1)   'cell in row r1, column 1   (or A)
    For c1 = 2 to 18   ‘run from columns 2 to 18  (B to R)
        Text1 = Text1 & vbtab & cells(r1, c1)   ‘vbtab is the tab delimiter
    Next
        'Text1=left(Text1, Len(Text1)-1) 'remove last tab which is not be used
    Outfile.writeline Text1
Next
Outfile.close

End sub

【讨论】:

  • 我得到未为 Text1="" 定义的变量
  • 未为 Outfile 定义变量 - 是否应将其“dim”作为字符串?
  • 尝试 debug.print file2 并检查监视窗口中显示的内容 (Ctrl G) 我将尝试对 Text1 部分进行一些调试
  • 嗨,我刚刚修改了“Sub new_template()”,所以现在没有创建 xls 文件并且代码工作正常(请参阅上面的初始宏),唯一的问题是新的文本文件创建的会覆盖现有的。怎么改?
  • fs.OpenTextFile(file2, 8) '8 表示追加,1 被读取,2 被重写 FileExists = Dir(file2) If FileExists "" then '文件已经存在
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多