如何使用间接引用在VBA中选择单个单元格或范围 [英] How to use indirect reference to select a single cell or range in vba

查看:356
本文介绍了如何使用间接引用在VBA中选择单个单元格或范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要一个代码来选择一个单元格,但是要选择的那个单元格就会发生变化.我在工作簿中有一个单元格,它将确定应该是哪个单元格.单元格A1包含应选择的单元格#.

I need simply a code for selecting a cell, however that cell to select changes. I have a cell in the workbook that will identify what cell it should be. Cell A1 contains the cell # that should be selected.

在此示例中,单元格A1包含单词"P25",因此我希望下面的代码为A25的间接单元格引用A1,因此选择单元格P25.

In this example cell A1 contains the word "P25", so I want the below code to reference A1 for the indirect cell ref to P25, thus select cell P25.

我分别尝试了这两行:

Sub IndirectCellSelect()

     Sheet.Range(INDIRECT(A1)).Select
     Range(INDIRECT(A1)).Select

End Sub

当得到单词INDIRECT

I get the error Sub or Function is not defined, when it gets to the word INDIRECT

推荐答案

对已发布的代码稍作改动即可:

A slight alteration to the posted code works:

Range([indirect("a1")]).Select

但是我建议尝试以下任何一种方法:

but I would advise to try either of these instead:

Sheet.Range(Sheet.Range("A1").Value).Select
Range(Range("A1")).Select

第一个更为明确,在生产代码中建议使用.

the first being more explicit and is recommended in production code.

这篇关于如何使用间接引用在VBA中选择单个单元格或范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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