473,467 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do I Import Multiple Excel Files' Data Using Filters

5 New Member
Hi.

I have thousands of excel files in one directory. I want to build up the Access table from excel files.
Each excel file has one worksheet, but I don't need every column and low of the worksheet.

I am trying to import a FILTERED Excel worksheet of every excel files in that directory into an access table using visual basic code.


I was able to build up (someone helped me) visual basic code to import multiple excel files to one single Access table without FILTERING.

Someone can say that I can just import without filtering and do query in Access.
The problem is that each excel worksheet has too many row and columns that I don't need it, and I need to import thousands of excel files to one access table. Eventually I will face Access table size problem.


I need to improve my visual basic code to import only selected rows and columns that I want to import into Access table.

my worksheet is in the following format

ID Month Year Day OPEN HIGH LOW ...VOL..... SETTLE TradeDate.
OD 7 2011 15 ................... 546 7/15/2011
OD 8 2011 18 ..................658 7/15/2011
..
FD 7 2011 15 .................... 987 7/15/2011
..
..
SM 7 2011 15 .................... 632 7/15/2011
...
KW 7 2011 15 .................... 838 7/15/2011
....

All I need to import is to find "FD" and "SM" in the 1st column (ID) and get the rest of the row (or even better one is to get only ID, Month, Year, Day, Settle, and Tradedate data in that row)

Anyone could help me to filter the data in worksheet and import into Access table?
(I use Access 2007 version)

thank you very much.


FYI, the following is the visual basic code that WORKS for importing multiple excel files to one single Access table without FILTERING.

Expand|Select|Wrap|Line Numbers
  1. Sub Import_multiple_excel_files()
  2.  
  3. Const strPath As String = "C:\Price Data\NYMEX\2011_test" 'Directory Path
  4. Dim strFile As String 'Filename
  5. Dim strFileList() As String 'File Array
  6. Dim intFile As Integer 'File Number
  7.  
  8. 'Loop through the folder & build file list
  9. 'strFile = Dir(strPath & "*.csv")
  10. strFile = Dir(strPath & "*.xlsx")
  11.  
  12. While strFile <> ""
  13. 'add files to the list
  14. intFile = intFile + 1
  15. ReDim Preserve strFileList(1 To intFile)
  16. strFileList(intFile) = strFile
  17. strFile = Dir()
  18. Wend
  19. 'see if any files were found
  20. If intFile = 0 Then
  21. MsgBox "No files found"
  22. Exit Sub
  23. End If
  24. 'cycle through the list of files & import to Access
  25. 'creating a new table called importedLMP_t
  26. For intFile = 1 To UBound(strFileList)
  27. DoCmd.TransferText acImportDelimi, , _
  28. "importedNYMEXinAccess", strPath & strFileList(intFile), True
  29. 'Check out the TransferText/TransferSpreadsheet options in the Access
  30. 'Visual Basic Help file for a full description & list of
  31. 'optional settings
  32. Next
  33. MsgBox UBound(strFileList) & " Files were Imported"
  34. End Sub
Oct 26 '11 #1
13 4377
NeoPa
32,556 Recognized Expert Moderator MVP
Have you considered a linked table that links to an Excel workbook and whose link you change each time a new workbook is required. That way you can copy the data across to your proper table using an APPEND query based on the linked Excel table and filtered to include only "FD" & "SM" type records. You can specify whichever columns you want/need in that scenario too.
Oct 26 '11 #2
jimatqsi
1,271 Recognized Expert Top Contributor
I agree with NeoPa. I think I would assume a constant name for the input Excel file rather than make a new link for every Excel sheet.

To set this up, copy one of your Excel files to file name ExcelFile.xls. Then Create an ODBC link in your .mdb to that file. Then, replace line 27/28 of your code with this:

Expand|Select|Wrap|Line Numbers
  1.     ' copy each new .xls to the common name of ExcelFile.xls
  2.     FileCopy strPath & strFileList(intFile), strPath & "ExcelFile.xls"
  3.     For Each tbl In CurrentDb.TableDefs
  4.         If tbl.Name = "ExcelFile" Then
  5.             tbl.RefreshLink  ' renew the ODBC link
  6.             Exit For
  7.         End If
  8.     Next
Now you can open table ExcelFile with whatever filter you want and append the data to importedNYMEXinAccess or some other table before you loop around to import the next Excel file.

Make sure you exclude ExcelFile.xls from your processing if you are copying it to the same folder as all of your original Excel files.

Jim
Oct 26 '11 #3
jimatqsi
1,271 Recognized Expert Top Contributor
I believe that For/Next loop could be replaced with
Expand|Select|Wrap|Line Numbers
  1. CurrentDb.TableDefs("ExcelFile").RefreshLink
assuming the name of your link (not the name of the excel file) is ExcelFile.

Jim
Oct 26 '11 #4
WU JU
5 New Member
hello Jim and NeoPa

Thank you for the reply.

I have two projects that I need to import multiple excel files with FILTERED Excel spreasheet into an access table.

One I described yesterday is easier one.

For my second project, I don't have good index row in excel format (not like yesterday's example).
Excel files are created without good index format.

I attached my second project's excel format. As you see, there is no 1st row that determine the index things like ID, month, ...
I guess I have to look for string "Jan", "Feb", ...."Dec" from the first column, then grasp the rows that has "Jan", "Feb", ....or "Dec" at the first column. I also have thousands of files in one directory, so I rather automate instead of modify each to get right format for importing.

Do you think even this case I can use link method to do that?

Originally I thought that I create one kind of visual basic to use both case with only minor changes. So finding string like '"FD" or "SM" (for first project), OR "Jan" "Feb",...or "Dec" (for second project), and filter and import to Access is useful method to fulfill both projects.



So if you know how to code in visual basic to filter with string and import OR you think link method works both projects, or you have better idea of any to fulfill both of my projects, then please help me out.

Regards,

WJ



FYI, second project's excel format is below.
As you see the below, it is not well formatted. Someone just built to show many information, but not in built-in data format. (there is no index row to determine Day Ah, Week AH, Bal Month, Nov, Dec, ..) In fact "Forward", "Period" are kind of index for "north" and "south", and "A" "B" "C" "D", but they are in first column with Day Ah, Week AH, Bal Month,Nov, Dec, ...
Worst of all, every file has different orders of months. Some file start with Jan thru Dec, other file start with Nov thru Oct, and so on in 1st column. Also each file has deferment blank between Period and Day Ah.
Plus first couple rows have some information that are not directly useful in data point of view.


Power Price
today 10/18/2011

North South
forward A B C D period onpk offpk onpk offpk onpk offpk onpk offpk


Day Ah 38 41 32 45 43 33 55 46
Week Ah 32 45 43 33 55 46 33 24
Bal month 33 55 46 33 24 43 43 32

Nov-11 38 41 32 45 43 33 55 46
Dec-11 .....
Jan-12 .....
....
Oct 26 '11 #5
NeoPa
32,556 Recognized Expert Moderator MVP
I cannot say that working with such files will work well, but I would imagine that linking them gives you as good a chance of working with them as any other.

How easy it is after that really depends on the format you find in there and how well it is able to be manipulated or recognised.
Oct 27 '11 #6
WU JU
5 New Member
Thanks.
Then I will try to use link method.

** Snip **

Thanks NeoPa and Jim for your help.

WJ
Oct 27 '11 #7
NeoPa
32,556 Recognized Expert Moderator MVP
I've removed the new question from your post as asking new questions is not allowed within an existing thread.

Before posting your new question in a separate thread give consideration to what you may ask and what you may not (There are prominent threads available to guide you on this). We cannot allow simple questions wher you provide the spec for a job. You need to ask specific questions to help you with the work you're doing.
Oct 27 '11 #8
WU JU
5 New Member
Hello Jim,

I tried your code to create an ODBC link in my .accdb to ExcelFile.xlsx file. I was in "Select Data Source" step, then chose "File Datra Source", then "Look in" to the dircetory that all the excel files exist, then there is no furture step that I could go. Do I need to choose Machine Data Sourse instead? or how do I link my .mdb to ExcelFile.xlsx file?

My access is in .accdb not .mdb, is it going to be problem?

I appreciate your help.

WJ
Oct 27 '11 #9
NeoPa
32,556 Recognized Expert Moderator MVP
JimatQSI:
Then Create an ODBC link in your .mdb to that file.
Jim's code doesn't create the link for you. It assumes you already have one created (manually) as instructed in the post.

The code ensures that the newly copied file is enabled via the link.

NB. Don't forget he suggested replacement code in a later post which is far more efficent and neat than the original. You should be using that version rather than the original.
Oct 27 '11 #10
WU JU
5 New Member
Hello NeoPa,

I mean I tried create an ODBC link in my .accdb to ExcelFile.xlsx file before I use Jim's code.
I went to "External Data" my Access toolbar, clicked ODBC Database. I was in "Select Data Source" step, then chose "File Datra Source", then "Look in" to the dircetory that all the excel files exist, then there is no furture step that I could go. I also clicked to Machine Data Sourse instead, but no clue I have. So could you please show me how to link my .accdb to ExcelFile.xlsx file? I am pretty new to Access and VB, so I need more detail information.

WJ
Oct 27 '11 #11
NeoPa
32,556 Recognized Expert Moderator MVP
If I say you need to be linking to a Microsoft Excel file (*.xls, *.xlsx) instead of an ODBC Databases file () then I expect you can take it from there quite easily.
Oct 27 '11 #12
jimatqsi
1,271 Recognized Expert Top Contributor
Wu Ju,
I apologize, I made a mistake by referencing an ODBC connection. If you choose the Excel file type in the first step then just browse to the file you want. It's a little more simple than ODBC connections.

Jim
Oct 28 '11 #13
NeoPa
32,556 Recognized Expert Moderator MVP
LoL Jim.

I didn't even notice that when I read your post. I should have picked up on it earlier. I suppose thinking of things without actually having it in front of you does allow for some mistakes to creep in.
Oct 28 '11 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jen | last post by:
Trying to take one table in access and split it into multiple excel files(using an excel template); and then email based on email addresses in Table2; Of course, I would like to do all of this...
4
by: jayfeb29 | last post by:
Hi , Any one can guide me in Import the Excel 2002 data into sql server 2000 using ado.net1.1. I have searched all the forums most of the solutions end with a line of suggestion not with any code...
1
rcollins
by: rcollins | last post by:
So I did the searching for someone to already have the answer. I had modified this code for My spreadsheets and database. I have marked the three lines in the code that are bad. Am I missing...
11
by: MD | last post by:
Hello, I need to import a sheet of 884 different excel-file with same lay- out. The sheet name is 'Totaal' and is the same in all different files. Is there a script (module) in order to: 1....
3
by: theodorej | last post by:
......how do I select multiple Excel files to import into Access? Any insights would be deeply appreciated.
28
by: kkadakia | last post by:
I get a daily excel file for a entire month which I want to transfer into Access at the end of the month. So, there are around 20-25 excel files I get by the end of the month, and I would like to...
2
by: ciaran.hudson | last post by:
Hi I have multiple excel files of the same format in a directory. They are called book1.xls, book2.xls, book3.xls and so on. What is the easiest way to import the tab named sheet1 from each of...
7
by: ivancycheng | last post by:
I have around 400 excel files with same format (multiple sheets) and wants to import few sheets from excel to MS Access (XP version) for further processing. any one can advise me how to do it? (I'm...
0
by: zizi2 | last post by:
Hi, how do I output multiple excel files from one source using vbscript? Regards, Noluthando
0
by: rhonda6373 | last post by:
Is there a way to use the TransferSpreadsheet to import multiple Excel workbooks from a single directory in VBA at one time?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.