Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Reply
Old 09-24-2009, 02:12 PM   #1 (permalink)
 
Newb Techie

Join Date: Sep 2009

Posts: 1

katto01 is on a distinguished road

Default excel VBA open file

Hello

what I am tryng to do is import in excel a number of files (a file per sheet).
I would like to be able to specify for the open file command the "OtherChar" option, say "|"
The code below works fine except for this option (see red lines below) which I was not able to figure it out.

Please help

Katto01
Code:
Sub ImportAllFilesInDirectory()
    Dim SelectedItem As Variant
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    
    Dim strPath As String, ext As String
    Dim strFile As String
    fd.AllowMultiSelect = False
   
    
    If fd.Show = False Then Exit Sub
    strPath = fd.SelectedItems(1)
    mePath = FunctionGetFilepath(strPath)
    strFile = FunctionGetFileName(strPath)
    ext = FunctionGetFileExt(strPath)
    
    strFile = UCase(Dir(mePath & "*" & ext))
    Do While strFile <> ""
        With ActiveWorkbook.Worksheets.Add
            With .QueryTables.Add(Connection:="TEXT;" & strPath, _
                Destination:=.Range("A1"))
                .Parent.Name = Replace(strFile, ext, "")
                .TextFileParseType = xlDelimited
                .TextFileTextQualifier = xlTextQualifierDoubleQuote
                .TextFileConsecutiveDelimiter = False
                .TextFileTabDelimiter = False
                .TextFileSemicolonDelimiter = False
                .TextFileCommaDelimiter = True
                .TextFileSpaceDelimiter = False
                .TextFileColumnDataTypes = Array(1)
                .TextFileTrailingMinusNumbers = True
                .Refresh BackgroundQuery:=False
            End With
        End With
        strFile = Dir
     Loop
End Sub

Sub ImportSelectedFiles()
'Program will ask for how many files to import.
'You will be prompted to browse to and select files to import.
    Dim strPath As String
    Dim strFile As String
    Dim N As Integer, L As Integer
    Dim Dupe As String
    Dim SelectedItem As Variant
    Dim fd As FileDialog

    
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    
    N = Application.InputBox _
    (Prompt:="Enter Number of Files To Import ? ", _
        Default:=1, Type:=1)
   
        
  For L = 1 To N
Rechoose:
       If fd.Show = False Then Exit Sub
       strPath = fd.SelectedItems(1)
       strFile = FunctionGetFileName(strPath)
       ext = FunctionGetFileExt(strPath)
       If InStr(1, Dupe, strPath) Then
        MsgBox "Duplicate File Name. Reselect a file"
        GoTo Rechoose
       End If
       Dupe = Dupe & strPath
       T$ = "S" + LTrim(RTrim(Chr$(L + 48)))
       S$ = Replace(strFile, ext, T$)
       S$ = Mid(S$, 1, 28) + LTrim(RTrim(Chr$(L + 48)))
        With ActiveWorkbook.Worksheets.Add
            With .QueryTables.Add(Connection:="TEXT;" & strPath, _
                Destination:=.Range("A1"))
                .Parent.Name = S$
                .TextFileParseType = xlDelimited
                .TextFileTextQualifier = xlTextQualifierDoubleQuote
                .TextFileConsecutiveDelimiter = True
                .TextFileTabDelimiter = True
                .TextFileSemicolonDelimiter = False
                .TextFileCommaDelimiter = True
                .TextFileSpaceDelimiter = False
                .TextFileColumnDataTypes = Array(1)
                .TextFileTrailingMinusNumbers = True
                '.TextFileOther = True
                '.TextFileOtherChar = "|"
                .Refresh BackgroundQuery:=True
            End With
        End With
   Next L

End Sub
Function FunctionGetFileName(FullPath As String)
    Dim StrFind As String
    Do Until Left(StrFind, 1) = "\"
        iCount = iCount + 1
        StrFind = Right(FullPath, iCount)
        If iCount = Len(FullPath) Then Exit Do
    Loop
    FunctionGetFileName = Right(StrFind, Len(StrFind) - 1)
End Function
Function FunctionGetFileExt(FullPath As String)
    Dim StrFind As String
    Do Until Left(StrFind, 1) = "."
        iCount = iCount + 1
        StrFind = Right(FullPath, iCount)
        If iCount = Len(FullPath) Then Exit Do
    Loop
    FunctionGetFileExt = Right(StrFind, Len(StrFind))
End Function
Function FunctionGetFilepath(FullPath As String)
    Dim StrFind As String
    Do Until Left(StrFind, 1) = "\"
        iCount = iCount + 1
        StrFind = Right(FullPath, iCount)
        If iCount = Len(FullPath) Then Exit Do
    Loop
    FunctionGetFilepath = Mid(FullPath, 1, Len(FullPath) - Len(StrFind) + 1)
End Function


Last edited by Mak213; 09-24-2009 at 10:41 PM. Reason: added code tags
katto01 is offline   Reply With Quote
Old 09-24-2009, 03:39 PM   #2 (permalink)
 

Join Date: Jul 2005

Location: England

Posts: 2,159

kmote has a spectacular aura aboutkmote has a spectacular aura about

Default Re: excel VBA open file

I'm no VB expert but I can't find either of those properties. Closest: TextFileOtherDelimiter Property*[Excel 2007 Developer Reference]
__________________
MSI P43 Neo|Enermax Pro82+ 425W|E5200|silent 8500GT|250GB Samsung spinpoint F1|Samsung SATA DVD RW|4GB Corsair|Antec SOLO|openSUSE11


There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page
kmote is online now   Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hijackthis log Jamez HijackThis Logs (finished) 17 09-23-2009 09:20 PM
Just checking, analyze please kazeryu209 HijackThis Logs (finished) 7 01-06-2009 07:58 PM
Excel error: Hyperlink unable to open. No program is registered to open this file LightingBird Windows Operating Systems and Software 2 11-21-2008 05:52 PM
Explorer.exe CRASHES INSTANTLY =( [F] wootwoot HijackThis Logs (finished) 28 07-15-2008 02:47 PM
My home PC hijackthis log BrendanGrady HijackThis Logs (finished) 19 02-04-2008 11:15 PM