[英]Run-time error 9: Subscript out of range while working with string arrays
I'm new to vba script. 我是vba脚本的新手。 I am trying to write a function below but couldn't make it out successfully.
我正在尝试在下面编写一个函数,但无法成功实现。 I really appreciate any help I can get on this.
我真的很感谢我能为此提供的任何帮助。
The code is: 代码是:
Option Explicit
Dim status As String
Sub StartModule()
Dim index As Integer
Dim result As String
Dim a As Integer
Dim Name As Variant
Range("D4").Value = 1
Range("D5").Value = 5
Range("D6").Value = 9
Range("D7").Value = 2
Dim o: Set o = CreateObject("NAddIn.Functions")
status = ""
Do Until status = "DADA"
result = o.getRandomNumber
Name = Split(result, ",")
If Trim(Name(3)) = Trim(Range("D4").Value) Then
Range("C4").Value = "one"
End If
If Trim(Name(3)) = Trim(Range("D5").Value) Then
Range("C5").Value = "five"
End If
If Trim(Name(3)) = Trim(Range("D6").Value) Then
Range("C4").Value = "nine"
End If
If Trim(Name(3)) = Trim(Range("D7").Value) Then
Range("C7").Value = "two"
End If
Wait 1 '<~~ Wait for a second
If status = "EXIT" Then Exit Do
Loop
End Sub
Sub StopModule()
status = "EXIT"
End Sub
Private Sub Wait(ByVal nSec As Long)
nSec = nSec + Timer
While nSec > Timer
DoEvents
Wend
End Sub
This vba script is calling a getRandomNumber()
which is a user defined function in dll file. 此vba脚本正在调用
getRandomNumber()
,这是dll文件中的用户定义函数。 It generates string of random numbers in the range(1,10); 它生成范围为(1,10)的随机数字符串; Then the thrid random number in the string is compared with cell values in excel to update cells in excel with some string values.
然后将字符串中的第三随机数与excel中的单元格值进行比较,以使用一些字符串值更新excel中的单元格。
Bu,the problem is I am getting an error Run-time error 9: Subscript out of range
at line If Trim(Name(3)) = Trim(Range("D4").Value)
then. Bu,问题是我遇到了错误
Run-time error 9: Subscript out of range
If Trim(Name(3)) = Trim(Range("D4").Value)
则下Run-time error 9: Subscript out of range
。
Then the thrid random number in the string is compared with cell values in excel to update cells in excel with some string values.
然后将字符串中的第三随机数与excel中的单元格值进行比较,以使用一些字符串值更新excel中的单元格。
You're comparing fourth random number. 您正在比较第四个随机数。 Your 'Name' elements are (0),(1),(2),(3),(4),... I suppose there is no element Name(3) as you're getting 'out of range' error.
您的“名称”元素是(0),(1),(2),(3),(4),...我想当您遇到“超出范围”错误时,没有元素名称(3) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.