简体   繁体   English

下标超出范围,单元格值包含图纸参考

[英]Subscript out of range with sheet reference from cell value

I get this message with the following block of code. 我通过以下代码块收到此消息。 I know I'm not defining my range correctly but I'm a bit lost. 我知道我没有正确定义我的范围,但我有点迷茫。 Thanks! 谢谢!

 Sub clean_data()


Dim Preliminary_MTM As Worksheet
Dim Sheetlink As String

Sheetlink = Worksheets("Input for model").Range("E17").Value

Set Preliminary_MTM = Sheets(Sheetlink)

Preliminary_MTM.Activate

Based on your comment that the contents of cell "E17" is exactly Worksheets("Preliminary MTM Q1") then you would have to have a worksheet named "Worksheets("Preliminary MTM Q1")" (ie that's what you would see on the worksheet tab in your Excel file) and my guess is you don't have one with that name. 根据您的评论,单元格“ E17”的内容正好是Worksheets("Preliminary MTM Q1")那么您将必须有一个名为“ Worksheets(” Preliminary MTM Q1“)”的工作表(即您将在Excel文件中的“工作表”标签),我想您还没有一个使用该名称的工作表标签。 As a matter of fact, you can't have a worksheet with that name because it's too long: 实际上,您不能使用该名称的工作表,因为它太长了:

在此处输入图片说明

What you want in E17 is simply Preliminary MTM Q1 so that when you reference it in this code: 在E17中您想要的只是简单的Preliminary MTM Q1因此在此代码中引用它时:

Set Preliminary_MTM = Worksheets(Worksheets("Input for model").Range("E17").Value2)

it accesses the contents of that cell and looks for the worksheet named there. 它访问该单元格的内容并查找在那里命名的工作表。

Note: I've changed .Value for .Value2 . 注意:我已将.Value更改为.Value2 .Value2 gives you the completely unformatted text in the cell, while .Value will return formatting information - this can cause problems. .Value2在单元格中为您提供完全未格式化的文本,而.Value将返回格式信息-这可能会导致问题。

Note: I've reduced the code to the one line because that's all that's necessary. 注意:我已经将代码缩减为一行,因为这是必需的。 If you're actually using the variable sheetlink elsewhere, then you'll need to include that. 如果您实际上在其他地方使用了变量sheetlink ,则需要将其包括在内。

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

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