Solved

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

Posted on 2014-01-31
3
1,212 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

831 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