我有一些代码将表加载到数组中,进行一些计算,创建一个新表,将更改后的数组写入新工作表,并将新表保存为CSV。
我需要一个CSV,因为这是唯一的格式,目的系统,这是接受。
我想知道是否可以直接将数组写入CSV,跳过两个步骤。
通过我的研究,我从THIS POST中找到了下面的代码,但是我无法让它工作。
' SaveAsCSV saves an array as csv file. Choosing a delimiter different as a comma, is optional.
'
' Syntax:
' SaveAsCSV dMyArray, sMyFileName, [sMyDelimiter]
'
' Examples:
' SaveAsCSV dChrom, app.path & "\Demo.csv" --> comma as delimiter
' SaveAsCSV dChrom, app.path & "\Demo.csv", ";" --> semicolon as delimiter
'
' Rev. 1.00 [8 jan 2003]
' written by P. Wester
' wester@kpd.nl
Public Sub SaveAsCSV(MyArray() As Variant, sFilename As String, Optional sDelimiter As String = ",")
Dim n As Long 'counter
Dim M As Long 'counter
Dim sCSV As String 'csv string to print
On Error GoTo ErrHandler_SaveAsCSV
'check extension and correct if needed
If InStr(sFilename, ".csv") = 0 Then
sFilename = sFilename & ".csv"
Else
While (Len(sFilename) - InStr(sFilename, ".csv")) > 3
sFilename = Left(sFilename, Len(sFilename) - 1)
Wend
End If
'If MultiDimensional(MyArray()) = False Then '1 dimension
'save the file
' Open sFileName For Output As #7
' For n = 0 To UBound(MyArray(), 1)
' Print #7, Format(MyArray(n, 0), "0.000000E+00")
' Next n
' Close #7
'Else 'more dimensional
'save the file
Open sFilename For Output As #7
For n = 1 To UBound(MyArray(), 1)
sCSV = ""
For M = 1 To UBound(MyArray(), 2)
sCSV = sCSV & Format(MyArray(n, M)) & sDelimiter
Next M
sCSV = Left(sCSV, Len(sCSV) - 1) 'remove last Delimiter
Print #7, sCSV
Next n
Close #7
'End If
Exit Sub
ErrHandler_SaveAsCSV:
Close #7
使用此语法
SaveAsCSV arOut, Application.ActiveWorkbook.Path & "\Demo.csv"
我在那条线上发现了这个错误。类型不匹配:期望数组或用户定义的类型。我可以通过编辑这一行和删除括号来弥补这一点。
Public Sub SaveAsCSV(MyArray() As Variant, sFilename As String, Optional sDelimiter As String = ",")
变到
Public Sub SaveAsCSV(MyArray As Variant, sFilename As String, Optional sDelimiter As String = ",")
但是,这只会创建一个空白文件,因为这里现在出现了一个错误。这将触发错误处理。
我无法理解的是,当我在手表窗口中看到MyArray时,它的填充方式应该是这样的,所以我无法发现这个错误。任何帮助都是非常感谢的。
发布于 2021-12-07 16:43:49
您需要理解Variant
和Array of Variant
之间的区别。
VBA中的Variant
变量可以保存任何内容,其中包括一个数组。数组总是数组:
Dim v1 as Variant
v1 = 3 ' Now v1 holds an Integer
Redim v1(1 to 10) ' ...and now it holds an array of Variant
v1(1) = 3 ' The first member of v1 now holds an Integer
v1 = "VBA is a miracle" ' Now v1 holds a string (and is no longer an array)
Dim v2() as Variant ' v2 is an array of Variant per declaration
Redim v2(1 to 10) ' Assign 10 members to v2
v2(1) = 3 ' The first member of v1 now holds an Integer
v2 = "VBA is a mystery" ' This is not possible as v2 is an array!
' (Compiler error "Can't assign to an array)
如果您使用参数MyArray() As Variant
声明您的Sub,那么您的函数就需要一个变体数组作为参数。在调用SaveAsCSV arOut, Application.ActiveWorkbook.Path & "\Demo.csv"
时,您将变量arOut
作为参数传递。
这个变量很可能声明为Variant
(而不是变量数组),因此您将得到第一个编译器错误:变量不是变量数组。编译器根本无法知道在运行时,arOut
将是一个数组。它也可以是一个整数或字符串或其他任何东西。
现在,您将例程声明的参数更改为MyArray As Variant
,并且您的调用变得有效,编译器不再抱怨,它假设MyArray
可以是任何东西。如果您给它分配了一个数字或字符串,那么无论如何都会调用Sub (当然,它不会工作)。
在例程中,编写UBound(MyArray(), 1)
。编译器再次抱怨,因为MyArray
不是数组。通过更改对UBound(MyArray, 1)
的调用,编译器是快乐的。如果MyArray
不包含数组,您将得到一个运行时错误(类型不匹配)。
因此,在函数声明中使用MyArray as Variant
,并在UBound
中使用它时去掉括号,可以解决您的问题,每个人都很高兴。
有一点缺点:正如前面提到的,编译器不能保护您不使用无效的参数调用Sub。作为一名程序员,您希望编译器能够捕捉到尽可能多的问题。因此,另一种选择是将arOut
声明为数组:
Dim arOut() as Variant
arOut = Range("A1:F10") ' Do whatever magic you need to fill the array.
现在,您可以返回声明Public Sub SaveAsCSV(MyArray() As Variant...
。使用UBound(myArray(), 1)
也会有效。但是,在任何情况下,您都可以(而且应该)编写UBound(myArray, 1)
。
https://stackoverflow.com/questions/70262659
复制相似问题