我一直试图让这段代码工作数周。我可以让它'打印'到表格的第一列和行(D4)。之后,似乎它忽略了在其后的每个列和行中输入更多数据的循环。
我试图让它将第一列中的数字与第三行中的相应数字进行比较,如果它们匹配,则粘贴右侧列下同一行中的单元格值(101s)数。如果有人能看到我出错的地方,那就太好了。我已粘贴下面的工作表和代码。
单:
Sub Fill()
Dim col As Integer
Dim row As Integer
For col = 1 To 3
For row = 1 To 3
If Cells(3, col + 3) = Cells(row + 3, 1) Then
Cells(row + 3, col + 3).Value = Cells(row + 3, col + 2)
End If
Next row
Next col
End Sub
VBA代码:
| A | B | C | D | E | F |
--+-----+-----+-----+-----+-----+-----+
3 | | | | 1 | 2 | 3 |
4 | 1 | | 101 | 101 | | |
5 | 2 | | 101 | | 101 | |
6 | 3 | | 101 | | | 101 |
应该导致
DLookup()
答案 0 :(得分:0)
我相信col + 2
只是3
,即从C列获取值:
Sub Fill()
Dim col As Integer
Dim row As Integer
For col = 1 To 3
For row = 1 To 3
If Cells(3, col + 3) = Cells(row + 3, 1) Then
Cells(row + 3, col + 3).Value = Cells(row + 3, 3)
End If
Next row
Next col
End Sub
如果您使用以下内容,则会更容易阅读:
Sub Fill()
Dim hor As Long
Dim ver As Long
For hor = 4 To 6 ' it will be easier to understand if you use the counter
' to refer to the actual column
For ver = 4 To 6 ' it will be easier to understand if you use the counter
' to refer to the actual row
If Cells(3, hor).Value = Cells(ver, "A").Value Then
Cells(ver, hor).Value = Cells(ver, "C").Value
End If
Next ver
Next hor
End Sub
当然,这可以使用单元格D4中的以下公式来实现
=IF(D$3=$A4,$C4,"")
并将该公式复制到D4:F6。
答案 1 :(得分:0)
尝试以下代码(更新代码):
Sub Fill()
Dim col As Integer
Dim row As Integer
For col = 4 To 6
For row = 2 To 4
If Cells(row, 1) = Cells(1, col) Then
Cells(row, col).Value = Cells(row, 3)
End If
Next row
Next col
End Sub