Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Importing txt into XL as Delimited, what's wrong w my code?

Status
Not open for further replies.

sisieko

Programmer
Jun 13, 2004
56
US
I am using this code to import text file into excel.
Can someone tell me what's wrong with my code? Why do i keep getting my customized msgbox error.

Code:
Private Sub wkscmd_ImportData_Click()

'   Local Variables
    Dim strPath As String, strFile As String
    Dim rngRaw As Range

'   Get Data file parameters
    strPath = Me.Range("C1")
    strFile = Me.Range("C2")

'   Verify that table is empty
    Me.Range("rdi_TableTop", Me.Range("rdi_TableTop").End(xlDown)).EntireRow.ClearContents

'   Open and import datafile
    ' Open file
    Application.DisplayAlerts = False
    On Error Resume Next
    Workbooks.OpenText FileName:=strPath & strFile, Origin:=xlWindows, StartRow:=1, DataType:=xlDelimited, _
                       TextQualifier:=xlTextQualifierDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, Semicolon:=False, _
                       Comma:=False, Space:=False, Other:="*", FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
    Application.DisplayAlerts = True
On Error GoTo Err_BadFileName
    ' Import raw file
    Set rngRaw = Workbooks(strFile).ActiveSheet.Range("A1", Workbooks(strFile).ActiveSheet.Range("A1").End(xlDown))
    Me.Range("rdi_TableTop").Resize(rngRaw.Rows.Count, 1) = rngRaw.Value
    Workbooks(strFile).Close False
Exit_BadFileName:
    Exit Sub
Err_BadFileName:
    MsgBox "Please enter the correct FilePath in CELL C1, e.g. C:\myfolder\  " _
    & Chr(13) & "Be sure to have a slash behind your Filepath as shown above" _
    & Chr(13) & "And enter the correct FileName in CELL C2, e.g. rawdata.txt", vbOKOnly + vbCritical, "Bad Filepath OR FileName"
    Resume Exit_BadFileName

End Sub

This looks like the problematic part of my code: When i stepinto (F8) it.
Code:
On Error GoTo Err_BadFileName
    ' Import raw file
    Set rngRaw = Workbooks(strFile).ActiveSheet.Range("A1", Workbooks(strFile).ActiveSheet.Range("A1").End(xlDown))
    Me.Range("rdi_TableTop").Resize(rngRaw.Rows.Count, 1) = rngRaw.Value
    Workbooks(strFile).Close False

PLEASE HEEELP??

 
Which error occurs when you disable your error handler ?
Have you tried to replace this:
Set rngRaw = Workbooks(strFile).ActiveSheet.Range("A1", Workbooks(strFile).ActiveSheet.Range("A1").End(xlDown))
with this ?
With Workbooks(strFile).ActiveSheet
Set rngRaw = .Range("A1", .Range("A1").End(xlDown))
End With

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
HI PHV, When i disable my error handler it just doesn't do anything. Doesn't import, don't give an error :(

 
I recorded a new macro "inside the same workbook" i have been working with. And copied and inserted the macro code into my code. And it works like charm. Thanks for your time

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top