我有一个内置脚本,可以放入dir
中的文件中,并根据数据形成数组。
我该如何打印:
Dim k As Long, x As Long, j As Long ' counters
Dim varArray() As Variant
ReDim varArray(1 To 13, 1 To 1)
ReDim varArray(1 To UBound(varArray, 1), 1 To 1)
For j = 1 To .UsedRange.Rows.Count + 1
If .Cells(j, 1) <> "" Then
x = x + 1
ReDim Preserve varArray(1 To UBound(varArray, 1), 1 To x)
For k = 1 To UBound(varArray, 1)
varArray(k, x) = .Cells(j, k)
Next
End If
Next
这将构建数组,但是现在我需要将其打印到工作表上,所以我这样写:
With Workbooks("master.xlsm").Worksheets("Sheet2")
For j = 2 To UBound(varArray, 2)
For k = 1 To UBound(varArray, 1)
ActiveSheet.Cells(j, k) = varArray(k, j)
Next
Next
End With
这可行,但是现在我需要在用Redim打印后清除数组,但这是
ReDim varArray(1 To UBound(varArray, 1), 1 To 1)
在我将其放在最后一个end with
这里是整个脚本的整体视图:
Option Explicit
Sub Stuff()
Dim k As Long, x As Long, j As Long ' counters
Dim varArray() As Variant
ReDim varArray(1 To 13, 1 To 1)
Dim MyFile As String
MyFile = Dir("M:\Merge Files\")
If Len(MyFile) <> 0 Then
With ThisWorkbook.Worksheets(1)
ReDim varArray(1 To UBound(varArray, 1), 1 To 1)
For j = 1 To .UsedRange.Rows.Count + 1
If .Cells(j, 1) <> "" Then
x = x + 1
ReDim Preserve varArray(1 To UBound(varArray, 1), 1 To x)
For k = 1 To UBound(varArray, 1)
varArray(k, x) = .Cells(j, k)
Next
End If
Next
End With
With Workbooks("master.xlsm").Worksheets("Sheet2")
For j = 2 To UBound(varArray, 2)
For k = 1 To UBound(varArray, 1)
ActiveSheet.Cells(j, k) = varArray(k, j)
Next
Next
End With
ReDim varArray(1 To UBound(varArray, 1), 1 To 1)
End If
End Sub