简体   繁体   English

在范围函数(Excel-VBA)中引用命名范围

[英]Referencing Named Ranges in Range Function (Excel - VBA)

I'm trying to write a macro that takes a string variable as an input, with the string variable referencing a named range. 我正在尝试编写一个将字符串变量作为输入的宏,该字符串变量引用一个命名范围。

Currently what I have is: 目前我拥有的是:

Sub SubItems()

Dim M As String
    M = "=R[-1]C"
    'where M refers to row above, currently it is Manufacturers

Dim g As Range

Set g = Range(" & M & ")

ActiveCell.Value = g(2)
'For Example

End Sub

The problem is with the Set g = Range(" & M & ") syntax 问题在于Set g = Range(“&M&”)语法

I want the input argument for the Range function to be what M is, and not the literal letter M. Similar to how in C you would do printf('%s', M) for example. 我希望Range函数的输入参数是M,而不是文字字母M。例如,类似于C语言中的printf('%s',M)。

Edit: 编辑:

Currently how I have the excel sheet setup, is that you select a main item from a drop down menu. 目前,我如何设置Excel工作表是从下拉菜单中选择一个主要项目。 Then I want to select the cell below the main item and automatically fill in the rows with sub items. 然后,我要选择主要项目下方的单元格,并自动用子项目填充行。 The sub items are stored in a named range that is named after the main item. 子项存储在以主项命名的命名范围中。

Hence I want my macro to automatically read the row above it (Main Item) hence why I have M = "=R[-1]C". 因此,我希望宏自动读取其上方的行(主要项目),因此为什么我有M =“ = R [-1] C”。 Then I want to input that into the range function and that's the problem I'm currently facing. 然后,我想将其输入到range函数中,这就是我当前面临的问题。

I hope this clarifies my problem more clearly. 我希望这可以更清楚地阐明我的问题。

String literals are encased in double quotes, so you're essentially referencing a named Range called & M & . 字符串文字用双引号引起来,因此您实际上是在引用名为&M&的命名范围。

If you're using a string variable , remember that its value is surrounded by quotes as well. 如果您使用的是字符串变量 ,请记住其值也用引号引起来。 So it should just be Range(M) , or Range("=R[-1]C") , the two are the same. 因此,应该只是Range(M)Range("=R[-1]C") ,两者是相同的。

Note: =R[-1]C is a very strange name, are you sure you meant this? 注意:= R [-1] C是一个非常奇怪的名称,您确定是这个意思吗? Make sure you understand the difference between a named range and what's in the range (it looks like a formula)! 确保您了解命名范围和范围内的内容之间的区别(它看起来像一个公式)! Perhaps some description on what =R[-1]C is and I can help you more? 也许关于= R [-1] C是什么的描述,我可以为您提供更多帮助?

The problem is that your variable M describes a relative reference, and I do not believe you can apply a relative reference to a Range object in the manner you describe. 问题在于您的变量M描述了一个相对引用,并且我认为您不能以您描述的方式将一个相对引用应用于Range对象。 Perhaps you want to attack your problem from a different angle? 也许您想从另一个角度来解决您的问题? For instance: 例如:

You could use your method but rather set an absolute reference, eg 您可以使用您的方法,而是设置一个绝对引用,例如

M = "A5"
Debug.Print Range(M).Value

Or alternatively you could specify a relative reference using code such as: 或者,您也可以使用以下代码指定相对引用:

debug.print activecell.Offset(-1,0).Value

What I have understood so far - you have a named range, called M and you want to assign it to a VBA range. 到目前为止,我已经了解了-您有一个名为M的命名范围,并且想要将其分配给VBA范围。 If this is the case, this is the code to achieve it: 如果是这种情况,这是实现它的代码:

Sub TestMe()

    Dim g As Range
    Set g = ActiveSheet.[M]
    Debug.Print g.Address

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM