Results 1 to 2 of 2
  1. #1
    Bob Blooms is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Aug 2012
    Location
    FL and New England
    Posts
    19

    Move query data to a new table using VBA

    My opbjective is to eliminate an operation that is currently performed in Excel and move it to Access. I have an Access table that includes Employee Name, Account, Percentage. I have an Access query that calculates the cost of each employee. Using this data an other querty prorates the employee costs to each account. I need to get this data consisting of 195 employee and some 13 fields into three fields: Account, Cost Object, i.e., wage, fica, and a data field Cost. Currently I run the cost allocation query, export the data to Excel, run the marco which I have attached, then import it back to Access so that I can query it against actual costs for analysis.



    I have never used VBA in Access so I need some handholding assistance. Don't have any idea where to start.


    Code:
    Sub Main()
    ' Creates allocation of costs for fte positions by Account
    'Control screen updates
        Application.ScreenUpdating = False
        'Application.Calculation = xlCalculationManual
    'Clear Allocate worksheet
        Sheets("Allocate").Select
        Cells.Select
        Selection.ClearContents
        Range("A1").Select
            
    'Develop listing by Name, Account and FTE allocation percentage, omitting cases with blank cells
        Dim myArray() As Variant 'array that will hold data from AutorizedPositions table
        Dim TheLastRow As Long 'holds the value of the last row with data
        Dim TheLastCol As Long 'holds the value of the last column with data
        Dim eRow As Integer 'points to the current active row to be written
        Dim Counter1 As Integer 'used to determine location by row in table
        Dim Counter2 As Integer 'used to determine location by column in table
    'Setting up area to receive data and determine the area to be read into the array
        Sheets("AuthorizedPositions").Select
        TheLastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row 'Determine the number of rows filled
        TheLastCol = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column 'Determine how many columns will have data
        
        myArray = Range(Cells(1, 1), Cells(TheLastRow, TheLastCol)).Value 'Reads data into array
        
        eRow = 2 'By starting with 2 the loop steps over Name
    'Writes rows with data to worksheet creating a range of three columns
        For Counter1 = 2 To TheLastRow
            For Counter2 = 2 To TheLastCol
                If Not IsEmpty(myArray(Counter1, Counter2)) Then
                    Range("AC" & eRow).Value = myArray(Counter1, Counter2)
                    Range("AB" & eRow).Value = myArray(1, Counter2)
                    Range("AA" & eRow).Value = myArray(Counter1, 1)
                    eRow = eRow + 1
                End If
            Next Counter2
        Next Counter1
        Range("AA1").Value = "Name"
        Range("AB1").Value = "Account"
        Range("AC1").Value = "Allocate"
    'Move listing to Allocate worksheet
        Range("A1").Select
        Selection.End(xlToRight).Select
        Selection.End(xlToRight).Select
        Columns("AA:AC").Cut
        Sheets("Allocate").Select
        ActiveSheet.Paste
        Selection.Columns.AutoFit
    
    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,624
    One way to read/write data in tables is with VBA using recordsets. Here is one article http://msdn.microsoft.com/en-us/libr...ffice.12).aspx
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Move Data to Next Column Over
    By Jerseynjphillypa in forum Queries
    Replies: 5
    Last Post: 06-25-2012, 11:12 AM
  2. Replies: 3
    Last Post: 04-12-2012, 03:59 AM
  3. Using a query to move data to new columns
    By emunson in forum Queries
    Replies: 2
    Last Post: 12-01-2010, 10:16 PM
  4. Replies: 1
    Last Post: 08-09-2010, 03:59 PM
  5. Move data from one table to another
    By rebyrd in forum Queries
    Replies: 2
    Last Post: 12-24-2009, 12:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums