简体   繁体   English

如何根据第一行和第一列中的值对一系列矩阵求和 - Excel VBA

[英]How can I sum a series of matrices based on the values in the first row and column - Excel VBA

I have a series of 5x5 matrices (20 total) and I need to create a global matrix that sums the 4x4 sections of the matrices based on the corresponding values of the first row and column.我有一系列 5x5 矩阵(总共 20 个),我需要创建一个全局矩阵,根据第一行和第一列的相应值对矩阵的 4x4 部分求和。 Refer image below.参考下图。

https://i.stack.imgur.com/xUAEL.png https://i.stack.imgur.com/xUAEL.png 在此处输入图片说明 The boxed 4x4 matrices are the elements that I need to sum.装箱的 4x4 矩阵是我需要求和的元素。

The global matrix will be the sum of all of the corresponding values that match the first row and column.全局矩阵将是与第一行和第一列匹配的所有相应值的总和。 For example, in the image above, the values from k1 and k2 that get added together will be the cells corresponding to 10,10 (top left for both 4x4 matrices), which is 0.000 + 0.010.例如,在上图中,来自 k1 和 k2 的值加在一起将是对应于 10,10(两个 4x4 矩阵的左上角)的单元格,即 0.000 + 0.010。 The matrices k3 and k4 don't have a 10,10 cell, so no values will be added from these matrices.矩阵 k3 和 k4 没有 10,10 单元格,因此不会从这些矩阵添加任何值。

So the global matrix will look similar to the below image (note the matrix is incomplete).所以全局矩阵看起来类似于下图(注意矩阵是不完整的)。

https://is https://is tack.imgur.com/We8oO.png [![输入我 mage description here] 3 ] 3 If possible, I'd like to write a macro that can do this in order to save me having to do it manually. mage description here] 3 ] 3如果可能,我想编写一个可以执行此操作的宏,以节省我必须手动执行的操作。 I've tried a couple of match functions but can't seem to get anywhere with it given my novice coding ability.我已经尝试了几个匹配函数,但鉴于我的新手编码能力,它似乎无处可去。

Any suggestions would be greatly appreciated.任何建议将不胜感激。

After making the result into an array in advance, you can add the contents of each area according to the index number.预先把结果做成数组后,就可以根据索引号添加各个区域的内容了。

Sub test()
    Dim vResult(1 To 20, 1 To 20)
    Dim vDB As Variant
    Dim rngTable As Range
    Dim rng As Range
    Dim i As Integer, j As Integer
    
    '5x5 Matrix first cell
    Set rngTable = Range("d2,d10,L2,L10") '<~~ Add more first cells from another table.
    
    For Each rng In rngTable
        vDB = rng.Resize(5, 5)
        For i = 2 To 5
            For j = 2 To 5
                vResult(vDB(i, 1), vDB(1, j)) = vResult(vDB(i, 1), vDB(1, j)) + vDB(i, j)
            Next j
        Next i
    Next rng
    
    'Result Matrix
    'The result is displayed based on cell e19, but if the cell position is adjusted, the result is displayed in another cell.
    Range("e19").Resize(UBound(vResult, 1), UBound(vResult, 2)) = vResult

End Sub

Image图片

在此处输入图片说明

Sum Matrices求和矩阵

  • Carefully adjust the values in the constants section.仔细调整常量部分中的值。

The Code编码

Option Explicit

Sub sumMatrices()
    ' Source
    Const srcName As String = "Sheet1"
    Const srcFirstCell As String = "A1"
    Const hGap As Long = 2
    Const vGap As Long = 2
    Const hCount As Long = 10
    Const vCount As Long = 2
    Const hSize As Long = 5
    Const vSize As Long = 5
    ' Target
    Const tgtName As String = "Sheet2"
    Const tgtFirstCell As String = "B2"
    Const hMax As Long = 20
    Const vMax As Long = 20
    ' Other
    Dim wb As Workbook
    Set wb = ThisWorkbook ' The workbook containing this code.
    
    ' Define first Source Range.
    Dim src As Worksheet
    Set src = wb.Worksheets(srcName)
    Dim rng As Range
    Set rng = src.Range(srcFirstCell).Resize(hSize, vSize)
    
    ' Write values from Source Ranges to arrays of Jagged Source Array.
    
    Dim Source As Variant
    ReDim Source(1 To hCount * vCount)
        
    Dim hOffs As Long
    Dim vOffs As Long
    hOffs = hSize + hGap
    vOffs = vSize + vGap
    
    Dim hCurr As Long
    Dim vCurr As Long
    Dim i As Long
    Dim j As Long
    Dim l As Long
    
    For i = 1 To hCount
        hCurr = (i - 1) * hOffs
        For j = 1 To vCount
            vCurr = (j - 1) * vOffs
            l = l + 1
            Source(l) = rng.Offset(hCurr, vCurr).Value
        Next j
    Next i
    
    ' Write values from arrays of Jagged Source Array to Target Array.
    Dim Target As Variant
    ReDim Target(1 To hMax, 1 To vMax)
    
    For l = 1 To UBound(Source)
        For i = 2 To hSize
            For j = 2 To vSize
                Target(Source(l)(i, 1), Source(l)(1, j)) _
                  = Target(Source(l)(i, 1), Source(l)(1, j)) _
                  + Source(l)(i, j)
            Next j
        Next i
    Next l
    
    ' Write values from Target Array to Target Range.
    Dim tgt As Worksheet
    Set tgt = wb.Worksheets(tgtName)
    tgt.Range(tgtFirstCell).Resize(hMax, vMax).Value = Target
    
    ' Inform user.
    MsgBox "Data transferred.", vbInformation, "Success"

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Excel中用基于列的查找中的查找值求和? - How to sum a row in Excel with lookup values from a column based lookup? Excel VBA:需要更有效的解决方案,以根据第一列的值将此矩阵排序为新矩阵-花费的时间太长 - Excel VBA: need more efficient solution to sorting this matrix into new matrices based on values of first column - takes too long Excel VBA根据一列中的值选择是转移到下一个空行还是首先保留一个空行 - Excel VBA Based on values in one column choose whether to transfer to next empty line or to leave an empty row first 如何根据给定月份对Excel列中的值求和? - How can I sum values in an Excel column based upon a given month? 在 Excel (VBA) 中,如何根据表列中的值隐藏工作表 - In Excel (VBA) how can I hide worksheets based on values in table column 根据所选名称对所有行求和 Excel 中的第一列 - SUM all row based on selected name First Column in Excel VBA:根据列中第一次出现的值删除 excel 行 - VBA: Deleting an excel row based on first occurrence of a value in a column VBA Excel-如何根据另一个单元格的值自动在excel中填充系列? - VBA Excel - How can I automatically fill a series in excel, based on the value of another cell? 使用excel公式基于其他列的首次出现的总和值 - Sum values based on first occurrence of other column using excel formula Excel VBA - 从第一行复制值并粘贴到列中 - Excel VBA - Copy Values from Row First and Paste into Column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM