• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA - Loading text file without getfileopen and without giving full file path

siva1

New Member
Hi,
I am trying to load a text file in Execl VB (which is there in the same excel location). I don't want to mention the full file path and I don't want to use GetOpenFilename (browse option). Since the excel and text file in samelocation , I would like to try other possibilities.
Could you please share ideas.
 

siva1

Where do You try to load a text file?
... You didn't specific eg to sheet?
Here one link with many variations:
 

siva1

Where do You try to load a text file?
... You didn't specific eg to sheet?
Here one link with many variations:
Hi Vletm.
I have gone through the shared link. I don't want to declare text file path. I am writing VB in excel. This excel and text file are in the same folder. So is this any chance to access from vb without mentioning the text file path. I will share this text file and excel sheet to my colleague. If I am declaring file path , then others need to modify the input file location. To avoid this am looking for other alternate ideas. TIA
 
This excel and text file are in the same folder.
So is this any chance to access from vb without mentioning the text file path.
Hello, just using the workbook Path property, obviously … Like you can check within VBA help.​
 
@Marc L
I am new to VB and learning.
I am using below steps.

>>> use code - tags <<<
Code:
Dim folderpath as String
Dim txtfile as String

Folderpath = This workbook.path & "\"
'Getting excel path
Txtfile = folderpath & "\input.txt"
'Assigning excel path as input file text path
Is this the correct procedure
 
Last edited by a moderator:
Try :

Code:
Sub AddTextFileToSpreadsheet()

Dim FilePath As String
Dim TxtLine As String
Dim StartCell As Range
Dim x As Long

'Text File Path
  FilePath = ThisWorkbook.Path & "\Excel.txt"

'Determine first cell you want data in
  Set StartCell = ActiveSheet.Range("A1")

'Open the text file in a Read State
  Open FilePath For Input As FreeFile
 
'Loop through each row in Text File and Write to Spreadsheet
  Do Until EOF(1)
    Line Input #1, TxtLine
    StartCell.Offset(x) = TxtLine
    x = x + 1
  Loop
    
'Close Text File
  Close FreeFile
 
End Sub
 
Back
Top