如何使用 for 循环将 Array(22,6) 写入或打印到 csv 文件?有详细说明
How to write or print Array(22,6) to csv file using for loop? with detail explanation
Excel 22行6列的数据,如何使用for循环写入或打印到文本(csv)文件,另请说明。
Sub Simple_Text()
Dim FName As String
Dim ArrayRange As Variant
Dim r As Long, c As Long
Dim lRow As Long: lRow = _
ThisWorkbook.Sheets("CSV").Range("A4").End(xlDown).Row - 3
Dim lCol As Long: lCol = _
ThisWorkbook.Sheets("CSV").Range("A4").End(xlToRight).Column
FName = ThisWorkbook.Path & "\SajidTest2.csv"
ReDim ArrayRange(1 To lRow, 1 To lCol)
'Populate Array range with value in the table from sheet "CSV"
'row loop
For r = 1 To lRow
'column loop
For c = 1 To lCol
ArrayRange(r, c) = Cells(r + 3, c).Value
Next c
Next r
'Make Entries in the text CSV file
Open FName For Output As #1
'row loop
For r = 1 To lRow
Print #1, ArrayRange(r, 1) & "," & ArrayRange(r, 2) & "," & _
ArrayRange(r, 3) & "," & _
ArrayRange(r, 4) & "," & ArrayRange(r, 5) & "," & ArrayRange(r, 6)
Next r
Close #1
End Sub
我想对行和列数据使用 for 循环,以便将其写入 csv 文件
试试这个
Open FName For Output As #1
For r = 1 To lRow
Dim s As String: s = ""
For c = 1 To lCol
s = s & ArrayRange(r, c) & ","
Next
Print #1, Left(s, Len(s) - 1)
Next
Close #1
为了进一步优化,您可以在没有数组的情况下使用它以节省更多系统资源
Open FName For Output As #1
For r = 1 To lRow
Dim s As String: s = ""
For c = 1 To lCol
s = s & Cells(r + 3, c).Value
Next c
Print #1, Left(s, Len(s) - 1)
Next r
Close #1
我想在文件处理方面提供更现代的解决方案(而不是传统的 Open
和 Print
)。相反,我建议使用 FileSystemObject
, in addition to the Join()
方法来将逗号放在正确的位置。
准备
添加对 Microsoft.Scripting.Runtime
的引用
从(长)引用 .dll 列表中选择它
和好的离开那里。
文件处理代码
首先,创建文件并保存文件的驱动程序子例程。这调用 ToCSV()
生成 csv 内容为 String
.
Public Sub SaveCsvToFile()
Dim csv As String, r As Range
' Set range of export starting from "A4"
' and spanning 22 rows and 6 columns
Set r = Sheets("Sheet1").Range("A4").Resize(22, 6)
' Call function to convert data in range to csv string
csv = RangeToCSV(r, formatted:=False)
Dim fso as NewFileSystemObject
Dim fn As String
' Get filename adjecent to workbook
fn = fso.BuildPath(ThisWorkbook.Path, "SajidTest2.csv")
' Or get filename in temp folder
' fn = fso.BuildPath(fso.GetSpecialFolder(2), "SajidTest2.csv")
' Create or open the file and write the csv file
Dim fs As TextStream
Set fs = fso.CreateTextFile(fn, True)
Call fs.Write(csv)
fs.Close
End Sub
现在是从 Excel 范围生成 csv 内容的辅助函数。
Public Function RangeToCSV(ByVal r_data As Range, ByVal formatted As Boolean) As String
Dim i As Long, j As Long
Dim n As Long, m As Long
' Get the range size (#rows & #columns)
n = r_data.Rows.Count: m = r_data.Columns.Count
Dim csv As String, csv_row As String
Dim data() As String
' Create a working string array to store each
' row of the table as comma delimited
ReDim data(0 To m - 1)
For i = 1 To n
' For each row, go through the columns and
' extract the data into the working array
For j = 1 To m
If formatted Then
' Store actual data
data(j - 1) = CStr(r_data.Cells(i, j).Value)
Else
' Or to store formatted text
data(j - 1) = r_data.Cells(i, j).Text
End If
Next j
' Convert array of strings, into a delimited string
' {"A","B","C"} => "A,B,C"
csv_row = Strings.Join(data, ",")
' Add row into result and add a new line feed
csv = csv & csv_row & vbCrLf
Next i
RangeToCSV = csv
End Function
Excel 22行6列的数据,如何使用for循环写入或打印到文本(csv)文件,另请说明。
Sub Simple_Text()
Dim FName As String
Dim ArrayRange As Variant
Dim r As Long, c As Long
Dim lRow As Long: lRow = _
ThisWorkbook.Sheets("CSV").Range("A4").End(xlDown).Row - 3
Dim lCol As Long: lCol = _
ThisWorkbook.Sheets("CSV").Range("A4").End(xlToRight).Column
FName = ThisWorkbook.Path & "\SajidTest2.csv"
ReDim ArrayRange(1 To lRow, 1 To lCol)
'Populate Array range with value in the table from sheet "CSV"
'row loop
For r = 1 To lRow
'column loop
For c = 1 To lCol
ArrayRange(r, c) = Cells(r + 3, c).Value
Next c
Next r
'Make Entries in the text CSV file
Open FName For Output As #1
'row loop
For r = 1 To lRow
Print #1, ArrayRange(r, 1) & "," & ArrayRange(r, 2) & "," & _
ArrayRange(r, 3) & "," & _
ArrayRange(r, 4) & "," & ArrayRange(r, 5) & "," & ArrayRange(r, 6)
Next r
Close #1
End Sub
我想对行和列数据使用 for 循环,以便将其写入 csv 文件
试试这个
Open FName For Output As #1
For r = 1 To lRow
Dim s As String: s = ""
For c = 1 To lCol
s = s & ArrayRange(r, c) & ","
Next
Print #1, Left(s, Len(s) - 1)
Next
Close #1
为了进一步优化,您可以在没有数组的情况下使用它以节省更多系统资源
Open FName For Output As #1
For r = 1 To lRow
Dim s As String: s = ""
For c = 1 To lCol
s = s & Cells(r + 3, c).Value
Next c
Print #1, Left(s, Len(s) - 1)
Next r
Close #1
我想在文件处理方面提供更现代的解决方案(而不是传统的 Open
和 Print
)。相反,我建议使用 FileSystemObject
, in addition to the Join()
方法来将逗号放在正确的位置。
准备
添加对 Microsoft.Scripting.Runtime
从(长)引用 .dll 列表中选择它
和好的离开那里。
文件处理代码
首先,创建文件并保存文件的驱动程序子例程。这调用 ToCSV()
生成 csv 内容为 String
.
Public Sub SaveCsvToFile()
Dim csv As String, r As Range
' Set range of export starting from "A4"
' and spanning 22 rows and 6 columns
Set r = Sheets("Sheet1").Range("A4").Resize(22, 6)
' Call function to convert data in range to csv string
csv = RangeToCSV(r, formatted:=False)
Dim fso as NewFileSystemObject
Dim fn As String
' Get filename adjecent to workbook
fn = fso.BuildPath(ThisWorkbook.Path, "SajidTest2.csv")
' Or get filename in temp folder
' fn = fso.BuildPath(fso.GetSpecialFolder(2), "SajidTest2.csv")
' Create or open the file and write the csv file
Dim fs As TextStream
Set fs = fso.CreateTextFile(fn, True)
Call fs.Write(csv)
fs.Close
End Sub
现在是从 Excel 范围生成 csv 内容的辅助函数。
Public Function RangeToCSV(ByVal r_data As Range, ByVal formatted As Boolean) As String
Dim i As Long, j As Long
Dim n As Long, m As Long
' Get the range size (#rows & #columns)
n = r_data.Rows.Count: m = r_data.Columns.Count
Dim csv As String, csv_row As String
Dim data() As String
' Create a working string array to store each
' row of the table as comma delimited
ReDim data(0 To m - 1)
For i = 1 To n
' For each row, go through the columns and
' extract the data into the working array
For j = 1 To m
If formatted Then
' Store actual data
data(j - 1) = CStr(r_data.Cells(i, j).Value)
Else
' Or to store formatted text
data(j - 1) = r_data.Cells(i, j).Text
End If
Next j
' Convert array of strings, into a delimited string
' {"A","B","C"} => "A,B,C"
csv_row = Strings.Join(data, ",")
' Add row into result and add a new line feed
csv = csv & csv_row & vbCrLf
Next i
RangeToCSV = csv
End Function