2017-02-06 14 views
1

このコードでは、「If If Block Without If」とエラーが表示され続けます。私はそれを見て、問題を見ることができない、それを印刷し、すべてのIfステートメントを彼らの結合End Ifに接続し、すべてが正しいように見える。終了Access VBAで私に適合する場合

ブロックがある/ End Withブロックのようなものがありますか?

Private Sub cmd__Import_Eligibility_Click() 


    ' Requires reference to Microsoft Office 11.0 Object Library. 

    Dim fDialog As FileDialog 
    Dim varFile As Variant 
    Dim filelen As Integer 
    Dim filename As String 
    Dim tblname As String 


    ' Set up the File Dialog. 
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker) 
    fDialog.InitialFileName = "oo*.*" 
    With fDialog 


     ' Set the title of the dialog box. 
     .Title = "Please select a file" 

     ' Clear out the current filters, and add our own. 
     .Filters.Clear 

     .Filters.Add "Excel Spreadsheets", "*.xls*" 
     .Filters.Add "Comma Separated", "*.CSV" 
     .Filters.Add "All Files", "*.*" 

     ' Show the dialog box. If the .Show method returns True, the 
     ' user picked at least one file. If the .Show method returns 
     ' False, the user clicked Cancel. 
     If .Show = True Then 

     'Loop through each file selected and add it to our list box. 
     varFile = fDialog.SelectedItems(1) 
     If Right(varFile, 4) = ".xls" Or Right(varFile, 5) = ".xlsx" Then 


      'get only file name 
      For a = Len(varFile) To 1 Step -1 
       If Mid(varFile, 1) = "\" Then 
        filelen = a 
       End If 
      Exit For 
      filename = Right(varFile, filelen) 
      tblname = Left(filename, InStr(filename, ".") - 1) 
      DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, tblname, filename, True 
     End If 'ERRORS OUT ON THIS LINE ========================== 
     Else 
     MsgBox "You clicked Cancel in the file dialog box." 
     End If 
    End With 

End Sub 
+1

あなたの 'Exit For'は' filelen = a'の中のIF文の中にあるべきで、 'Next a'をその場所に置くべきです。 –

答えて

5

As Scott posted as a comment、あなたのFor...Nextループ構文が不正な形式れる:

For a = Len(varFile) To 1 Step -1 
    If Mid(varFile, 1) = "\" Then 
     filelen = a 
    End If 
Exit For 

For...Exit Forループのようなものはありません。あなたがこれを行うには意味:

For a = Len(varFile) To 1 Step -1 
    If Mid(varFile, 1) = "\" Then 
     filelen = a 
     Exit For 
    End If 
Next 

そうでない場合、コンパイラは[おおよそ]この見ている:

自動圧子を実行
If [bool-expression] Then 

    For [for-loop-setup] 
     If [bool-expression] Then 
      [instructions] 
     End If 
     Exit For 
     [instructions] 
End If '<~ expecting "Next" before that "End If" token. 

は、この問題が明らかになりましたでしょう、私は思います。私は、人気のスマートインデント VBEアドインを.NETに移植して、64ビット環境で実行できるオープンソースプロジェクトを管理しています。すべての機能については、rubberduckvba.comを参照してください。

関連する問題