在Excel VBA中从稀疏工作表中恢复和sorting数据

我有一个Excel工作簿中的几个稀疏填充表,我需要恢复和sorting特定键的值。 查找将由事件触发。

我可以做到这一点使用循环和数组,但已被警告在VBA循环我想知道是否有一个更优雅的方法可能使用内置的Excel函数。

速度很重要。

UID| UK | FR | DE | FI | LUX | ..... 1 | 0 | 0 |0.03| 0.1| 0 | ..... 2 | 0 | 0 | 0 | 0 | 0.5 | ..... 3 |0.01| 0 | 0 | 0 | 0.09| ..... 4 | 0 | 0 | 0 | 0.2| 0 | ..... 5 |0.31|0.07| 0 | 0 | 0 | ..... . . . . . . . . . . . . . . . . . . . . . . . . 

所以在这个示例表中,我想要返回每个唯一ID的地理分布并对其进行sorting,省略任何0权重的区域。

桌子上有几千个UID和几千个地区。

目前还不清楚你想要完成什么,但是我会这么说:没有内置的Excel函数可以实现,或者至less不如自己编写代码。

尝试这个:

 Dim rngData As Range Dim varDummy As Variant Dim iUID As Long, iRegion As Long Set rngData = Range("A2:Z50") ' or wherever your data is varDummy = rngData ' Reads in whole range at once into array. (Must be Variant.) ' Much quicker than accessing one cell at a time. ' Go nuts with loops here. For iUID = LBound(varDummy,1) To UBound(varDummy,1) For iRegion = LBound(varDummy,2) To UBound(varDummy,2) If varDummy(iUID,iRegion) = 0 Then ' Nothing here, ignore this cell. ' Do nothing. Else ' Do your thang ' ... code for whatever it is you want to do goes here. End If Next iRegion Next iUID