gpt4 book ai didi

c# - 如何将数组从 C# (VSTO) 项目传递到 VBA 宏

转载 作者:太空宇宙 更新时间:2023-11-03 11:53:08 25 4
gpt4 key购买 nike

我的 VSTO 解决方案存在性能问题,我认为原因主要是 cellColor 逐个单元格设置的方式。

这取决于记录集中的数据,因此每次都不一样。 (我不能使用另一行/列的 copyFormats)

它类似于填充一系列值,只是对于那个值有几种方法。

我想首先在内存中用 C# 创建整个东西(一个 XlColorIndex[,] 数组),我将其传递给类似于下面的 VBA 方法:

Sub fillInterior(ByRef rg As Range, a As Variant)
//a is a double array that represents the colors for the spreadsheet
Dim r As Long, c As Long
Dim tmpRg As Range
r = 1
c = 1
For Each Row In a
For Each colorIdx In Row
Set tmpRg = rg(r, c)
With tmpRg.Interior
.ColorIndex = colorIdx
.PatternColorIndex = xlAutomatic
.PatternColor = xlSolid
End With
c = c + 1
Next
c = 1
r = r + 1
Next
End Sub

我一直在尝试通过以下方式调用这个宏,但还没有成功,非常感谢任何指点:

            Excel.Range rg = this.Range[this.Cells[5, 3], this.Cells[6, 4]];

object[,] test2 = new object[2, 2];
test2[0, 0] = 15;
test2[0, 1] = 15;
test2[1, 0] = 15;
test2[1, 1] = 15;

this.Application.Run("Sheet1.fillInterior", rg, test2,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);

我试过 int[,] -

当我尝试 Nullable int 或 Double 时,我确实遇到了不同的错误:double?[,](可空 double 组):

The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

如果我不尝试可为 null 的类型,我会收到以下 HRESULT 错误(类型不匹配?)

Exception from HRESULT: 0x800A000D

最佳答案

好吧,我应该读过 this更好:应该避免变体,所以如果我可以选择编写我的 VBA,我最好不要使用变体,而是使用适当的数组。

其次,我错误地使用了 VBA 数组,我应该对多维数组 (source) 执行以下操作:

我的 VBA 代码现在看起来像这样:

Sub fillInteriorMulti(rg As Range, Arr() As Long)
Dim N As Long, Ndx1 As Long, Ndx2 As Long
Dim icol As Long
Dim irow As Long
Dim NumDims As Long


// Custom Function: Get the number of array dimensions.
// NumberOfArrayDimensions will return 0
// if the array is not allocated.

NumDims = NumberOfArrayDimensions(Arr:=Arr)
Select Case NumDims
Case 0
// unallocated array
Exit Sub
Case 1
// single dimensional array
For N = LBound(Arr) To UBound(Arr)
With rg(N, 1).Interior
.ColorIndex = Arr(N)
.PatternColorIndex = xlAutomatic
.PatternColor = xlSolid
End With
Next N
Case 2
// 2 dimensional array
For Ndx1 = LBound(Arr, 1) To UBound(Arr, 1)
For Ndx2 = LBound(Arr, 2) To UBound(Arr, 2)
With rg(Ndx1, Ndx2).Interior
.ColorIndex = Arr(Ndx1, Ndx2)
.PatternColorIndex = xlAutomatic
.PatternColor = xlSolid
End With
Next Ndx2
Next Ndx1
Case Else
// Too many dimensions - Do Nothing
End Select
End Sub

Public Function NumberOfArrayDimensions(Arr As Variant) As Integer
// NumberOfArrayDimensions
// This function returns the number of dimensions of an array. An unallocated dynamic array
// has 0 dimensions. This condition can also be tested with IsArrayEmpty.

Dim Ndx As Integer
Dim Res As Integer
On Error Resume Next
// Loop, increasing the dimension index Ndx, until an error occurs.
// An error will occur when Ndx exceeds the number of dimension
// in the array. Return Ndx - 1.
Do
Ndx = Ndx + 1
Res = UBound(Arr, Ndx)
Loop Until Err.Number <> 0

NumberOfArrayDimensions = Ndx - 1
End Function

最后是测试这个的 C# 代码:

            int[] test3 = new int[3];
test3[0] = 15;
test3[1] = 15;
test3[2] = 48;

int[,] test4 = new int[2, 2];
test4[0, 0] = 15;
test4[0, 1] = 15;
test4[1, 0] = 15;
test4[1, 1] = 15;

this.Application.Run("Sheet1.fillInteriorMulti", rg, test4,
missing, missing, missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing);

关于c# - 如何将数组从 C# (VSTO) 项目传递到 VBA 宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1531011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com