如何通过一个范围,并填写另一个工作表的单元格的值?

在我的Excel工作表中,我有这样的东西:

1 2 3 John Paul Mike 1 John 0 1 1 2 Paul 1 0 3 Mike 1 0 

这与对称matrix类似。 注意:

  • 每个人都有一个ID;

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

我需要的是一个macros,通过“matrix”,并按以下格式输出到另一个工作表的值:

 From To Strenght 1 2 1 1 3 1 2 1 1 3 1 1 

这是我到目前为止,但不工作,因为它返回错误“对象是必需的”,指向strenghts

 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 

有关如何做到这一点的任何提示?

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

假设你的工作表是这样的

在这里输入图像说明

使用这个代码。 我已经评论了这个代码,以便你不会理解它。 如果你这样做,然后简单地问:)

 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的输出。 按适用情况更改。

在这里输入图像说明

摆脱Set Set strenghts = ... 只要写strenghts = ... Set仅用于设置对象的引用。 strenghts不是一个对象,它是一个数值。

另外,我不确定这个拼写错误是否是故意的,但通常拼写为“ strenghts ”。