Solved

Excel VBA - Combining text files, adding file name to a column

Posted on 2014-01-31
3
1,133 Views
Last Modified: 2014-02-05
Hi,


Does anyone have any code that could do this?


Thanks
Kieran
0
Comment
Question by:kieranjcollins
3 Comments
 
LVL 29

Expert Comment

by:gowflow
ID: 39823798
yes sure can you be more specific and post a workbook ?
to add the current file name of an open workbook to Cell A1 you do this:

Range("A1") = Activeworkbook.FullName

gowflow
0
 
LVL 35

Accepted Solution

by:
mvidas earned 500 total points
ID: 39824119
Hi Kieran,

Is this what you're looking for? It will prompt you to select the file(s) you want to import, ask you if there is a delimiter, then import them with the file path/name in column A and each line starting in column B.
Sub KieranImportFiles()
 Dim vFF As Long, vFiles As Variant, vFile As Variant, FileCont(), filecnt As Long, i As Long
 Dim vDelim As String, vRow As Long
 
 vFiles = Application.GetOpenFilename("Text Files,*.txt;*.csv,All Files,*.*", MultiSelect:=True)
 If LCase(vFiles(1)) = "false" Then Exit Sub 'hit cancel
 vDelim = InputBox("What is the file delimiter? Enter blank for fixed width.", "Enter delimiter", ",")
 
 filecnt = 0
 ReDim FileCont(1, filecnt)
 For Each vFile In vFiles
  vFF = FreeFile
  Open vFile For Input As #vFF
  Do Until EOF(vFF)
   Line Input #vFF, TempStr
   ReDim Preserve FileCont(1, filecnt)
   FileCont(0, filecnt) = vFile
   FileCont(1, filecnt) = Split(TempStr, vDelim)
   filecnt = filecnt + 1
  Loop
  Close #vFF
 Next
 filecnt = filecnt - 1
 
 Application.ScreenUpdating = False
 Workbooks.Add 1
 vRow = 1
 For i = 0 To filecnt
  Cells(vRow, 1) = FileCont(0, i)
  Range(Cells(vRow, 2), Cells(vRow, UBound(FileCont(1, i)) + 2)) = FileCont(1, i)
  vRow = vRow + 1
 Next 'i
 Application.ScreenUpdating = True
End Sub

Open in new window

If not, please give an example of what you're looking to do, or describe it a little better.

Matt
0
 

Author Closing Comment

by:kieranjcollins
ID: 39835687
That code worked a treat, thank you
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now