2013-09-26 106 views
-2

我想用vba在excel中选择一个单元格。我希望能够使用变量(i & e)来选择单元格。其中I = 3E = 13我希望能够在excel中选择一个带有VBA的单元格

ActiveCell(e, i).Activate 

,并选择单元格C13。

ActiveCell(e, i).Activate 

确实有效,但仅当原始活动单元格为A1时才起作用。任何帮助将不胜感激!

+4

欢迎StackOverflow的什么命令。在这里简单搜索一下“vba excel select cell”应该可以找到几十个这个问题的答案。在发布问题之前,请至少尝试自己找到解决方案,因为以前有人问过并获得答案的机会相当不错。谢谢。 –

+0

如果要通过索引引用列,请查看R1C1语法。 –

+0

看看这个:http://support.microsoft.com/kb/291308 – NoChance

回答

3

我在代码中添加了评论,所以它应该很容易理解。

运行下面的代码首先

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 

然后看看你的表和分析做什么:)

enter image description here

相关问题