Solved

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Formula returning #N/A 9 33
Excel formula to append date to end of url 6 29
Delete row if does not start with 0 43 38
Excel Pivot Table question 5 15
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

733 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