[英]Create multiple text files for selected data on excel using vba scripting in single click
I am using the code below to generate the single file for selected range and considering the first cell in the selected range as file name. 我正在使用下面的代码来生成所选范围的单个文件,并将所选范围内的第一个单元格视为文件名。 Please find the image below for more details [This image shows the selected range,Consider K column(Firstline) and N Column( Lastline) to be in one file and other set of 1st and last line in other file ] this image shows the print file for a single file this is the way m currently using for generating files.I need to create more 30k files so please help me to create more files in single click considering the first and last line as header and footer for the file 请查看下面的图像以获取更多详细信息[此图像显示所选范围,考虑K列(第一行)和N列(最后行)在一个文件中,而另一组第一和最后一行在另一个文件中] 此图像显示打印单个文件的文件,这是m当前用于生成文件的方式。我需要创建更多的30k文件,因此请考虑单击第一行和最后一行作为文件的页眉和页脚来帮助我单击以创建更多文件
Private Sub CommandButton1_Click()
Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer, path As String, filename, filename2 As String
path = "D:\Watchlist-Files\"
filename = Selection.Cells(1, 1).Value
filename2 = Left(Mid(filename, 32, 99), Len(Mid(filename, 32, 99)) - 2)
myFile = path & filename2
Set rng = Selection
Open myFile For Output As #1
For i = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
cellValue = rng.Cells(i, j).Value
If j = rng.Columns.Count Then
Print #1, cellValue
Else
Print #1, cellValue,
End If
Next j
Next i
Close #1
End Sub
The code below is using a Loop that scans rows in a range that consists of Columns K:N (according to your attached screen-shots). 下面的代码使用一个循环,该循环扫描由K:N列组成的范围内的行(根据所附的屏幕截图)。
Assumptions made : your FirstLine
is in Column K , and it's the marker of the start position of copying the first cell in the first row. 假设 :您的FirstLine
在K列中 ,它是复制第一行中第一个单元格的开始位置的标记。 Your LastLine
is in Column N , and it's the marker of the last cell to copy, this is why I am closing the file once it is found. 您的LastLine
在N列中 ,它是要复制的最后一个单元格的标记,这就是为什么一旦找到该文件就将其关闭的原因。
Edit 1 : added a Msgbox
to allow the user selection of exporting the entire range or not. 编辑1 :添加了一个Msgbox
以允许用户选择是否导出整个范围。 In case the user selected NO , then a second InputBox
appears that allows the user to enter manually the last row number to export. 如果用户选择NO ,则出现第二个InputBox
,允许用户手动输入要导出的最后一行。
Option Explicit
Public Sub CommandButton1_Click()
Dim myFile As String
Dim rng As Range
Dim cellValue As Variant
Dim i As Long
Dim j As Long
Dim LastRow As Long
Dim path As String
Dim filename As String
Dim response As Boolean
path = "D:\Watchlist-Files\"
response = MsgBox("Do you want to Export the entire Range ? ", vbYesNo)
' Export the entire Range
If response = vbYes Then
LastRow = Cells(Rows.Count, "N").End(xlUp).Row
Else ' enter in the inputbox the last row number you want to export
LastRow = InputBox("Enter Last Row Number you wsnt to Export")
End If
Set rng = Range("K2:N" & LastRow)
For i = 2 To LastRow
' Column K holds the file name
If Cells(i, 11) <> "" Then
filename = Left(Mid(Cells(i, 11).Value, 32, 99), Len(Mid(Cells(i, 11).Value, 32, 99)) - 2)
myFile = path & filename
Open myFile For Output As #1
End If
For j = 1 To rng.Columns.Count
cellValue = Cells(i, 10 + j).Value
If j = rng.Columns.Count Then
Print #1, cellValue
' found LastLine >> close the file
If Not cellValue = "" Then
Close #1
End If
Else
Print #1, cellValue,
End If
Next j
Next i
End Sub
Edit 2 : Added new code below (to keep the first option valid). 编辑2 :在下面添加了新代码(以保持第一个选项有效)。 The user needs to confirm that every selection he makes start and ends with FirstLine
and LastLine
, there is no error handling. 用户需要确认他所做的每个选择都以FirstLine
和LastLine
开始和结束,没有错误处理。
Option Explicit Section 期权明确部分
Option Explicit
Dim filename As String
Dim path As String
Dim myFile As String
Dim rng As Range
Dim j As Long
Public Sub CommandButton1_Click 公共子CommandButton1_Click
Public Sub CommandButton1_Click()
Dim lastRow As Long
Dim Sel_Range As Long
Dim response As Boolean
Dim rowStart() As Long
Dim rowFinish() As Long
path = "D:\Watchlist-Files\"
response = MsgBox("Do you want to Export only the Selected Range ? ", vbYesNo)
If response = True Then
Set rng = Selection
ReDim rowStart(1 To Selection.Areas.Count)
ReDim rowFinish(1 To Selection.Areas.Count)
For Sel_Range = 1 To Selection.Areas.Count
rowStart(Sel_Range) = Selection.Areas(Sel_Range).Row
rowFinish(Sel_Range) = Selection.Areas(Sel_Range).Row + Selection.Areas(Sel_Range).Rows.Count - 1
Call CreateTextFiles(rowStart(Sel_Range), rowFinish(Sel_Range))
Next Sel_Range
Else ' export the entire Range in Columns K:N
lastRow = Cells(Rows.Count, "N").End(xlUp).Row
Set rng = Range("K2:N" & lastRow)
Call CreateTextFiles(2, lastRow)
End If
Sub CreateTextFiles(Sel_StartRow As Long, Sel_FinishRow As Long) - new routine to allow handling of multiple Ranges selection Sub CreateTextFiles(Sel_StartRow As Long,Sel_FinishRow As Long) -允许处理多个范围选择的新例程
Sub CreateTextFiles(Sel_StartRow As Long, Sel_FinishRow As Long)
Dim i As Long
Dim cellValue As Variant
For i = Sel_StartRow To Sel_FinishRow
' Column K holds the file name
If Cells(i, 11) <> "" Then
filename = Left(Mid(Cells(i, 11).Value, 32, 99), Len(Mid(Cells(i, 11).Value, 32, 99)) - 2)
myFile = path & filename
Open myFile For Output As #1
End If
For j = 1 To rng.Columns.Count
cellValue = Cells(i, 10 + j).Value
If j = rng.Columns.Count Then
Print #1, cellValue
' found LastLine >> close the file
If Not cellValue = "" Then
Close #1
End If
Else
Print #1, cellValue,
End If
Next j
Next i
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.