arrays - Excel VBA - 如何重新调整二维数组?

标签 arrays excel vba multidimensional-array

通过 Visual Basic 在 Excel 中,我正在迭代加载到 Excel 中的发票的 CSV 文件。发票采用客户可确定的模式。

我将它们读入动态二维数组,然后将它们写入带有旧发票的另一个工作表。我知道我必须反转行和列,因为只有数组的最后一个维度可以重新调暗,然后在将其写入主工作表时转置。

我的语法在某个地方有错误。它一直告诉我我已经对数组进行了维度化。我以某种方式将其创建为静态数组?我需要修复什么才能让它动态运行?

给出的每个答案的工作代码

Sub InvoicesUpdate()
'
'Application Settings
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual

'Instantiate control variables
Dim allRows As Long, currentOffset As Long, invoiceActive As Boolean, mAllRows As Long
Dim iAllRows As Long, unusedRow As Long, row As Long, mWSExists As Boolean, newmAllRows As Long

'Instantiate invoice variables
Dim accountNum As String, custName As String, vinNum As String, caseNum As String, statusField As String
Dim invDate As String, makeField As String, feeDesc As String, amountField As String, invNum As String

'Instantiate Workbook variables
Dim mWB As Workbook 'master
Dim iWB As Workbook 'import

'Instantiate Worksheet variables
Dim mWS As Worksheet
Dim iWS As Worksheet

'Instantiate Range variables
Dim iData As Range

'Initialize variables
invoiceActive = False
row = 0

'Open import workbook
Workbooks.Open ("path:excel_invoices.csv")
Set iWB = ActiveWorkbook
Set iWS = iWB.Sheets("excel_invoices.csv")
iWS.Activate
Range("A1").Select
iAllRows = iWS.UsedRange.Rows.Count 'Count rows of import data

'Instantiate array, include extra column for client name
Dim invoices()
ReDim invoices(10, 0) 

'Loop through rows.
Do

    'Check for the start of a client and store client name
    If ActiveCell.Value = "Account Number" Then

        clientName = ActiveCell.Offset(-1, 6).Value

    End If

    If ActiveCell.Offset(0, 3).Value <> Empty And ActiveCell.Value <> "Account Number" And ActiveCell.Offset(2, 0) = Empty Then

        invoiceActive = True

        'Populate account information.
        accountNum = ActiveCell.Offset(0, 0).Value
        vinNum = ActiveCell.Offset(0, 1).Value
        'leave out customer name for FDCPA reasons
        caseNum = ActiveCell.Offset(0, 3).Value
        statusField = ActiveCell.Offset(0, 4).Value
        invDate = ActiveCell.Offset(0, 5).Value
        makeField = ActiveCell.Offset(0, 6).Value

    End If

    If invoiceActive = True And ActiveCell.Value = Empty And ActiveCell.Offset(0, 6).Value = Empty And ActiveCell.Offset(0, 9).Value = Empty Then

        'Make sure something other than $0 was invoiced
        If ActiveCell.Offset(0, 8).Value <> 0 Then

            'Populate individual item values.
            feeDesc = ActiveCell.Offset(0, 7).Value
            amountField = ActiveCell.Offset(0, 8).Value
            invNum = ActiveCell.Offset(0, 10).Value

            'Transfer data to array
            invoices(0, row) = "=TODAY()"
            invoices(1, row) = accountNum
            invoices(2, row) = clientName
            invoices(3, row) = vinNum
            invoices(4, row) = caseNum
            invoices(5, row) = statusField
            invoices(6, row) = invDate
            invoices(7, row) = makeField
            invoices(8, row) = feeDesc
            invoices(9, row) = amountField
            invoices(10, row) = invNum

            'Increment row counter for array
            row = row + 1

            'Resize array for next entry
            ReDim Preserve invoices(10,row)

         End If

    End If

    'Find the end of an invoice
    If invoiceActive = True And ActiveCell.Offset(0, 9) <> Empty Then

        'Set the flag to outside of an invoice
        invoiceActive = False

    End If

    'Increment active cell to next cell down
    ActiveCell.Offset(1, 0).Activate

'Define end of the loop at the last used row
Loop Until ActiveCell.row = iAllRows

'Close import data file
iWB.Close

最佳答案

这并不完全直观,但您无法 Redim (VB6 Ref)一个数组,如果你用维度将其变暗。链接页面的确切引用是:

The ReDim statement is used to size or resize a dynamic array that has already been formally declared using a Private, Public, or Dim statement with empty parentheses (without dimension subscripts).

换句话说,而不是暗淡发票(10,0)

你应该使用

Dim invoices()
Redim invoices(10,0)

那么当你ReDim时,你需要使用Redim Preserve (10,row)

警告:重新调整多维数组的维度时,如果要保留值,则只能增加最后一个维度。 IE。 Redim Preserve (11,row) 甚至 (11,0) 都会失败。

关于arrays - Excel VBA - 如何重新调整二维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13183775/

相关文章:

excel - 对象变量或未设置 block - 用户窗体

c++ - 如何比较 vector 和数组?

javascript - MVC4 ModelState 属性名称到 JSON 或 Array

java - Apache Poi 设置数据透视表的数据字段样式

excel - 将许多表转换为 Excel 列

VBA无法复制和粘贴具有单个值的单元格

arrays - 用于查找自定义类型索引的数组扩展

javascript - 将函数插入数组 - 循环和拼接?

python - 将不完整的日期列表与引用日期列表进行比较

excel - VBA - SaveAs - 如果网络无法访问,则运行时错误