Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Drew101 is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2017
    Posts
    22

    Exporting data and summing per 5k lines


    Stuck on how to structure or perform this next step.

    I need for access to export 5,000 lines at a time from [Pre
    _upload]Table. Then go to the next 5,000, until all lines have been exported into excel, 5,000 at a time, per tab. In each export the first row should reflect a sum total of the 5,000 lines.

    I am thinking this may be a macro in access that exports the data by number of lines. Could someone point me in the right direction? Or is there a way to produce this data in Access the exact same way. I basically creating an upload database per 5,000 lines at a time from a database that holds 40,000 lines. So I should have 8 tabs in excel or 8 tables generate if it is in access

  2. #2
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    So I should have 8 tabs in excel or 8 tables generate if it is in access
    As someone who writes a lot of VBA for both Access and Excel, if you are exporting this all to Excel, I think it would be much easier to export all 40,000 rows to Excel, and then have an Excel macro split it for you and add your SUM totals.

  3. #3
    Drew101 is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2017
    Posts
    22
    Ok, should I switch to excel vba questions or may I ask you in this forum how to proceed with this vba

  4. #4
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I can help you with the code, if you can describe the structure of the data you are exporting.
    What format is it being exported in (Excel directly, or something else then imported into Excel)?
    How many columns are being exported?
    Which column is the one you want to Sum?

  5. #5
    Drew101 is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2017
    Posts
    22
    Ok the Qry_Upload is the file that will be droping out of Access into excel. I will then like to open my Macro File and run and it do the following


    Click image for larger version. 

Name:	Test_1.2.2.JPG 
Views:	21 
Size:	250.7 KB 
ID:	28924Click image for larger version. 

Name:	Test_1.2.1.JPG 
Views:	20 
Size:	129.2 KB 
ID:	28925

  6. #6
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Is there a second image there? I cannot see it. You said:
    I will then like to open my Macro File and run and it do the following
    What is it that you would like it to do, besides separate into 5000 rows per tab?

  7. #7
    Drew101 is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2017
    Posts
    22
    here is the next step. I don't know why the other attachment didn't work
    Attached Thumbnails Attached Thumbnails Test_1.3.JPG  

  8. #8
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Odd. I can see it on my work computer, but not my home computer.

    So, are columns A-J what comes over on the file, and we are creating column K?

    I do not understand what this line means:
    Line 1 would have the opposite value of the total sum of THE Amount FIELD. AKA offset for the je to balance to zero
    Could you perhaps post the an example of what the expected output would look like before exporting from Excel to a Tab Delimited file (maybe just post the first 20 lines including the header)?

  9. #9
    Drew101 is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2017
    Posts
    22
    Not per say, Column K will be there in the file but blank, we would have to create the number sequence if that is what you are saying. The 1,2,3,4,5 etc up to 5000+

    Here is another break down


    Click image for larger version. 

Name:	Capture1.JPG 
Views:	15 
Size:	241.7 KB 
ID:	28932

  10. #10
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Wow, that is a pretty big ask (there seems to be more and more to do with every single post)!

    That is going to take a bit of time to whip up.

  11. #11
    Drew101 is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2017
    Posts
    22
    I will start on what I can do and see how far I can get.

  12. #12
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    OK. I came up with something for you here. I saved this VBA code to its own Excel file, and I am using cells for you to place your "inputs" which may change, i.e.

    Cell B1: Full path and name of data file from Access
    Cell B2: Number of rows you want per file
    Cell B3: Value you want in row 2 under "Reference" field
    Cell B4: Value you want in row 2 under "Description" field
    Cell B5: Path you want exported tab-delimited files to go to

    As a default, the exported files are named with the following naming convention: yyyymmdd_s.tab (where s is a counter)
    So if it works out that three files are exported today, they would be named:
    20170531_1.tab
    20170531_2.tab
    20170531_3.tab


    You can obviously make changes to the VBA code to suit your needs. I documented it a lot, so you can follow along to see what is happening where.
    Code:
    Sub MyConversionMacro()
    
        Dim wbMacro As Workbook
        Dim wbData As Workbook
        Dim wbOutput As Workbook
        
        Dim dataFName As String
        Dim numRows As Long
        Dim myRef As String
        Dim myDescript As String
        Dim expPath As String
        Dim expFName As String
        
        Dim lastRow As Long
        Dim numSheets As Long
        Dim newSheets As Long
        Dim s As Long
        
        Dim sht As Long
        Dim sRow As Long
        Dim eRow As Long
        Dim lRow As Long
        
        Application.ScreenUpdating = False
        
    '   Capture settings from macro workbook
        Set wbMacro = ActiveWorkbook
        dataFName = Range("B1")     'pull full file path and name of data file
        numRows = Range("B2")       'pull number of rows from cell B2
        myRef = Range("B3")         'pull Reference from cell B3
        myDescript = Range("B4")    'pull Description from cell B4
        expPath = Range("B5")       'pull export file path from cell B5
        If Right(expPath, 1) <> "\" Then expPath = expPath & "\"
        
    '   Open and capture data file
        Workbooks.Open (dataFName)
        Set wbData = ActiveWorkbook
        
    '   Find last row with data on data file (using column A)
        lastRow = Cells(Rows.Count, "A").End(xlUp).Row
        
    '   Calculate how many tabs/files are needed
        numSheets = Application.WorksheetFunction.RoundUp((lastRow - 1) / numRows, 0)
    
    '   Open a new blank workbook
        Workbooks.Add
        Set wbOutput = ActiveWorkbook
        
    '   Insert/delete new sheets as needed to new workbook
        newSheets = numSheets - wbOutput.Sheets.Count
        Select Case newSheets
            Case Is > 0
                For s = 1 To newSheets
                    Sheets.Add After:=Sheets(Sheets.Count)
                Next s
            Case Is < 0
                For s = -1 To newSheets Step -1
                    Sheets(Sheets.Count).Select
                    Application.DisplayAlerts = False
                    ActiveWindow.SelectedSheets.Delete
                    Application.DisplayAlerts = True
                Next s
        End Select
        
    '   Loop through and build each sheet
        For sht = 1 To numSheets
            wbData.Activate
    '       Copy header
            wbData.Sheets(1).Range("A1:K1").Copy wbOutput.Sheets(sht).Range("A1")
    '       Copy rows
            sRow = numRows * (sht - 1) + 2
            eRow = numRows * sht + 1
            wbData.Sheets(1).Range(Cells(sRow, "A"), Cells(eRow, "K")).Copy wbOutput.Sheets(sht).Range("A3")
    '       Populate output sheets
            wbOutput.Activate
            Sheets(sht).Activate
    '       Find last row with data
            lRow = Cells(Rows.Count, "A").End(xlUp).Row
    '       Populate row 2
            Range("A3:B3").Copy Range("A2")
            Range("E2").FormulaR1C1 = "=SUM(R[1]C:R[" & lRow - 2 & "]C)"
            Range("C2").FormulaR1C1 = "=IF(RC[2]>0,RC[2],"""")"
            Range("D2").FormulaR1C1 = "=IF(RC[1]<0,RC[1],"""")"
            Range("F2") = myRef
            Range("G2") = myDescript
    '       Populate sequence number
            Range("K2:K" & lRow).Formula = "=Row()-1"
        Next sht
    
    '   Close data file
        wbData.Close
        
    '   Loop through and export sheets
        wbOutput.Activate
        For sht = 1 To numSheets
            Sheets(sht).Activate
    '       Build file name
            expFName = Format(Date, "yyyymmdd_") & sht & ".tab"
    '       Export sheet
            ActiveWorkbook.SaveAs Filename:=expPath & expFName, _
                FileFormat:=xlText, CreateBackup:=False
        Next sht
        
    '   Close workbook
        wbOutput.Close True
        
        Application.ScreenUpdating = True
        
        MsgBox "Done!"
            
    End Sub

  13. #13
    Drew101 is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2017
    Posts
    22
    WOW! Thanks Second any suggest on things to read that will allow me to write like that some day and Third for this String
    [Range("E2").FormulaR1C1 = "=SUM(R[1]C:R[" & lRow - 2 & "]C)"][code] I need it to bring the opposite value back. Currently it is just adding the sum and not making it the opposite. So would the correction be
    [Range("E2").FormulaR1C1 = "=-SUM(R[1]C:R[" & lRow - 2 & "]C)"][code]

  14. #14
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I am not sure I understand your question (your post looks a little garbled).
    But if is something is pulling back the correct value, but with the wrong sign, placing a negative in front of the formula should correct that.
    That should be easy enough to test.

  15. #15
    Drew101 is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2017
    Posts
    22
    Yes that confirms what I was asking For example

    [Range("E2").FormulaR1C1 = "=-SUM(R[1]C:R[" & lRow - 2 & "]C)"] [code]

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 04-19-2016, 09:56 AM
  2. Summing Multiple Queries & Summing Time
    By WEJ in forum Queries
    Replies: 2
    Last Post: 10-04-2013, 04:46 PM
  3. Replies: 5
    Last Post: 02-16-2013, 07:57 AM
  4. Replies: 1
    Last Post: 09-07-2011, 01:56 PM
  5. Replies: 0
    Last Post: 01-24-2009, 11:40 AM

Tags for this Thread

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