我正在尝试使用 vba 在 excel 中选择一个单元格。我希望能够使用变量(i & e)来选择一个单元格。其中i = 3和e = 13我希望能够
ActiveCell(e, i).Activate
它选择单元格C13。
ActiveCell(e, i).Activate
确实有效,但仅当原始活动单元格为 A1 时。任何帮助将不胜感激!
我在代码中添加了注释,所以它应该很容易理解。
先运行下面的代码
Sub Main()
Dim i As Long ' row variable
Dim e As Long ' column variable
i = 3 ' row 3
e = 13 ' column 13 ("M")
' this will put: Cells(3,13) in Range "M3"
Cells(i, e) = "Cells(" & i & ", " & e & ")"
' if you want to offset the current active cell then
' use Offset(x, y)
' use negative x to offset up
Cells(i, e).Offset(-1, 0) = "1 up"
' use positive x to offset down
Cells(i, e).Offset(1, 0) = "1 down"
' use negative y to offset left
Cells(i, e).Offset(0, -1) = "1 left"
' use positive y to offset right
Cells(i, e).Offset(0, 1) = "1 right"
' same principles apply when using range object
Dim r As Range
Set r = Cells(i, e)
r.Offset(-2, 0) = "2 up"
r.Offset(2, 0) = "2 down"
r.Offset(0, -2) = "2 left"
r.Offset(0, 2) = "2 right"
Columns.AutoFit
End Sub
然后看看你的工作表并分析什么命令做什么:)