如何读取 excel vba 中的矩阵?
how to read matrix in excel vba?
我想从 4*4 矩阵(二维数组)中读取和存储值,并在我的进一步程序中使用它。我说的是 VBA 对应 Excel。数据在 Excel sheet 中,我想通过 VBA 读取它。我对此很陌生,但学得很快。请帮我做一下。
这是我在 sheet
中的数据
一个 2 5 6
b 6 8 7
c 3 6 9
这就是我想要做的
a 0 2 7 13
b 0 6 14 21
c 0 3 9 18
我需要从 sheet 中读取 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
如果你想处理一个范围内的值,你不需要先将它们存储在数组中。您可以使用类似于以下的代码遍历范围内的每个单元格:
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
我想从 4*4 矩阵(二维数组)中读取和存储值,并在我的进一步程序中使用它。我说的是 VBA 对应 Excel。数据在 Excel sheet 中,我想通过 VBA 读取它。我对此很陌生,但学得很快。请帮我做一下。
这是我在 sheet
中的数据一个 2 5 6
b 6 8 7
c 3 6 9
这就是我想要做的
a 0 2 7 13
b 0 6 14 21
c 0 3 9 18
我需要从 sheet 中读取 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
如果你想处理一个范围内的值,你不需要先将它们存储在数组中。您可以使用类似于以下的代码遍历范围内的每个单元格:
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