我想将第4列中的第2到10行更改为10,但这不起作用。
限制:d As Single
不得更改
Sub ua()
Dim d As Single
Dim i As Integer
d = Cells(i, 4)
For i = 2 To 10
d = 10
Next i
End Sub
答案 0 :(得分:4)
遍历行和列是一种选择:
Sub TestMe()
Dim myRow As Long, myCol As Long
Dim d As Single: d = 10
For myRow = 2 To 10
For myCol = 4 To 10
Worksheets(1).Cells(myRow, myCol) = d
Next myCol
Next myRow
End Sub
另一种方法是在评论中使用@Jeep的单行解决方案-
With Workheets(1)
.Range(.Cells(2, 4), .Cells(10, 10)) = 10
end with
答案 1 :(得分:2)
更改工作表名称,然后尝试:
Sub test()
With Sheet1
.Range(.Cells(2, 4), .Cells(10, 10)).Value = 10
End With
End Sub
答案 2 :(得分:0)
您实际上对代码不执行任何操作(您只设置了一个变量,然后就循环了,而对单元格值没有任何影响)。如果要更改单元格中的值,则必须在代码中编写该值。您可以像这样更改单元格中的值:
Cells(3, 2).Value = 2
如果需要此变量,则可以在循环中编写以下内容:
For i = 2 to 10
Cells(3, i).Value = 1000 'Set the value of the cell to 1000
Next i