Solved

Import all worksheets in an excel file then distinct count particular column and append to a table

Posted on 2007-11-20
3
475 Views
Last Modified: 2013-11-27
Hi All,

Purpose: To retreive monthly the total headcount for UK permanent & temp employees only, broken down into Building 1 and Building 2.

I have a headcount spreadsheet that holds the following details in separate worksheets:
permanent employees (perm_emp) - only need three columns to perform calculations "Employee ID", "Building", "Country" (There are 26 columns)
temp employees (temp_emp) - "Employee ID", "Building", "Country"

So far can import the worksheets with the attached code that I have reused from someone elses post (thanks!).

Now I need to:
1) Perform a distinct count on the Employee ID, only where the Employee is from the UK and either Building A or B (A&B are only in the UK).  This is for both Permanents and Temps.
2) Append this figure as 'Total Headcount Building 1' and ''Total Headcount Building 1' to a Results table as the previous months results.  Currently have 3 months data that has been manually entered working on automating all data going in to this database.  So the logic is you would be importing data on the 1st working day of the month for the previous months data.  Is there a way of checking what the last months entry was and then automatically date stamping the data being imported?
e.g                      Date_Created    Total Headcount Building 1   Total Headcount Building 2
                           Jan-07                            1000                                     500
Last Import== >   Feb-07                           1012                                     497
New Import
Auto Date ==>     Mar-07                           1015                                     515
   

3) Deleting the tables Headcount_Import_perm_emp & Headcount_temp_emp.

After any tips, pointers, code for 1 - 3 above.  Am I going about this the right way? (I'm not asking for someone to write the code for me, just pointers to any examples is fine for each section)




Function getXlFile()
Dim objXL As Object, xlSht As Object
Dim strWSname As String
Dim i As Integer, j As Integer
Dim sFile, sTable
    sFile = "\\anetworkshare\test\test.xls"
sTable = "Headcount_Import"
 
Set objXL = CreateObject("Excel.Application")
    objXL.Workbooks.Open sFile, , True
    With objXL
            For i = 1 To .worksheets.Count
                strWSname = .worksheets(i).Name
                Set xlSht = .activeworkbook.worksheets(i)
                'Get header names
                    For j = 1 To xlSht.usedrange.Columns.Count
                       ' Test msg box
                       ' MsgBox xlSht.cells(1, j).Value
 
                    Next
 
            DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
                    "" & sTable & "_" & strWSname & "", sFile, True, "" & strWSname & "!"
            Next
            
    End With
    objXL.Quit
    Set objXL = Nothing
 
End Function

Open in new window

0
Comment
Question by:Surferprime
  • 2
3 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 20321414
create a sub to create the append query  based on the imported worksheet


sub AppendData(strTable as string)
dim sql as string
'build the query
sql="insert into NameOftable(field1,field2... )"
sql=sql & " select distinct  <what ever > 
sql=sql & " from ["& strTable &"]"

currentdb.execute sql

end sub





           DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
                    "" & sTable & "_" & strWSname & "", sFile, True, "" & strWSname & "!"
'insert here to call the sub
                     
              call AppendData(stable & "_" & strWSName)

'delete the table
             currentdb.execute "drop table ["& sTable & "_" & strWSname &"]"

Next
0
 

Author Comment

by:Surferprime
ID: 20326700
Hi, thanks for the post, however I'm not sure on a couple of things?
How will this perform a distinct count of the number of employees for both Temps and Perms then add them together?
Also  Is there a way of checking what the last months entry was and then automatically date stamping the data being imported?

Many thanks!
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 20326926
<How will this perform a distinct count of the number of employees for both Temps and Perms then add them together?> you have to build the query

<Also  Is there a way of checking what the last months entry was and then automatically date stamping the data being imported?>
you can use dmax("datefield","nameoftable","datefield< date")
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now