MACRO for importing TEXT file from location

Jaydeep_sanghavi

New Member
Joined
Jun 21, 2011
Messages
11
i want a macro which will import the TEXT file from specific(fixed) location irrespective of file name means whatever would be the file name it should import the file from that location


Thanks in Advance
 
Last edited:

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Will you be importing more than one file?

Have you tried just using Excel to browse to the file and open it? This will start the import wizard.
 
Upvote 0
Will you be importing more than one file?

Have you tried just using Excel to browse to the file and open it? This will start the import wizard.

no i will import only one file at a time.

to answer your second question yes i did so many times but want to make it automated using macro
 
Upvote 0
Try this:

Code:
Sub FileImport()
    Dim Fldr As String, FldrArr() As Variant
    Fldr = "Your File Location here"
    FldrArr() = FileCount(Fldr)
    If FldrArr(0) = 1 Then
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;" & Fldr & FldrArr(1) & "", Destination:=Range("$A$5"))
            .Name = FldrArr(1)
            .Refresh BackgroundQuery:=False
        End With
    Else
        MsgBox FldrArr(1)
    End If
End Sub
Private Function FileCount(Folder As String) As Variant
    Dim objFSO As Object
    Dim objFile As Object
    Dim objFolder As Object
    Dim Name As String
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    'Get the folder object associated with the directory
    Set objFolder = objFSO.GetFolder(Folder)
    Select Case objFolder.Files.Count
        Case 0
            FileCount = (Array(objFolder.Files.Count, "No Files were Found in " & Folder))
        Case 1
            For Each objFile In objFolder.Files
            Name = objFile.Name
            Next
            FileCount = (Array(objFolder.Files.Count, Name))
        Case Else
            FileCount = (Array(objFolder.Files.Count, "There is more than one file in " & Folder))
    End Select
    'Clean up!
    Set objFolder = Nothing
    Set objFile = Nothing
    Set objFSO = Nothing
End Function
 
Upvote 0
Have you changed:

Code:
Fldr = "Your File Location here"

To the folder Directory you want to pull the file from?

EG.

Code:
Fldr = "C:/Import Folder/"
 
Upvote 0
It worked thanks a lot thank you very much

just one more thing if i have more than one file in folder then is there any way by which it will ask me which file to import???

Thanx in advance
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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