Arrays 如何使用for循环将数组(22,6)写入或打印到csv文件?详细解释
Excel数据22行6列,如何使用for循环将其写入或打印到文本(csv)文件,也请解释Arrays 如何使用for循环将数组(22,6)写入或打印到csv文件?详细解释,arrays,excel,vba,for-loop,text-files,Arrays,Excel,Vba,For Loop,Text Files,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).Ro
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
)。相反,我建议除了使用该方法外,还使用a来注意将逗号放置在正确的位置
准备
添加对Microsoft.Scripting.Runtime的引用
从reference.dll的(长)列表中选择它
好的,离开那里
文件处理代码
首先是创建并保存文件的驱动程序子例程。这将调用ToCSV()
以字符串的形式生成csv内容
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
请告诉我们您在做研究后尝试了什么,并解释您遇到的问题。我还想知道,我在这段代码中使用了数组,这段代码可以在没有数组的情况下编写,哪一个更好…VBA中是否有类似代码优化的内容…编写代码的最佳实践是什么…您还声明了代码之间的变量…这是编写代码的一种好方法吗…嗨,John,谢谢,代码太棒了,因为我还是VBA的新手。我正在学习新的东西。启用microsoft脚本运行时有什么好处?请仔细阅读我链接的文档。它显示您可以检查文件是否存在,提取文件、路径和扩展名,读取和写入文件流,处理临时文件,获取特殊文件夹,以及现代操作系统支持的一切,在模块中,将顶部的行添加为NewFileSystemObjectGlobal fso
,然后我可以使用fso
变量访问所有代码中的所有文件系统操作。例如fso.GetParentFolderName(path)
。