import multiple files into Excel

Johny1900

Board Regular
Joined
Dec 20, 2007
Messages
54
Hi everybody,

I need to import about 1500 HTML file into a DB (SQL Server). But (here is the big problem), I only need to import some data of each HTML file (always the same line/variable).
So I thought using Excel as you can say with part of the HTML file you want to import. It works great.
But I did it with one file. Is there a way you can select a whole directory and it does it for you?

Many thx

Johny
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hello Johny,
The easy answer it yes, it can be done. It's a little difficult to give specific help with such a vague answer, and there are other approaches, but one method that should work is to use the dir() function with a loop, something like:
Code:
Sub GetHtmlFiles()
Dim f As String, Path As String, NextFile As String, ThisBook As String

Path = "C:\excel tests\"  'replace with your directory name here
ChDir Path
f = dir(Path & "*.xls")   'if the file extension is something other than .htm, change this
While f <> ""
    Workbooks.OpenText Filename:=f
    '....whatever code you need to get your data and copy/paste it into an excel workbook
    MsgBox f    'this is just an example of code that could be within your loop
    
    Workbooks(f).Close SaveChanges:=False 'change to true if you want to save to the original html filename
    f = dir()
Wend
Hope this helps,
Cindy
 
Upvote 0
Oops...I didn't notice until too late to edit. The "*.xls" reference should have been "*.htm" for your purposes!
Code:
Sub GetHtmlFiles()
Dim f As String, Path As String, NextFile As String, ThisBook As String

Path = "C:\excel tests\"  'replace with your directory name here
ChDir Path
f = dir(Path & "*.htm")   'if the file extension is something other than .htm, change this
While f <> ""
    Workbooks.OpenText Filename:=f
    '....whatever code you need to get your data and copy/paste it into an excel workbook
    MsgBox f    'this is just an example of code that could be within your loop
    
    Workbooks(f).Close SaveChanges:=False 'change to true if you want to save to the original html filename
    f = dir()
Wend
 
Upvote 0
Thx Cindy,
But as I am a simpel user of Excel I don´t know how I can get that specified line of data of each file.
The files I have are HTM file of a web and each file has an adress which I need to get somehow in a DB table. So by importing a HTM file it asks me which part of the HTM file. Which I select by hand.
As all the HTM file are the same structure, so I thought it would be great if Excell let me select a directory and repeat the accion for each file in it.

Thx anyway
 
Upvote 0
Johny,
I'm sorry...I think I am confused about what you are trying to accomplish.
You said in your original post that your data was in a directory, and you wanted Excel to repeat your actions for each file in the directory. In Excel, macros are the "automation workhorse", so I assumed that you wanted a macro that would automate the identification/opening/closing of each file in a directory...with your actions in between the open and close. Did you want excel to create a list of the files? It could do that too, if that's what you need.
If you just need only one line / variable of data from each of the files, and if it's always identifiable by location or label in the html file, I can help you write a macro that could automate the whole process. Let me know if that would be useful.
Cindy
 
Upvote 0
Hi Cindy,

thxt is correct, I have HTML file and I have converted them to TXT file already, because I am trying by another way (using ASP, which is my specialty), reading each file and getting the data from lines 11,14,16,18,20
Then I put the into an excel file whith something like

NAME ADDRESS CITY POSTALCODE TLF
JOHNY MY street NY 089898 0019898989
nancy Her street .......

I have a DB with all the names of the files, so I read the name, search the file, and get the data (the part I am in now)

And to be honest, Excel isn´t my strongest side, I just know to open, add some formules and that´s it.

So any help is welcome

Johny
 
Upvote 0
Hello Johny,
The following code doesn't depend on the filename list, but could be modified to do that if the approach here doesn't work (this is modified slightly from code I wrote recently, so it was easier to use this approach than starting from scratch). Also, this is a "first draft", in that it doesn't yet split your incoming data into multiple columns. That should be easy to add in...it depends on how your data is delimited or arranged in the incoming data file.
The following steps may sound tedious...I've given lots of detail since you're not very familiar with Excel or Visual Basic. It should be worth it given the number of files you're working with.
To use this, open or create a workbook to use to store the macro (for the time being, not the name of the file that will contain all of the data), then open a visual basic module...
Press Alt-F11 to open the visual basic editor
In the Project window (usually a pane on the far left top), highlight the workbook you're going to use to store the macro
From the VBA Insert menu, choose module
Paste the code below into the new module
Modify the file extension if it's not ".txt"
Change the path assignment to the location of your 1500 files (although for testing you might want to copy just a few to a test directory, and change the path to that location).
Switch back to the workbook / worksheet, select Tools, Macro, Macros, highlight GetHTMLData, and select run. A new workbook will be created that contains lines 11, 14, 16, 18, and 20 from each of the files in the target directory.
Once this part works for you, we can add in the details of the data layout or format (as written this macro will put it all in column A)

Code:
Sub GetHTMLData()
'Assumptions:  file extension is .txt
'
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fs, a, FileToOpen
Dim datarow As Long
Dim fname
Dim ThisBook As String, Target_Sheet As String, nextline As String, ReportDate As String
Dim LineNum As Integer, RecordRow As Integer
Dim AcctCol As Integer, DescCol As Integer, ValuCol As Integer, CurrCol As Integer, DateCol As Integer, InfoCol As Integer
Dim CustCol As Integer, VendCol As Integer, PartCol As Integer, XxxxCol As Integer
Dim OrgCol As Integer, DstCol As Integer, CoCol As Integer, FlagCol As Integer, TypeCol As Integer
Dim DateStart As Integer, PageStart As Integer, PcsCol As Integer, WtCol As Integer

Dim f As String, Path As String

    Workbooks.Add
    ActiveWorkbook.SaveAs Filename:="Summary.xls"  'change if desired
    ThisBook = ActiveWorkbook.Name
    Target_Sheet = ActiveSheet.Name
    datarow = 1

    Path = "C:\excel tests\"  'replace with your directory name here
    ChDir Path
    FileToOpen = dir(Path & "*.txt")   'if the file extension is something other than .htm, change this
    While FileToOpen <> ""
        LineNum = 1
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set a = fs.OpenTextFile(FileToOpen, ForReading, False)
        Application.ScreenUpdating = False
        
        Do While a.AtEndOfStream <> True And LineNum <= 20 'keep going to the end of the file
            
            nextline = a.readline 'get the line from the file
            Select Case LineNum
            Case 11, 14, 16, 18, 20
                With Workbooks(ThisBook).Worksheets(Target_Sheet)
                    datarow = datarow + 1
                    .Cells(datarow, 1) = nextline 'change this to split the data into appropriate cells
                    '.Cells(datarow, 2) = mid(nextline,....)
                End With

            End Select
            LineNum = LineNum + 1
        Loop
        a.Close
        
        'save it
        Workbooks(ThisBook).Save
        Application.ScreenUpdating = True
    
        FileToOpen = dir()
    Wend
End Sub
It's quite late here, so I won't be able to respond until morning my time (not enough hours away at this point), but if this works, let me know how the data on the selected input lines is delimited or structured, and I can modify the middle parts of the code to split it into appropriate cells.
Hope this gets you started in the right direction,
Cindy
 
Upvote 0
FANTASTIC!!!!!!! Cindy!
It works. And It looks a lot at my ASP code.
I have programmed it in ASP, and it works. Still I would prefer to have it in excel, as it will skip a step by converted the data to exel.
It might sound crazy, but you teached me something more, how to open the VBeditor and toi run a Macroscript!
Now the program writes the data one below another. The only thing that I need no is that it copies it right in the rightolumn.

I am going to play with the editor and see if I can get something.

Thx again:) A big hug from me to you!

Regards,

Johny

Oh yes, I will be gone also within 3 hours (it is now 12.30) so I wonñt reply until monday.

Again: THX!!!!!
 
Upvote 0
Hi Cindy,

I have resolved it with the columns. I added a kind of var, that it won´t jumpto the next line until it has line 20 of the data. So I made different caqese because every one is a different column.

Now I get all the data in every column I wanted.

Here is the code

Code:
        data_row_count = 1
        Do While a.AtEndOfStream <> True And LineNum <= 20 'keep going to the end of the file
            nextline = a.readline 'get the line from the file
            If data_row_count = 1 Then
                datarow = datarow + 1
                data_row_count = 0
            End If
            Select Case LineNum
            Case 11
                With Workbooks(ThisBook).Worksheets(Target_Sheet)
                    
                    .Cells(datarow, 1) = nextline 'change this to split the data into appropriate cells
                    '.Cells(datarow, 2) = mid(nextline,....)
                End With
            Case 14
                With Workbooks(ThisBook).Worksheets(Target_Sheet)
                    
                    .Cells(datarow, 2) = nextline 'change this to split the data into appropriate cells
                End With
            Case 16
                With Workbooks(ThisBook).Worksheets(Target_Sheet)
                
                    .Cells(datarow, 3) = nextline 'change this to split the data into appropriate cells
                End With
            Case 18
                With Workbooks(ThisBook).Worksheets(Target_Sheet)
                
                    .Cells(datarow, 4) = nextline 'change this to split the data into appropriate cells
                End With
            Case 20
                With Workbooks(ThisBook).Worksheets(Target_Sheet)
                
                    .Cells(datarow, 5) = nextline 'change this to split the data into appropriate cells
                End With
            End Select
            LineNum = LineNum + 1
            
        Loop
        data_row_count = 1
        a.Close

What I like the most of this thread is that I have learned a lot of what you teached me, and also new cod to use.
Again, :pray: MANY THX :pray: for spending your time and helping me!!!

Johny
 
Upvote 0
Glad to help!
I figured that if you were an SAP expert, you wouldn't have any trouble picking up VBA, but it's always useful to have a running start!
Cindy
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top