简体   繁体   中英

Subscript out of range error in VBA

I am trying to import FDF files(can be multiple) with VBA. 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. Meaning you need loop through the array from 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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