【问题标题】:Error when trying to populate array using range function with variables尝试使用带变量的范围函数填充数组时出错
【发布时间】:2014-07-27 16:36:05
【问题描述】:

我在填充数组时遇到问题。出于某种原因,我能找到的所有教程都宁愿在代码本身中明确列出数据和调整大小(这有点违背了使用 VBA 开始这项任务的目的)。

我的问题是关于如何动态调整数组大小,如果它甚至可以按照我尝试的方式这样做的话。代码本身带有背景和具体问题的注释。

当我运行代码时,我收到“应用程序定义或对象定义错误”(1004)

此外,使用 for...next 循环来填充数组可能最终导致计算效率低下,因为数据集通常平均 23k 行

代码:

Sub DCList()
ReDim DCList(0, 0)

Dim cnum As Integer

' count the number of columns that have been used in the workbook
cnum = ActiveWorkbook.Sheets("data").UsedRange.Columns.Count

' set array dimensions to 1 row & Cnum columns
ReDim DCList(1, cnum)


' set array values to values found in the range (A1, cnum 1)
DCList = ActiveWorkbook.Sheets("data").Range(Cells(1, 1), Cells(1, cnum)).Value


'Other info:
' DCList is a global variable set as Variant type
' The overarching goal of this sub is purely to determine the column names of
' a dataset so that (in another sub) the user can select which column to lookup



End Sub

【问题讨论】:

  • 感谢 Gary 的学生,当你这样做时,他正试图弄清楚如何正确格式化这个东西。
  • 将 ActiveWorkbook.Sheets("data").Range(Cells(1, 1), Cells(1, cnum)).Value 替换为 ActiveWorkbook.Sheets("data").Range(ActiveWorkbook. Sheets("data").Cells(1, 1), ActiveWorkbook.Sheets("data").Cells(1, cnum)).Value ?

标签: arrays vba excel


【解决方案1】:

试试这个版本:

Sub DCList_sub()
    Dim DCList
    Dim cnum As Long

    ' count the number of columns that have been used in the workbook
    cnum = ActiveWorkbook.Sheets("data").UsedRange.Columns.Count

    ' set array dimensions to 1 row & Cnum columns
    ReDim DCList(1 To 1, 1 To cnum)

    ' set array values to values found in the range (A1, cnum 1)
    DCList = ActiveWorkbook.Sheets("data").Range(Cells(1, 1), Cells(1, cnum)).Value

    'Other info:
    ' DCList is a global variable set as Variant type
    ' The overarching goal of this sub is purely to determine the column names of
    ' a dataset so that (in another sub) the user can select which column to lookup

End Sub

【讨论】:

  • 有效!现在的问题是:为什么这个工作有效而我的原始代码没有?是不是因为我的 dclist 数组 redim 只有 1 到 cnum?
  • 1 我将 sub 的名称更改为与数组的名称不同。 2 我将 Integer 更改为 Long。 我按照指示进行了 ReDimmed DCList。
  • 事实上它不应该工作,除非 activesheet 已经是 sheet("Data")。
【解决方案2】:

如果您希望 DCList 包含工作表的第一行,则无需重新调整 DClist。简单地说:

Dim DCList As Variant
DCList = ActiveWorkbook.Sheets("Data").UsedRange.Rows(1)

应该这样做。

如果您出于其他原因需要列数

cnum = ubound(DCList,2)

【讨论】:

    【解决方案3】:

    Gary 的学生代码已更正:

    Sub DCList_sub()
    Dim DCList() as variant
    Dim cnum As Long
    
    
    with ActiveWorkbook.Sheets("data") 'might be better with thisworkbook, if the code is in the same workbook...    
    
        ' count the number of columns that have been used in the workbook
        cnum = .UsedRange.Columns.Count 
    
        ' set array dimensions to 1 row & Cnum columns
        ReDim DCList(1 To 1, 1 To cnum)
    
        ' set array values to values found in the range (A1, cnum 1)
        DCList = .Range(.Cells(1, 1), .Cells(1, cnum)).Value 'note the two "." before "Cells"
    
    end with
    
    'do stuff here with the array...
    

    '释放内存 删除 DClist

    结束子

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-28
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-20
      相关资源
      最近更新 更多