【问题标题】:VBA Loop Print to CSV OutputVBA 循环打印到 CSV 输出
【发布时间】:2014-06-05 15:34:59
【问题描述】:

我正在尝试在 VBA 中创建一个 CSV 输出文件,但我似乎无法获得它。我需要遍历电子表格并根据列 D 中是否有“1”从“I”列中提取数字。然后我想将“I”列的内容粘贴到 CSV 输出文件的“A”列中。有人可以帮我完成这个吗?我想合并以下所有内容:

Sub Test()

Dim FileNum, bOutputLine, bFile As String
Dim bOUTPUT, iRow As Integer

bOUTPUT = FreeFile                                   'Define bOUTPUT as a FreeFile
bFile = "C:\Desktop\Test.csv"                        'set the filepath equal to a string

For iRow = 2 To ActiveSheet.UsedRange.Rows.Count
If Trim(range("D" & iRow)) <> "" Then
FileNum = Trim(range("I" & iRow))
End If
Next

Open bFile For Output As bOUTPUT                     'Open the file
bOutputLine = FileNum                                
Print #bOUTPUT, bOutputLine                                                                            
Close #bOUTPUT                                       'Close the file

End Sub

【问题讨论】:

    标签: vba excel csv output


    【解决方案1】:

    您要么需要将文件交互放在 for-next 循环中并作为追加而不是输出打开,要么在循环中构建一个字符串变量,该变量将在底部打印出来。这是两个选项:

    Sub Test()
    
    Dim FileNum, bOutputLine, bFile As String
    Dim bOUTPUT, iRow As Integer
    
    bOUTPUT = FreeFile                                   'Define bOUTPUT as a FreeFile
    bFile = "C:\Users\HPUser\Desktop\Test.csv"                        'set the filepath equal to a string
    Open bFile For Append As bOUTPUT                     'Open the file
    For iRow = 2 To ActiveSheet.UsedRange.Rows.Count
        If Trim(range("D" & iRow)) <> "" Then
            FileNum = Trim(range("I" & iRow))
    
            bOutputLine = FileNum                                
            Print #bOUTPUT, bOutputLine                                                                            
    
        End If
    Next
    Close #bOUTPUT                                       'Close the file
    End Sub
    

    Sub Test()
    
    Dim FileNum, bOutputLine, bFile As String
    Dim bOUTPUT, iRow As Integer
    
    bOUTPUT = FreeFile                                   'Define bOUTPUT as a FreeFile
    bFile = "C:\Users\HPUser\Desktop\Test.csv"                        'set the filepath equal to a string
    
    For iRow = 2 To ActiveSheet.UsedRange.Rows.Count
        If Trim(range("D" & iRow)) <> "" Then
            bOutputLine = bOutputLine & Trim(range("I" & iRow)) & vbcrlf
        End If
    Next
    
    Open bFile For Output As bOUTPUT                     'Open the file                              
    Print #bOUTPUT, bOutputLine                                                                            
    Close #bOUTPUT                                       'Close the file
    
    End Sub
    

    【讨论】:

      【解决方案2】:

      一种方法是直接在循环中写入:

      Open bFile For Output As bOUTPUT
      For iRow = 2 To ActiveSheet.UsedRange.Rows.Count
      If InStr(1, Range("D" & iRow), "1") <> 0 Then
      Print #bOUTPUT, Trim(Range("I" & iRow))
      End If
      Next
      Close #bOUTPUT 
      

      InStr 将在 D 列的单元格中查找值“1”(根据问题的措辞,它可能类似于“AAA1A”。如果未找到“1”,则返回 0。

      【讨论】:

      • 这正是我所需要的。谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-18
      • 1970-01-01
      相关资源
      最近更新 更多