help on text file import error handler

saurabh_546

New Member
Joined
Sep 7, 2009
Messages
19
Dear all
I am importing a text file into the excel
I am working on errorhandler now
there are only three possible runtime errors i can get
1st is user cancels the procedure
2nd is the destination sheet alerady exists
3rd is if text file is not found

I am able to workout first 2 , i need help on 3rd
here is my code

Code:
ErrorHandler:
    If folderName = "" Then
        Application.DisplayAlerts = False
        ShNew.Delete
        Application.DisplayAlerts = True
    Exit Sub
 
    Else
 
  If Err.Description = "Cannot rename a sheet to the same name as another sheet, a referenced object library or a workbook referenced by Visual Basic." Then
 
    MsgBox "An error was encountered while attempting to import the data." & vbCrLf & Err.Description _
    & vbCrLf & "Please rename or delete the sheet before importing the data." & vbCrLf, vbCritical, "Error " & Err.Number
        Application.DisplayAlerts = False
        ShNew.Delete
        Application.DisplayAlerts = True
      Else
      If Err.Description = "Cannot rename a sheet to the same name as another sheet, a referenced object library or a workbook referenced by Visual Basic." Then
      MsgBox "The Folder does not exists" & vbCrLf & "Please select the valid folder." & vbCrLf, vbCritical, "Error " & Err.Number
 
 
            End If
        End If
    End If
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
thanx for help andrew
but i have one more question

i want to give user another chance if he by-mistake gives wrong path
i want to direct him to inputbox where he gives folder name
and its pretty much at the beginning of the code (line no. 36)
i tried to use goto or resume but m getting syntax error


can you tell me how to do that?

regards
Saurabh
 
Upvote 0
my file name is fixed
only difference is there are different folders eg. run11, run12 and so on.
program accepts the folder name with inout box

Code:
folderName = InputBox("Enter the folder name." & vbCrLf & "(e.g. run12)", "Folder Name")

then program creats a string for import

Code:
listPath = "TEXT;" & excelPath & folderName & "\erg_f_d_filter.lst"

so for the second chance user should again start from folder name
 
Upvote 0
Here's an example:

Code:
Sub Test()
    Const ExcelPath As String = "C:\TEMP\"
    Const FileName As String = "erg_f_d_filter.lst"
    Dim FolderName As String
    Do
        FolderName = InputBox("Enter the folder name." & vbCrLf & "(e.g. run12)", "Folder Name")
'       Cancel pressed
        If FolderName = "" Then Exit Sub
'       Test for existence of file
        If Dir(ExcelPath & FolderName & "\" & FileName) <> "" Then
            Exit Do
        Else
            MsgBox "The file does not exist in that folder"
        End If
    Loop
'   Code to process file
End Sub
 
Upvote 0
Thanx Andrew<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
my little program is finished now in coming days i will submit this to my professor..<o:p></o:p>
<o:p></o:p>
thanx for your valuable suggestions, without which it was not possible to complete..<o:p></o:p>
<o:p></o:p>
i would like to have this apportunity to thank you and other people who helped me.<o:p></o:p>
i donno when in future i will do excel or vba programming again..<o:p></o:p>
as my area of study is automotive engg.
<o:p></o:p>
Hope to see you soon<o:p></o:p>
Kind Regards <o:p></o:p>
Saurabh<o:p></o:p>
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top