?
Solved

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

Posted on 2014-01-31
3
Medium Priority
?
1,468 Views
Last Modified: 2014-02-05
Hi,


Does anyone have any code that could do this?


Thanks
Kieran
0
Comment
Question by:kieranjcollins
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 31

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 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

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 article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
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 …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

765 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