+ Reply to Thread
Results 1 to 7 of 7

Loading Specific File

  1. #1
    Registered User
    Join Date
    10-18-2006
    Posts
    12

    Loading Specific File

    Hi,

    I would like to ask if anyone knows how I can make a makro that reads a text format data value from a cell ( example: B2; B2 = FILE001 ) and uses this info in next line statement:
    Workbooks.OpenText Filename:= _
    "C:\Documents and Settings\elektra2\My Documents\ALAN\FILE001.FLW", Origin:= _

    The apendix *.FLW, *.TXT, *.XLS should be fixed for each macro.

    For detailed info you can mail me at mojster29@email.si

    bye

  2. #2
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628
    myPath = "C:\Documents and Settings\elektra2\My Documents\ALAN\"

    For Each myCell In Range("b2:b2")
    myFilename = myCell.Value

    fileToOpen = (Path & myFilename & ".*")
    Do While fileToOpen <> ""
    If LCase(fileToOpen) Like "*.flw" _
    Or LCase(fileToOpen) Like "*.txt" _
    Or LCase(fileToOpen) Like "*.xls" Then

    'code to open filòe
    fullNameToOpen = myPath & fileToOpen

    'jere insert your code
    Workbooks.OpenText Filename:= _
    fullNameToOpen, Origin:= _

    fileToOpen = Dir
    Loop
    Next

  3. #3
    Registered User
    Join Date
    10-18-2006
    Posts
    12

    Thank But...

    thanks this looks promising but i don`t know which form should it have:

    1) your program inside makro

    Sub Makro2()
    '
    ' Makro2 Makro
    ' Makro ustvaril: elektra2, dne 18.10.2006
    '

    '
    myPath = "C:\Documents and Settings\elektra2\My Documents\ALAN\"

    For Each myCell In Range("b2:b2")
    myFilename = myCell.Value

    fileToOpen = (Path & myFilename & ".*")
    Do While fileToOpen <> ""
    If LCase(fileToOpen) Like "*.flw" _
    Or LCase(fileToOpen) Like "*.txt" _
    Or LCase(fileToOpen) Like "*.xls" Then

    'code to open file
    fullNameToOpen = myPath & fileToOpen

    'here insert your code
    Workbooks.OpenText Filename:= _
    fullNameToOpen, Origin:= _

    fileToOpen = Dir
    Loop
    Next

    End Sub



    2) or

    myPath = "C:\Documents and Settings\elektra2\My Documents\ALAN\"

    For Each myCell In Range("b2:b2")
    myFilename = myCell.Value

    fileToOpen = (Path & myFilename & ".*")
    Do While fileToOpen <> ""
    If LCase(fileToOpen) Like "*.flw" _
    Or LCase(fileToOpen) Like "*.txt" _
    Or LCase(fileToOpen) Like "*.xls" Then

    'code to open file
    fullNameToOpen = myPath & fileToOpen

    'here insert your code

    Sub Makro2()
    '
    ' Makro2 Makro
    ' Makro ustvaril: elektra2, dne 18.10.2006
    '

    '

    Workbooks.OpenText Filename:= _
    fullNameToOpen, Origin:= _


    End Sub

    fileToOpen = Dir
    Loop
    Next


    and how does this program know in which file and sheet the program looks for file name in cell B2.

    thank`s in advance
    Describe a bit with words please.

  4. #4
    Registered User
    Join Date
    10-18-2006
    Posts
    12

    Thank But...

    I have filed in as folloving:

    Sub Makro2()
    '
    ' Makro2 Makro
    ' Makro ustvaril: elektra2, dne 18.10.2006
    '

    '
    myPath = "C:\Documents and Settings\elektra2\My Documents\ALAN\"

    For Each myCell In Range("b2:b2")
    myFilename = myCell.Value

    fileToOpen = (Path & myFilename & ".*")
    Do While fileToOpen <> ""
    If LCase(fileToOpen) Like "*.flw" _
    Or LCase(fileToOpen) Like "*.txt" _
    Or LCase(fileToOpen) Like "*.xls" Then

    'code to open file
    fullNameToOpen = myPath & fileToOpen

    'here insert your code
    Workbooks.OpenText Filename:= _
    "C:\Documents and Settings\elektra2\My Documents\ALAN\FulNameToOpen", Origin:= _
    852, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), _
    Array(8, 1), Array(17, 1), Array(25, 1), Array(32, 1)), DecimalSeparator:=".", _
    fileToOpen = Dir
    Loop
    Next

    End Sub

  5. #5
    Registered User
    Join Date
    10-18-2006
    Posts
    12
    I got it this far:

    Sub Makro2()
    '
    ' Makro2 Makro
    ' Makro ustvaril: elektra2, dne 18.10.2006
    '

    '
    myPath = "C:\Documents and Settings\elektra2\My Documents\ALAN\"

    For Each myCell In Range("b2:b2")
    myFilename = myCell.Value

    fileToOpen = (Path & myFilename & ".*")
    Do While fileToOpen <> ""
    If LCase(fileToOpen) Like "*.flw" _
    Or LCase(fileToOpen) Like "*.txt" _
    Or LCase(fileToOpen) Like "*.xls" Then

    'code to open file
    FullNameToOpen = myPath & fileToOpen

    'here insert your code
    Workbooks.OpenText Filename:= _
    FullNameToOpen, Origin:= _
    852, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), _
    Array(8, 1), Array(17, 1), Array(25, 1), Array(32, 1)), DecimalSeparator:=".", _
    TrailingMinusNumbers:=True

    fileToOpen = Dir
    Loop
    Next

    End Sub

    when I run this makro program displays Compile error:

    Loop without Do

    WHAT TO DO??

  6. #6
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628
    I'm sorry but I didn't remember to put 'end if' before the row with 'loop' statement.

    The right code is:
    myPath = "C:\Documents and Settings\elektra2\My Documents\ALAN\"

    For Each myCell In Range("b2:b2")
    myFilename = myCell.Value

    fileToOpen = (Path & myFilename & ".*")
    Do While fileToOpen <> ""
    If LCase(fileToOpen) Like "*.flw" _
    Or LCase(fileToOpen) Like "*.txt" _
    Or LCase(fileToOpen) Like "*.xls" Then

    'code to open file
    FullNameToOpen = myPath & fileToOpen

    'here insert your code
    Workbooks.OpenText Filename:= _
    FullNameToOpen, Origin:= _
    852, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), _
    Array(8, 1), Array(17, 1), Array(25, 1), Array(32, 1)), DecimalSeparator:=".", _
    TrailingMinusNumbers:=True

    fileToOpen = Dir
    End If

    Loop
    Next

    Regards,
    Antonio

  7. #7
    Registered User
    Join Date
    10-18-2006
    Posts
    12

    Thanks

    Thank you for help

    I simplified subroutine but it does what it has to. Here`s the code for others to se.

    Sub PRETVORBA()
    '
    ' Makro2 Makro
    ' Makro ustvaril: elektra2, dne 18.10.2006
    '

    '
    myPath = "C:\Documents and Settings\elektra2\My Documents\ALAN\"

    For Each myCell In Range("b1:b1")
    myFilename = myCell.Value

    fileToOpen1 = (myFilename & ".flw")
    fileToOpen2 = (myFilename & ".occ")


    FullNameToOpen1 = myPath & fileToOpen1
    FullNameToOpen2 = myPath & fileToOpen2


    Workbooks.OpenText Filename:= _
    FullNameToOpen1, Origin:= _
    852, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), _
    Array(8, 1), Array(17, 1), Array(25, 1), Array(32, 1)), DecimalSeparator:=".", _
    TrailingMinusNumbers:=True

    Next
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1