创建一个对称矩阵 [英] Creating a symmetric matrix

查看:137
本文介绍了创建一个对称矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习如何在VBA中创建对称矩阵.例如,在第一步中,我想选择Range("C3:I3"),然后复制到Range("B4:B10").在第二阶段,应选择Range("D4:I4"),然后复制到Range("C5:C10").它应该这样下去.

I want to learn how to create a symmetric matrix in VBA. For example in the first step I want to choose Range("C3:I3") then copy to the Range("B4:B10"). In the second stage it should choose Range("D4:I4") then copy to the Range("C5:C10"). It should go on like that.

推荐答案

我有一些代码可以做到这一点.您选择的单元格必须在数字范围内.

I have some code to do it. Your selected cell must be within the range of numbers.

1.如果对角线为空.

代码:

Sub making_symmetric_matrix()

Dim i As Long, j As Long
Dim rng As Range
Set rng = Selection.CurrentRegion
Dim rngStart As Range
Set rngStart = Cells(rng.Row, rng.Column - 1)

For i = 1 To rng.Rows.Count
    For j = i To rng.Columns.Count
        rngStart.Offset(j, i - 1).Value = rngStart.Offset(i - 1, j).Value
    Next
Next

End Sub


2.如果对角线不为空.

代码:

Sub making_symmetric_matrix2()

Dim i As Long, j As Long
Dim rng As Range
Set rng = Selection.CurrentRegion
Dim rngStart As Range
Set rngStart = Cells(rng.Row, rng.Column)

For i = 1 To rng.Rows.Count
    For j = i To rng.Columns.Count
        rngStart.Offset(j - 1, i - 1).Value = rngStart.Offset(i - 1, j - 1).Value
    Next
Next

End Sub

这篇关于创建一个对称矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆