arrays - 传递数组值的麻烦
问题描述
我有一个 sub 用我的电子表格中固定范围内的用户定义值列表填充数组。我想从另一个调用这个 sub,并利用数组中的值来驱动 For Each 循环。
当我调用 sub 时,getInvoiceList
我可以看到它确实invoiceList
用用户的值填充了数组。但他们不会传回我打电话的潜艇。
Public Sub columnLoop()
Dim i As Long, j As Long
getInvoiceList
Stop 'to view array values
Sheets("Calculator").Columns(10).Font.Color = vbBlack
For i = 0 To UBound(invoiceList)
'Loops through column for specific value(as declared)
'Recolors text when current cell value = specific value
For j = 3 To Range("NumFilledRows").Value
If Sheets("Calculator").Cells(j, 10).Value = invoiceList(i) Then
Sheets("Calculator").Cells(j, 10).Font.Color = vbRed
End If
Next j
Next i
End Sub
'Fill array from fixed range
Public Sub getInvoiceList()
Dim invoiceList() As Variant
invoiceList = Sheet2.Range("C4:C14")
Stop 'allows review of array.
End Sub
当我调用子“getInvoiceList”时,我可以看到它确实用用户的值填充了数组 invoiceList。但他们不会传回我打电话的潜艇。
Run-time error '13': Type Mismatch
调试带我去For i = 0 to UBound
生产线。Locals 窗口显示invoiceList
,但value = empty
在停止行和单击调试后都有。
解决方案
您可以将变量传入/传出子。
Public Sub columnLoop()
Dim i As Long, j As Long, InvoiceList As Variant
getInvoiceList InvoiceList
Stop 'to view array values
Sheets("Calculator").Columns(10).Font.Color = vbBlack
For i = 0 To UBound(InvoiceList)
'Loops through column for specific value(as declared)
'Recolors text when current cell value = specific value
For j = 3 To Range("NumFilledRows").Value
If Sheets("Calculator").Cells(j, 10).Value = InvoiceList(i) Then
Sheets("Calculator").Cells(j, 10).Font.Color = vbRed
End If
Next j
Next i
End Sub
'Fill array from fixed range
Public Sub getInvoiceList(ByRef InvoiceList As Variant)
InvoiceList = Application.Transpose(Sheet2.Range("C4:C14").Value)
Stop 'allows review of array.
End Sub
推荐阅读
- python - ModuleNotFoundError:Python 中没有名为 kivy.app 的模块
- django - 使用基于类的视图时,django url 对我不起作用
- c# - 如何在c#中将“1”位附加到二进制整数的前面
- javascript - 为什么 React Component 函数构造函数中的方法在组件初始化后进行评估?
- sql - 需要仅使用连接在一列中获取 2 个表的输出
- android - kotlin 应用程序中的 DataBinding 出错
- javascript - 从函数中获取数据到
渲染方法中的组件 - mongodb - 如何在 mongodb 中使用带有 DataType NumberDecimal 的 insertMany() 插入数据?
- android - TextAlignment 不适用于复选框
- angular - 无法在 Angular 7 上使用解析器预取数据