简体   繁体   English

Excel VBA“下标超出范围”运行错误“ 9”

[英]Excel VBA “Subscript out of range” Run Error '9'

I have tried to implement the following code so that I can read a cell value and then places this value into another cell on the same Worksheet. 我试图实现以下代码,以便可以读取一个单元格值,然后将该值放入同一工作表上的另一个单元格中。

The worksheet has the following name: TestUserGuidance (Has no spaces) The code is as follows: 工作表具有以下名称:TestUserGuidance(无空格)代码如下:

Sub GuideTest()
    Dim dblPower, dblMass, dblRatedSpeed, dblRefLength, dblAwot, dblEngineSpeed, dblRoadSpeed As Double
    Dim dblPMR As Double

    dblPower = Worksheets("TestUserGuidance").Cell("B1").Value
    Worksheets("TestUserGuidance").Cell("E1") = dblPower
End Sub 

Could anyone please advise where I'm going wrong? 谁能告诉我我要去哪里错了吗?

Thanks 谢谢

在此处输入图片说明

I think vba can't find the TestUserGuidance sheet. 我认为vba找不到TestUserGuidance表。 If I run the following in the immediate window I get an "Object doesn't support this property or method" error: 如果在立即窗口中运行以下命令,则会出现“对象不支持此属性或方法”错误:

?worksheets(1).cell("A1")

If I run this, I get the error you mention "Subscript out of range": 如果运行此命令,则会收到错误消息,提示您“下标超出范围”:

?worksheets("non-existent sheet").cell("A1")

This suggests to me that the active workbook is incorrect since you would have gotten the other error if vba was able to find the worksheet. 这向我表明活动的工作簿是不正确的,因为如果vba能够找到工作表,则将出现另一个错误。 Can you try adding ThisWorkbook (and also use range)? 您可以尝试添加ThisWorkbook(并同时使用范围)吗?

dblPower = ThisWorkbook.Worksheets("TestUserGuidance").Range("B1").Value
ThisWorkbook.Worksheets("TestUserGuidance").Range("E1") = dblPower

Another option would be to rename your worksheet: 另一个选择是重命名工作表:

在此处输入图片说明

Then you won't have to worry about the active workbook (and also get intellisence): 这样,您就不必担心活动的工作簿(也可以提高智能):

shtTestUserGuidance.Range("E1") = shtTestUserGuidance.Range("B1")

Also, I would suggest you make all the corrections suggested by others. 另外,我建议您进行其他人建议的所有更正。 You will likely have other issues moving forward otherwise. 否则,您可能还会遇到其他问题。

Lastly, if the worksheet is like a template and the structure won't change, you'd be better off just using a formula (ie E1's formula: =B1). 最后,如果工作表就像模板,结构不会改变,那么最好只使用一个公式(即E1的公式:= B1)。 Even if there's conditional logic, it might be easier to maintain as a formula rather than vba code. 即使存在条件逻辑,也可能更容易将其维护为公式而不是vba代码。

Here are some troubleshooting steps: 以下是一些故障排除步骤:

Toggle a break point on the line where dblPower is set by clicking on the grey area to the left: 通过单击左侧的灰色区域,在设置dblPower的行上切换断点: 在此处输入图片说明

Also open the Immediate window if it's not already there by pressing ctrl+g: 如果尚不存在,请通过按ctrl + g打开“立即”窗口: 在此处输入图片说明

When you run the code, it should pause at the break point. 运行代码时,它应在断点处暂停。 Now you can inspect what's in scope. 现在您可以检查范围。 Try typing the following into the Immediate window and press enter: 尝试在立即窗口中键入以下内容,然后按Enter:

?Worksheets("TestUserGuidance").Name

or 要么

?ActiveWorkbook.Name

Note that you can also step through the code line by line by pressing F8 while the cursor is somewhere within the subroutine. 请注意,当光标位于子例程内的某个位置时,也可以通过按F8键逐行浏览代码。 Bottom line is that you may need to examine the code line by line in order to figure this out. 底线是您可能需要逐行检查代码以弄清楚这一点。

This is the way to make your code working: 这是使代码正常工作的方式:

Sub GuideTest()
    Dim dblPower, dblEngineSpeed, dblRoadSpeed As Double ' first two are of variant type
    Dim dblPMR As Double

    dblPower = Worksheets(1).Range("B1").Value
    Worksheets(1).Range("E1") = dblPower
End Sub

When you use "E1" or "B1" you should use Range , not Cells. 使用“ E1”或“ B1”时,应使用Range而不是Cells。 In my answer Worksheets(1) refers to Worksheets("TestUserGuidance") . 在我的回答中, Worksheets(1)是指Worksheets("TestUserGuidance")

Only dblRoadSpeed is declared as double. dblRoadSpeed被声明为double。 To fix it, declare all your variables as individual, in vba you can do in 2 ways. 要解决此问题,请将所有变量声明为单独变量,在vba中,您可以通过两种方式进行操作。

Dim dblPower As Double, dblMass As Double

Or 要么

Dim dblPower As Double
Dim dblMass As Double

Also, you don't need to declare any variable to do what you are doing. 另外,您无需声明任何变量即可执行操作。 You can copy the value in one step. 您可以一步一步复制该值。

Worksheets("TestUserGuidance").Range("E1").Value = Worksheets("TestUserGuidance").Range("B1").Value 

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

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