使用条件从 Excel 创建多个 txt 文件

发布于 2025-01-14 07:59:47 字数 265 浏览 1 评论 0原文

我需要找到一种方法从 Excel 创建多个 txt 文件,如图所示。

详细来说,我需要创建一个名为“sez-A1”的txt文件和另一个“sez-A2”等。在 txt 中,我需要有 Excel 中 2 列 x 和 y 的数字。

输入图片此处描述

I need to find a way to create multiple txt files from an Excel, as shown in the picture.

In detail I need to create a file txt with the name "sez-A1" and another "sez-A2" and so on. Inside the txt I need to have the numbers of the 2 columns x and y from the Excel.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海不蓝 2025-01-21 07:59:48

安东尼奥,欢迎来到 Stack Overflow。根据您的问题,我假设您对 VBA 相当陌生。我采用的方法假设您的数据在工作表中是连续的。如果这段代码遇到空行,就会结束。这里的想法是从工作表的第一行开始并继续向下处理数据。当 A 列中的一行具有数值时,我们向当前文件添加一行。如果 A 列中的值不是数字,我们假设它是下一个文件的名称。如果您对我所写的内容有具体疑问,请在评论中提问,我会尽力回答。

    Sub makeFiles()
      ' this code requires the workbook to be saved on a local dirve (e.g. "c:\some\path")
      ' If saved on a remote drive (e.g. onedive) it will fail
    
      Dim r As Long
      Dim s As Worksheet
      Dim path As String
      
      Set s = ActiveSheet ' use this line build files from active sheet
      'Set s = Worksheets("Sheet1") ' use this line to choose a specific sheet
    
      ' This line is reading the path from the current workbook. 
      ' The workbook must be saved first.
       path = ThisWorkbook.path & Application.PathSeparator
      
      r = 1
      ' this is an un-needed file to make the code simpler.  I delete it below.
      Open path & "junk.txt" For Output As #1  
      
      Do Until s.Cells(r, 1).Value = ""
        If IsNumeric(s.Cells(r, 1).Value) Then
          Print #1, s.Cells(r, 2).Value, s.Cells(r, 3).Value
        Else
          Close #1
          Debug.Print "Creating " & s.Cells(r, 1).Value + ".txt"
          Open path & s.Cells(r, 1).Value + ".txt" For Output As #1
        End If
        r = r + 1
      Loop
          Close #1
        
      Kill path & "junk.txt"  ' this approach creates an extra file to simplify the code, deleting it here
      Debug.Print "Done"


End Sub

Antonio, welcome to Stack Overflow. Based on your question, I'm going to assume that you are pretty new to VBA. The approach I'm taking assumes that your data are contiguous in the worksheet. If this code encounters a blank line, it will end. The idea here is to start at the first line of the worksheet and continue down to process the data. When a row in column A has a numeric value, we add a line to the current file. If the value in column A is not a number, we assume it's the name of the next file. If you have specific questions about what I have written, please ask in a comment and I'll try to answer.

    Sub makeFiles()
      ' this code requires the workbook to be saved on a local dirve (e.g. "c:\some\path")
      ' If saved on a remote drive (e.g. onedive) it will fail
    
      Dim r As Long
      Dim s As Worksheet
      Dim path As String
      
      Set s = ActiveSheet ' use this line build files from active sheet
      'Set s = Worksheets("Sheet1") ' use this line to choose a specific sheet
    
      ' This line is reading the path from the current workbook. 
      ' The workbook must be saved first.
       path = ThisWorkbook.path & Application.PathSeparator
      
      r = 1
      ' this is an un-needed file to make the code simpler.  I delete it below.
      Open path & "junk.txt" For Output As #1  
      
      Do Until s.Cells(r, 1).Value = ""
        If IsNumeric(s.Cells(r, 1).Value) Then
          Print #1, s.Cells(r, 2).Value, s.Cells(r, 3).Value
        Else
          Close #1
          Debug.Print "Creating " & s.Cells(r, 1).Value + ".txt"
          Open path & s.Cells(r, 1).Value + ".txt" For Output As #1
        End If
        r = r + 1
      Loop
          Close #1
        
      Kill path & "junk.txt"  ' this approach creates an extra file to simplify the code, deleting it here
      Debug.Print "Done"


End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文