[英]Subscript out of range error in VBA
I am trying to import FDF files(can be multiple) with VBA. 我正在尝试使用VBA导入FDF文件(可以是多个)。 When I run my code I got Subscript out of range error.
当我运行代码时,出现下标超出范围错误。
I know that the error suggests the worksheet it is looking for does not exist but I don't believe the code below actually defines the worksheet name which is probably the cause of the problem? 我知道该错误表明它正在查找的工作表不存在,但我不相信下面的代码实际上定义了工作表名称,这可能是问题的原因吗?
Can I have assistance in where, and what, code to insert to address this error? 在解决此错误的代码插入位置和内容方面,我可以获得帮助吗? This is my code what I tried:
这是我尝试的代码:
Sub FDFImport()
Dim OutSH As Worksheet
Dim Fname As Variant, f As Integer
Fname = Application.GetOpenFilename("FDF File,*.fdf", 1, "Select One Or More Files To Open", , True)
For f = 1 To UBound(Fname)
Open Fname(f) For Input As #1
Do While Not EOF(1)
Line Input #1, myvar
arr = Split(myvar, Chr(10))
arr2 = Split(arr(4), "/V")
If InStr(1, myvar, "(Contact)") > 0 Then
Set OutSH = Sheets("Contact")
outrow = OutSH.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
For i = 1 To 8
placer = InStr(1, arr2(i), ")")
OutSH.Cells(outrow, i).Value = Left(arr2(i), placer - 1)
Next i
Else
Set OutSH = Sheets("NoContact")
outrow = OutSH.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
For i = 1 To 12
placer = InStr(1, arr2(i), ")")
OutSH.Cells(outrow, i).Value = Left(arr2(i), placer - 1)
Next i
End If
Loop
Close #1
Sheets("Contact").Cells.Replace what:="(", replacement:=""
Sheets("NoContact").Cells.Replace what:="(", replacement:=""
Next f
End Sub
This is just a guess based on what you have posted but give this a try 这只是基于您已发布内容的猜测,请尝试一下
Sub FDFImport()
Dim OutSH As Worksheet
Dim Fname As Variant, f As Integer
Dim myvar, arr, arr2, outrow, i, placer
Fname = Application.GetOpenFilename("FDF File,*.fdf", 1, "Select One Or More Files To Open", , True)
If VarType(Fname) = vbBoolean Then
Exit Sub
End If
For f = LBound(Fname) To UBound(Fname)
Open Fname(f) For Input As #1
Do While Not EOF(1)
Line Input #1, myvar
arr = Split(myvar, Chr(10))
arr2 = Split(arr(4), "/V")
If InStr(1, myvar, "(Contact)") > 0 Then
Set OutSH = Sheets("Contact")
outrow = OutSH.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
For i = 0 To 7
placer = InStr(1, arr2(i), ")")
OutSH.Cells(outrow, i).Value = Left(arr2(i), placer - 1)
Next i
Else
Set OutSH = Sheets("NoContact")
outrow = OutSH.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
For i = 0 To 11
placer = InStr(1, arr2(i), ")")
OutSH.Cells(outrow, i).Value = Left(arr2(i), placer - 1)
Next i
End If
Loop
Close #1
Sheets("Contact").Cells.Replace what:="(", replacement:=""
Sheets("NoContact").Cells.Replace what:="(", replacement:=""
Next f
End Sub
When you Split
the array will be 0 based. Split
,数组将基于0。 Meaning you need loop through the array from 0 to X
. 这意味着您需要从
0 to X
遍历数组。 When you are looping arr2
you have For i = 1 To 8
my guess is it should be For i = 0 To 7
you are doing the same for arr
I have changed this is my answer. 当您循环
arr2
, For i = 1 To 8
我的猜测应该是For i = 0 To 7
对于arr
您也做同样的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.