2016-04-22 219 views
0

我想读取和存储来自4 * 4矩阵(2维数组)的值,并将其用于我的进一步程序中。我正在谈论Excel的VBA。数据在Excel工作表中,我想通过VBA读取它。我对此很陌生,但学得很快。请帮我做。如何在excel vba中读取矩阵?

这是我在片数据

b 6分配8 7

的C 3 6 9

这就是我想要做

一个0 2 7 13

b 0 6 14 21

c 0 3 9 18

我需要从表中读取3 * 3矩阵并将其转换为累积矩阵,如图所示。 (添加以前的号码并继续)。

基本上我模拟一个马尔可夫链,需要计算一个人经过每个阶段的次数。 子实施例7()

Dim A As Double, B As Double, C As Double, PC(4, 4) As Double, row As Double, maxrwo As Double, col As Double, maxcol As Double 

Range("o5").Activate 

For i = 1 To 4 
For j = 1 To 4 
PC(i, j) = ActiveCell.Value 

ActiveCell.Offset(0, 1).Select 

Next j 

ActiveCell.Offset(1, -4).Select 
Next i 

Range("T4") = PC(2, 4) 

End Sub 
+0

欢迎来到Stackoverflow!您能否详细说明您的问题,比如代码或其他事情,以便人们能够尽早解决问题并帮助您?谢谢! – manetsus

+1

从一个范围到一个VBA变量可以很容易地得到一个二维数组。例如,如果'A'被声明为类型'Variant',并且数据在范围A1:D4中,那么'A = Range(“A1:D4”)。Value'将把数据传送到'A'。 –

回答

1

如果你要处理的范围内,你不需要他们先存储在数组中的值。您可以通过使用类似于以下代码循环遍历范围中的每个单元格:

Sub LoopThroughRange() 

Dim currentCell As Range 
Dim desiredRange As Range 
Dim outputCell As Range 
Dim total As Double 

Set outputCell = Range("A6") 
Set desiredRange = Range("Sheet1!A1:D4") 

'This will add the values of each cell in the range and output the total to cell A6 
For Each currentCell In desiredRange 

    total = total + currentCell.Value 

Next currentCell 

outputCell.Value = total 

End Sub