简体   繁体   English

Excel VBA将多个范围导出到文本文件

[英]Excel VBA export multiple ranges to text file

I would like to make a UserForm with check boxes to select data from multiple ranges within an excel workbood and then have a button that will export the checked ranges to a single text file. 我想制作一个带复选框的UserForm,以从excel workbood中的多个范围中选择数据,然后有一个按钮,它将选中的范围导出到单个文本文件。

Currently I'm stuck with getting the data to save to a single text file. 目前,我一直坚持将数据保存到单个文本文件中。 I am able to export a separate text file for each range but would like all ranges to be added to the same text file. 我能够为每个范围导出一个单独的文本文件,但希望将所有范围添加到同一文本文件中。 here is a sample what I have so far: 这是我到目前为止的样本:

 Private Sub UserForm_Initialize() 'resets all check boxes and is run when UserForm is opened and when "Clear" command is used Dim oCtrl As Control For Each oCtrl In Me.Controls If TypeOf oCtrl Is msforms.CheckBox Then 'Loop for unchecking all checkboxs oCtrl.Value = False End If Next End Sub Private Sub SelectAll_Click() Dim oCtrl As Control For Each oCtrl In Me.Controls If TypeOf oCtrl Is msforms.CheckBox Then oCtrl.Value = True End If Next End Sub Private Sub CancelCommandButton_Click() Unload Me End Sub Private Sub ClearCommandButton_Click() 'calls the UserForm_Initialize function and clears all check boxes Call UserForm_Initialize End Sub Sheet2.Activate ''''''''''''''''''''resin 1 TEMP 75'''''''''''''''''''''''''''''' If ResinCheckBox5.Value = True Then Dim filename5 As String, lineText5 As String 'creates filename3 and lineText3 as strings "Declaire In Memory" Dim Dim myrng5 As Range, i5, j5 'creates myrng3 as a Range filename5 = ThisWorkbook.Path & "\\resin 1-" & Format(Now, "ddmmyy-hhmmss") & ".txt" 'filename3 sent to workbook location with name "LFR21321" Open filename5 For Output As #1 Set myrng5 = Range("AM18:AM38") 'Sets myrng3 with Data range For i5 = 1 To myrng5.Rows.Count 'counts rows For j5 = 1 To myrng5.Columns.Count 'counts Columns lineText5 = IIf(j5 = 1, "", lineText5 & ",") & myrng5.Cells(i5, j5) Next j5 Print #1, lineText5 Next i5 Close #1 End If Sheet2.Activate '''''''''''''''''''resin 1 TEMP 400''''''''''''''''''''''''''''''- If ResinCheckBox6.Value = True Then Dim filename6 As String, lineText6 As String Dim myrng6 As Range, i6, j6 filename6 = ThisWorkbook.Path & "\\resin 1-" & Format(Now, "ddmmyy-hhmmss") & ".txt" Open filename6 For Output As #1 Set myrng6 = Range("O40:O60") For i6 = 1 To myrng6.Rows.Count For j6 = 1 To myrng6.Columns.Count lineText6 = IIf(j6 = 1, "", lineText & ",") & myrng2.Cells(i6, j6) Next j6 Print #1, lineText4 Next i6 Close #1 End If End Sub 

The ResinCheckbox5 and ResinCheckbox6 loops export separate text files, I would like them to both read to the same text file. ResinCheckbox5和ResinCheckbox6循环可导出单独的文本文件,我希望它们都读取同一文本文件。

Thanks 谢谢

  1. Always declare Option Explicit at the top. 始终在顶部声明Option Explicit This will help you find mistakes. 这将帮助您发现错误。
  2. Declare one variable per line, always declare the type. 每行声明一个变量,始终声明类型。

To answer your question: I like to make a collection object and then feed all my lines in to that collection object. 回答您的问题:我想制作一个收集对象,然后将所有行输入该收集对象。 At the end I can loop through my collection and dump each line to the text file. 最后,我可以遍历我的集合并将每一行转储到文本文件中。

Try this: 尝试这个:

Option Explicit

Private Sub UserForm_Initialize()
    'resets all check boxes and is run when UserForm is opened and when "Clear" command is used
    Dim oCtrl As Control
    For Each oCtrl In Me.Controls
        If TypeOf oCtrl Is msforms.CheckBox Then 'Loop for unchecking all checkboxs
            oCtrl.Value = False
        End If
    Next
End Sub

Private Sub SelectAll_Click()
    Dim oCtrl As Control
    For Each oCtrl In Me.Controls
        If TypeOf oCtrl Is msforms.CheckBox Then
            oCtrl.Value = True
        End If
    Next
End Sub

Private Sub CancelCommandButton_Click()
    Unload Me
End Sub

Private Sub ClearCommandButton_Click()           'calls the UserForm_Initialize function and clears all check boxes
    Call UserForm_Initialize
End Sub

Private Sub OutPutText_Click()
    Dim textFileLines As Collection

    Sheet2.Activate
    ''''''''''''''''''''resin 1 TEMP 75''''''''''''''''''''''''''''''
    If ResinCheckBox5.Value = True Then
        ' Declare ONE variable at a time
        Dim lineText5 As String
        Dim myrng5 As Range
        Set myrng5 = Range("AM18:AM38")          'Sets myrng3 with Data range

        Dim i5 As Long
        Dim j5 As Long
        For i5 = 1 To myrng5.Rows.Count          'counts rows
            For j5 = 1 To myrng5.Columns.Count   'counts Columns
                lineText5 = IIf(j5 = 1, "", lineText5 & ",") & myrng5.Cells(i5, j5)
            Next j5
            textFileLines.Add lineText5
        Next i5
    End If

    Sheet2.Activate
    '''''''''''''''''''resin 1 TEMP 400''''''''''''''''''''''''''''''-
    If ResinCheckBox6.Value = True Then
        Dim lineText6 As String
        Dim myrng6 As Range
        Set myrng6 = Range("O40:O60")
        Dim i6 As Long
        Dim j6 As Long
        For i6 = 1 To myrng6.Rows.Count
            For j6 = 1 To myrng6.Columns.Count
                lineText6 = IIf(j6 = 1, "", lineText & ",") & myrng2.Cells(i6, j6)
            Next j6
            textFileLines.Add lineText6
        Next i6
    End If

    Dim filename5 As String
    filename5 = ThisWorkbook.Path & "\resin 1-" & Format(Now, "yyyy-MM-dd_hhmmss") & ".txt"

    Dim Line As Variant
    Open filename5 For Output As #1
    For Each Line In textFileLines
        Print #1, Line
    Next Line
    Close #1
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM