Vba 如何浏览一个范围并填写另一个工作表';有值的单元格?

Vba 如何浏览一个范围并填写另一个工作表';有值的单元格?,vba,excel,Vba,Excel,在我的Excel工作表中,我有如下内容: 1 2 3 John Paul Mike 1 John 0 1 1 2 Paul 1 0 3 Mike 1 0 这类似于对称矩阵。请注意: 每个人都有一个身份证 为了简化,我将值设置为1,但它们可以从1开始 到20岁。0永远是0。还有一些空白 我需要的是一个宏,它遍历“矩阵”,并以以下格式将值输出到另一

在我的Excel工作表中,我有如下内容:

          1      2      3
         John   Paul   Mike     
1  John   0      1      1
2  Paul   1      0
3  Mike   1             0
这类似于对称矩阵。请注意:

  • 每个人都有一个身份证

  • 为了简化,我将值设置为1,但它们可以从1开始
    到20岁。0永远是0。还有一些空白

我需要的是一个宏,它遍历“矩阵”,并以以下格式将值输出到另一个工作表中:

From     To     Strenght
  1      2        1
  1      3        1
  2      1        1
  3      1        1
到目前为止,这是我所知道的,但不起作用,因为它返回错误“对象是必需的”,指向
强度

Dim i As Integer, j As Integer, strenghts As Integer

    Set currentCell = Worksheets("Raw_Relationships").Cells(3, 3)

         For i = 1 To 145
         For j = 1 To 145

           If currentCell <> "" Then

             Set currentCell = Worksheets("Raw_Relationships").Cells(i, j).Offset(2, 3)
             Set strenghts = Worksheets("Raw_Relationships").Cells(i, j).Offset(2, 2).Value

             Set Worksheets("Gephi_Data").Cells(i, 1).Offset(150, 0).Value = i
             Set Worksheets("Gephi_Data").Cells(i, 2).Offset(150, 0).Value = j
             Set Worksheets("Gephi_Data").Cells(i, 3).Offset(150, 0).Value = strenghts

           End If

         Next j
         Next i
Dim i为整数,j为整数,strength为整数
设置currentCell=工作表(“原始关系”)。单元格(3,3)
对于i=1到145
对于j=1到145
如果当前单元格为“”,则
设置currentCell=工作表(“原始关系”)。单元格(i,j)。偏移量(2,3)
设置强度=工作表(“原始关系”)。单元格(i,j)。偏移量(2,2)。值
设置工作表(“Gephi_数据”)。单元格(i,1)。偏移量(150,0)。值=i
设置工作表(“Gephi_数据”)。单元格(i,2)。偏移量(150,0)。值=j
设置工作表(“Gephi_数据”)。单元格(i,3)。偏移量(150,0)。值=强度
如果结束
下一个j
接下来我

关于如何做到这一点有什么建议吗?

有很多方法可以实现你想要的。这是一个非常基本的例子,说明你想要什么

假设你的床单看起来像这样

使用此代码。我已经对代码进行了注释,这样您在理解代码时就不会有问题了。如果你这样做了,那么简单地问:)

代码

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim i As Long, j As Long
    Dim rw As Long, col As Long

    '~~> Change this to the relevant sheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    '~~> This is where the output will be generated
    rw = 2: col = 8

    With ws
        '~~> Create Headers of Output
        .Cells(1, col).Value = "From"
        .Cells(1, col + 1).Value = "To"
        .Cells(1, col + 2).Value = "Strength"

        '~~> Looping through rows
        For i = 3 To 5
            '~~> Looping through columns
            For j = 3 To 5
                '~~> Check if the cell is > 0
                If .Cells(i, j).Value > 0 Then
                    '~~> Write the `From` column
                    .Cells(rw, col).Value = .Cells(i, 1).Value
                    '~~> Write the `To` Column
                    .Cells(rw, col + 1).Value = .Cells(1, j).Value
                    '~~> Write the `Strength` Column
                    .Cells(rw, col + 2).Value = .Cells(i, j).Value
                    rw = rw + 1
                End If
            Next j
        Next i
    End With
End Sub
输出

我正在生成Col
H
以后的输出。如适用,进行更改


摆脱
集合中的
集合强度=…
。只需编写
strenghts=…
<代码>设置
仅用于设置对对象的引用<代码>强度不是对象,而是数值


另外,我不确定
strengts
中的打字是否是故意的,但通常拼写为“strengths”。

哇,谢谢!我想我把事情复杂化了。一些快速问题:-是否使用“With ws”将我先前定义的工作表设置为活动工作表?-您是否总是必须在单元格之前使用句点(.)?再次非常感谢您,这无疑促进了我对VBA的理解,并希望了解更多!