我想制作一个带有复选框的用户窗体,以从 excel 工作簿中的多个范围中选择数据,然后有一个按钮将选中的范围导出到单个文本文件。
目前,我坚持将数据保存到单个文本文件中。我能够为每个范围导出一个单独的文本文件,但希望将所有范围添加到同一个文本文件中。这是我到目前为止的示例:
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
ResinCheckbox5 和 ResinCheckbox6 循环导出单独的文本文件,我希望它们都读取到同一个文本文件。
谢谢