Link to home
Start Free TrialLog in
Avatar of jay_waugh
jay_waughFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Copy data from Excel into MSAccess

I am trying to open a spreadsheet from MSAccess using VBA. I then want to select some cells on the spreadsheet to copy the data into an access table. At the moment I can't even get the code to select the cells on the spreadsheet to work. Can you help please?

Private Sub opwb()

Dim sPath As String
Dim sFile As String
Dim WorkbookToRead As String
Dim excelApp As Excel.Application
Dim wb As Excel.Workbook
Dim Flag As String
Dim strPath As String
Dim strAppFPSS As String
Dim strAppId As String
Dim fpHostnames As Variant
Dim strWorkSheetName As String
Dim hn As Range


strPath = "C:\temp\"
strAppFPSS = "Doc1"
strAppId = Mid(strAppFPSS, 1, InStr(1, strAppFPSS, " ") - 1)
strWorkBook = strPath & strAppFPSS & ".xlsx"
strWorkSheetName = "payments"
   
On Error Resume Next

Set excelApp = GetObject(, "Excel.Application")  'http://support.microsoft.com/kb/288902 - If Excel is not open, an error occurs and Err.Number > 0
                Flag = 1
If Err.Number <> 0 Then Set excelApp = CreateObject("Excel.Application") 'There is an error if Err.Number <> 0     Create Excel.Application

On Error GoTo Err_Handler
                Flag = 2

    With excelApp
        .Visible = True  'Excel becomes visible. Confirmed.
        Set wb = .Workbooks.Open(strWorkBook, ReadOnly:=True)
        wb.Sheets(8).Sheets(strWorkSheetName).Range("A2").Select
        Sheets(strWorkSheetName).Range(Selection, Selection.End(xlDown)).Select
               
    End With
             
               
    Set wb = Nothing
    Set excelApp = Nothing

Exit_Sub:
    Exit Sub

Err_Handler:
    Set wb = Nothing
    Set excelApp = Nothing
    MsgBox Err.Description
    MsgBox "An error occurred while in Private Sub OpenExcelWorkbook(), called from cmdOpenExcelWorkbook_Click() | Flag = " & Flag
    Resume Exit_Sub

End Sub
SOLUTION
Avatar of Ian Sturland
Ian Sturland

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jay_waugh

ASKER

Excel sheet arrange in columns with headers. I am looking to get all the data under a single header, but without the header.
Avatar of Ian Sturland
Ian Sturland

ok you can do that i would suggest firstly creating a link table in access to your excel worksheet providing the layout remains constant, then you can use union queries to pull the data unto one column and append to another table

A simpler option if i understand it right would be to do as i suggested create a simple link table first, then use a select query to concatenate your columns into one using the & separator
Avatar of Gustav Brock
The easiest method is probably to open Excel, assign a Named Range to column A, save the workbook.

Then, in Access, link that Named Range as a linked table.
Finally, create a query that uses the linked table as source and filters on records where IsNumeric(F1)=True and assigns a meaningful alias to that field.

Now, use this query for your import or further processing.
I have to loop around about 400 spreadsheets so need to do this from MSAccess
No need? Of course not, many methods can be used, but you wrote:

I am trying to open a spreadsheet from MSAccess using VBA.

So what are you actually trying to do, please?
Thanks Gustav you have a fair point :)

What I am trying to do is:-

I have 400 spreadsheets. I want to import the first column of each spreadsheet into a single msaccess table. I have the name and location of each spreadsheet in a single list. This table into which I import the spreadhseet data needs to have two columns. One which contains a concatenated list of all the values in column a of each spreadsheet. The other column needs to contain the first 7 characters of each Excel file's name (this data is not available in the spreasheet itself.)

The header value in column a of each spreadsheet contains [ and : characters so I cannot get the DOCMD.transferspreadsheet to work
Could you provide a sample of such worksheet?
Does each worksheet belong to one or to separated workbooks?
Sadly I can't upload from where I am.

Yes each workbook belongs to seperate workbooks.

Sales1.xlsx
Sales2.xlsx
Sales3.xlsx
Sales4.xlsx
Sales5.xlsx
Sales6.xlsx

The spreadsheets are consistent in that worksheet 8 is called "confirmed purchases" and this is where I need to get the data from column A.

The header is [customer]:name and there could be any number of rows in this column.
The previous should have read:-

Sadly I can't upload from where I am.

 Yes each worksheet belongs to seperate workbooks.

 Sales1.xlsx
 Sales2.xlsx
 Sales3.xlsx
 Sales4.xlsx
 Sales5.xlsx
 Sales6.xlsx

 The spreadsheets are consistent in that worksheet 8 is called "confirmed purchases" and this is where I need to get the data from column A.

 The header is [customer]:name and there could be any number of rows in this column.
Using TransferSpreadsheet, just link to the spreadsheet.  You can then use an append query to select only the first column and append it to a local table.

We have bits and pieces of what you need to do but not a complete understanding.  Are you comfortable with VBA but just need ideas on how to attack the problem?  Are you looking for code that reads the files of a directory and opens them one at a time?  What do you need to do after you import that single column?  Do you need to move the processed files to a completed folder?  If not, how will you tell which files you have imported and which you haven't?  Would keeping a log table of imported files solve that problem so that you can bypass files that you have already processed.
Hi Pat,

Some code would be very useful if possible.

Tracking what's loaded into the table is not essential as I can quickly compare what is in the consolidated table against my original list of documents by using the column I mentioned which contains the first characters of the imported spreadsheets name.

Many thanks
Some code would be very useful if possible.
I have lots of useful code.  Exactly what do you need to do?  Originally, you said you needed to get specific cells.  That requires OLE automation code for Excel and someone posted some.  Later you said you wanted to import multiple tables but only the first column.  Have you tried what I suggested?  Use TransferSpreadsheet to link to the spreadsheet and an append query to copy the specific column from the linked table to your permanent table.

I'm not offering to write custom code for you but I have lots of working code that might be useful if only I knew the details of what you want to do.
Hi
I have moved forward with this but still can't quite get it to work. I can now upload my attempted code and would appreciate some assistance in finishing it off as I am still getting errors when importing and processing my data (simple as it maybe).

So my requirement is this.

I have a list or order numbers (circa400). Each order has an associated spreadsheet. One of the worksheets on each spreadsheet is a list of computers. I want to create a single list of Order numbers and associated computer hostnames.
When I try and import the spreadsheets into MSAccess I am having trouble as the header row in the original spreadsheets contains invalid characters such as [ and :. Hopefully the uploaded database and sample spreadsheets gives a good understanding of what I am trying.

Thanks Again


Jay
AL00016.xlsx
AL00015.xlsx
FPDB.accdb
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial