在Excel中选择具有VBA的单元格 [英] Selecting a cell with VBA in excel

查看:131
本文介绍了在Excel中选择具有VBA的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用vba在excel中选择一个单元格。我想要使​​用变量(i& e)来选择一个单元格。其中 i = 3 e = 13 我希望能够

  ActiveCell(e,i)。激活

并选择单元格C13。

  ActiveCell(e,i).Activate 

工作,但只有当原始活动单元格为A1时。任何帮助将不胜感激!

解决方案

我在代码中给出了意见,所以应该很容易理解。



首先运行以下代码

  Sub Main()

Dim i As Long'row variable
Dim e As Long'列变量

i = 3'row 3
e = 13'column 13(M )

'这将放置:单元格(3,13)在范围M3
单元格(i,e)=细胞(&,& e $


'如果要偏移当前活动单元格,然后
'使用Offset(x,y)

'使用负x来补偿
单元格(i,e).Offset(-1,0)=1 up

'使用正x来向下偏移
单元格我,e).Offset(1,0)=1 down

'使用负y来向左偏移
单元格(i,e).Offset(0,-1)= 1 left

'使用正y来偏移
单元格(i,e).Offset(0,1)=1 right


'同样的原则适用于使用范围对象
Dim r As Range
设置r =单元格(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

然后看看你的工作表并分析什么命令做什么:)




I am trying to select a cell in excel with vba. I want to be able to use to variables(i & e) to choose a cell. Where i = 3 and e = 13 I would like to be able to

ActiveCell(e, i).Activate

and it select cell C13.

ActiveCell(e, i).Activate

does work but only when original active cell is A1. Any help would be greatly appreciated!

解决方案

I put comments in the code so it should be really easy to understand.

Run the below code first

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

then look at your sheet and analyse what command does what :)

这篇关于在Excel中选择具有VBA的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆