Splitting ADODB Recordset to Excel worksheet?











up vote
1
down vote

favorite
1












I have a small macro program that extracts almost 2 million rows of data from SQL to Excel worksheet. But the problem is, each worksheet can only contain up to 1048576 rows, so it cuts my data.



I'm trying to figure out if there's a way to split the ADODB Recordset before you paste it to Excel.



Here's my code for extracting the data from SQL to Excel:



With oRecordSet
.ActiveConnection = oDBConnection
.Source = MySql
.LockType = adLockReadOnly
.CursorType = adOpenForwardOnly
.Open
End With
Sheets("Data)").Range("A2").CopyFromRecordset oRecordSet


Appreciate your help guys. Thanks in advance.










share|improve this question






















  • I believe the term you are looking for is "paging".
    – Mathieu Guindon
    Nov 9 at 4:25










  • I am not familiar with VBA but i got your point. How about if you use a limit of your script then remember the last record or insert the last record whatever you want to do for it. then paste or copy the other record into another sheet. After the first execution has been done.
    – Vijunav Vastivch
    Nov 9 at 5:23

















up vote
1
down vote

favorite
1












I have a small macro program that extracts almost 2 million rows of data from SQL to Excel worksheet. But the problem is, each worksheet can only contain up to 1048576 rows, so it cuts my data.



I'm trying to figure out if there's a way to split the ADODB Recordset before you paste it to Excel.



Here's my code for extracting the data from SQL to Excel:



With oRecordSet
.ActiveConnection = oDBConnection
.Source = MySql
.LockType = adLockReadOnly
.CursorType = adOpenForwardOnly
.Open
End With
Sheets("Data)").Range("A2").CopyFromRecordset oRecordSet


Appreciate your help guys. Thanks in advance.










share|improve this question






















  • I believe the term you are looking for is "paging".
    – Mathieu Guindon
    Nov 9 at 4:25










  • I am not familiar with VBA but i got your point. How about if you use a limit of your script then remember the last record or insert the last record whatever you want to do for it. then paste or copy the other record into another sheet. After the first execution has been done.
    – Vijunav Vastivch
    Nov 9 at 5:23















up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I have a small macro program that extracts almost 2 million rows of data from SQL to Excel worksheet. But the problem is, each worksheet can only contain up to 1048576 rows, so it cuts my data.



I'm trying to figure out if there's a way to split the ADODB Recordset before you paste it to Excel.



Here's my code for extracting the data from SQL to Excel:



With oRecordSet
.ActiveConnection = oDBConnection
.Source = MySql
.LockType = adLockReadOnly
.CursorType = adOpenForwardOnly
.Open
End With
Sheets("Data)").Range("A2").CopyFromRecordset oRecordSet


Appreciate your help guys. Thanks in advance.










share|improve this question













I have a small macro program that extracts almost 2 million rows of data from SQL to Excel worksheet. But the problem is, each worksheet can only contain up to 1048576 rows, so it cuts my data.



I'm trying to figure out if there's a way to split the ADODB Recordset before you paste it to Excel.



Here's my code for extracting the data from SQL to Excel:



With oRecordSet
.ActiveConnection = oDBConnection
.Source = MySql
.LockType = adLockReadOnly
.CursorType = adOpenForwardOnly
.Open
End With
Sheets("Data)").Range("A2").CopyFromRecordset oRecordSet


Appreciate your help guys. Thanks in advance.







sql sql-server excel vba excel-vba






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 3:40









bigbryan

1363825




1363825












  • I believe the term you are looking for is "paging".
    – Mathieu Guindon
    Nov 9 at 4:25










  • I am not familiar with VBA but i got your point. How about if you use a limit of your script then remember the last record or insert the last record whatever you want to do for it. then paste or copy the other record into another sheet. After the first execution has been done.
    – Vijunav Vastivch
    Nov 9 at 5:23




















  • I believe the term you are looking for is "paging".
    – Mathieu Guindon
    Nov 9 at 4:25










  • I am not familiar with VBA but i got your point. How about if you use a limit of your script then remember the last record or insert the last record whatever you want to do for it. then paste or copy the other record into another sheet. After the first execution has been done.
    – Vijunav Vastivch
    Nov 9 at 5:23


















I believe the term you are looking for is "paging".
– Mathieu Guindon
Nov 9 at 4:25




I believe the term you are looking for is "paging".
– Mathieu Guindon
Nov 9 at 4:25












I am not familiar with VBA but i got your point. How about if you use a limit of your script then remember the last record or insert the last record whatever you want to do for it. then paste or copy the other record into another sheet. After the first execution has been done.
– Vijunav Vastivch
Nov 9 at 5:23






I am not familiar with VBA but i got your point. How about if you use a limit of your script then remember the last record or insert the last record whatever you want to do for it. then paste or copy the other record into another sheet. After the first execution has been done.
– Vijunav Vastivch
Nov 9 at 5:23














1 Answer
1






active

oldest

votes

















up vote
0
down vote













You can query the data and apply some filtering logic.



https://support.office.com/en-us/article/Use-Microsoft-Query-to-retrieve-external-data-42a2ea18-44d9-40b3-9c38-4c62f252da2e



You can try delimit, and manage up to 100 million rows.



http://www.delimitware.com/



Or, use a file splitting tool.



https://sourceforge.net/projects/simpletextsplit/
https://www.makeuseof.com/tag/how-to-split-a-huge-csv-excel-workbook-into-seperate-files/



You can try a VBA solution as well.



step1

Save As, your Workbook with extension .xlsm (macros enabled)

step2

1) press ALT+F11 to open Visual Basic

2) Insert > module and paste the code below on the right

(from Sub....End Sub)

Sub SplitTxt_01()

Const HelperFile As String = "ABCD" '<<< temp. helper text file Name
Const N As Long = 700000 '<<< split each txt in N rows, CHANGE
Dim myPath
myPath = "c:Folder1Folder2" '<<< folder path, CHANGE
Dim myFile
myFile = "Data File.TXT" '<<< your text file. CHANGE txt file name as needed

Dim WB As Workbook, myWB As Workbook
Set myWB = ThisWorkbook
Dim myWS As Worksheet
Dim t As Long, r As Long
Dim myStr
Application.ScreenUpdating = False

'split text file in separate text files
myFile = Dir(myPath & myFile)
Open myPath & myFile For Input As #1
t = 1
r = 1
Do While Not EOF(1)
Line Input #1, myStr
If r > N Then
t = t + 1
r = 1
End If
Open myPath & HelperFile & t & ".txt" For Append As #2
Print #2, myStr
Close #2
r = r + 1
Loop
Close #1

'copy txt files in separate sheets
For i = t To 1 Step -1
Workbooks.OpenText Filename:=myPath & HelperFile & i & ".txt", DataType:=xlDelimited, Tab:=True
Set WB = ActiveWorkbook
Set rng = ActiveSheet.UsedRange
Set myWS = myWB.Sheets.Add
myWS.Name = HelperFile & i
rng.Copy myWS.Cells(1, 1)
WB.Close False
Next
myWB.Save

'Delete helper txt files
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Fldr = Fso.GetFolder(myPath)
For Each Filename In Fldr.Files
If Filename Like "*" & HelperFile & "*" Then Filename.Delete
Next
Application.ScreenUpdating = True
End Sub

3) Press ALT+Q to Close Visual Basic


As a final thought, I'll say it may be time to move up to Python or R.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219526%2fsplitting-adodb-recordset-to-excel-worksheet%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You can query the data and apply some filtering logic.



    https://support.office.com/en-us/article/Use-Microsoft-Query-to-retrieve-external-data-42a2ea18-44d9-40b3-9c38-4c62f252da2e



    You can try delimit, and manage up to 100 million rows.



    http://www.delimitware.com/



    Or, use a file splitting tool.



    https://sourceforge.net/projects/simpletextsplit/
    https://www.makeuseof.com/tag/how-to-split-a-huge-csv-excel-workbook-into-seperate-files/



    You can try a VBA solution as well.



    step1

    Save As, your Workbook with extension .xlsm (macros enabled)

    step2

    1) press ALT+F11 to open Visual Basic

    2) Insert > module and paste the code below on the right

    (from Sub....End Sub)

    Sub SplitTxt_01()

    Const HelperFile As String = "ABCD" '<<< temp. helper text file Name
    Const N As Long = 700000 '<<< split each txt in N rows, CHANGE
    Dim myPath
    myPath = "c:Folder1Folder2" '<<< folder path, CHANGE
    Dim myFile
    myFile = "Data File.TXT" '<<< your text file. CHANGE txt file name as needed

    Dim WB As Workbook, myWB As Workbook
    Set myWB = ThisWorkbook
    Dim myWS As Worksheet
    Dim t As Long, r As Long
    Dim myStr
    Application.ScreenUpdating = False

    'split text file in separate text files
    myFile = Dir(myPath & myFile)
    Open myPath & myFile For Input As #1
    t = 1
    r = 1
    Do While Not EOF(1)
    Line Input #1, myStr
    If r > N Then
    t = t + 1
    r = 1
    End If
    Open myPath & HelperFile & t & ".txt" For Append As #2
    Print #2, myStr
    Close #2
    r = r + 1
    Loop
    Close #1

    'copy txt files in separate sheets
    For i = t To 1 Step -1
    Workbooks.OpenText Filename:=myPath & HelperFile & i & ".txt", DataType:=xlDelimited, Tab:=True
    Set WB = ActiveWorkbook
    Set rng = ActiveSheet.UsedRange
    Set myWS = myWB.Sheets.Add
    myWS.Name = HelperFile & i
    rng.Copy myWS.Cells(1, 1)
    WB.Close False
    Next
    myWB.Save

    'Delete helper txt files
    Set Fso = CreateObject("Scripting.FileSystemObject")
    Set Fldr = Fso.GetFolder(myPath)
    For Each Filename In Fldr.Files
    If Filename Like "*" & HelperFile & "*" Then Filename.Delete
    Next
    Application.ScreenUpdating = True
    End Sub

    3) Press ALT+Q to Close Visual Basic


    As a final thought, I'll say it may be time to move up to Python or R.






    share|improve this answer

























      up vote
      0
      down vote













      You can query the data and apply some filtering logic.



      https://support.office.com/en-us/article/Use-Microsoft-Query-to-retrieve-external-data-42a2ea18-44d9-40b3-9c38-4c62f252da2e



      You can try delimit, and manage up to 100 million rows.



      http://www.delimitware.com/



      Or, use a file splitting tool.



      https://sourceforge.net/projects/simpletextsplit/
      https://www.makeuseof.com/tag/how-to-split-a-huge-csv-excel-workbook-into-seperate-files/



      You can try a VBA solution as well.



      step1

      Save As, your Workbook with extension .xlsm (macros enabled)

      step2

      1) press ALT+F11 to open Visual Basic

      2) Insert > module and paste the code below on the right

      (from Sub....End Sub)

      Sub SplitTxt_01()

      Const HelperFile As String = "ABCD" '<<< temp. helper text file Name
      Const N As Long = 700000 '<<< split each txt in N rows, CHANGE
      Dim myPath
      myPath = "c:Folder1Folder2" '<<< folder path, CHANGE
      Dim myFile
      myFile = "Data File.TXT" '<<< your text file. CHANGE txt file name as needed

      Dim WB As Workbook, myWB As Workbook
      Set myWB = ThisWorkbook
      Dim myWS As Worksheet
      Dim t As Long, r As Long
      Dim myStr
      Application.ScreenUpdating = False

      'split text file in separate text files
      myFile = Dir(myPath & myFile)
      Open myPath & myFile For Input As #1
      t = 1
      r = 1
      Do While Not EOF(1)
      Line Input #1, myStr
      If r > N Then
      t = t + 1
      r = 1
      End If
      Open myPath & HelperFile & t & ".txt" For Append As #2
      Print #2, myStr
      Close #2
      r = r + 1
      Loop
      Close #1

      'copy txt files in separate sheets
      For i = t To 1 Step -1
      Workbooks.OpenText Filename:=myPath & HelperFile & i & ".txt", DataType:=xlDelimited, Tab:=True
      Set WB = ActiveWorkbook
      Set rng = ActiveSheet.UsedRange
      Set myWS = myWB.Sheets.Add
      myWS.Name = HelperFile & i
      rng.Copy myWS.Cells(1, 1)
      WB.Close False
      Next
      myWB.Save

      'Delete helper txt files
      Set Fso = CreateObject("Scripting.FileSystemObject")
      Set Fldr = Fso.GetFolder(myPath)
      For Each Filename In Fldr.Files
      If Filename Like "*" & HelperFile & "*" Then Filename.Delete
      Next
      Application.ScreenUpdating = True
      End Sub

      3) Press ALT+Q to Close Visual Basic


      As a final thought, I'll say it may be time to move up to Python or R.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You can query the data and apply some filtering logic.



        https://support.office.com/en-us/article/Use-Microsoft-Query-to-retrieve-external-data-42a2ea18-44d9-40b3-9c38-4c62f252da2e



        You can try delimit, and manage up to 100 million rows.



        http://www.delimitware.com/



        Or, use a file splitting tool.



        https://sourceforge.net/projects/simpletextsplit/
        https://www.makeuseof.com/tag/how-to-split-a-huge-csv-excel-workbook-into-seperate-files/



        You can try a VBA solution as well.



        step1

        Save As, your Workbook with extension .xlsm (macros enabled)

        step2

        1) press ALT+F11 to open Visual Basic

        2) Insert > module and paste the code below on the right

        (from Sub....End Sub)

        Sub SplitTxt_01()

        Const HelperFile As String = "ABCD" '<<< temp. helper text file Name
        Const N As Long = 700000 '<<< split each txt in N rows, CHANGE
        Dim myPath
        myPath = "c:Folder1Folder2" '<<< folder path, CHANGE
        Dim myFile
        myFile = "Data File.TXT" '<<< your text file. CHANGE txt file name as needed

        Dim WB As Workbook, myWB As Workbook
        Set myWB = ThisWorkbook
        Dim myWS As Worksheet
        Dim t As Long, r As Long
        Dim myStr
        Application.ScreenUpdating = False

        'split text file in separate text files
        myFile = Dir(myPath & myFile)
        Open myPath & myFile For Input As #1
        t = 1
        r = 1
        Do While Not EOF(1)
        Line Input #1, myStr
        If r > N Then
        t = t + 1
        r = 1
        End If
        Open myPath & HelperFile & t & ".txt" For Append As #2
        Print #2, myStr
        Close #2
        r = r + 1
        Loop
        Close #1

        'copy txt files in separate sheets
        For i = t To 1 Step -1
        Workbooks.OpenText Filename:=myPath & HelperFile & i & ".txt", DataType:=xlDelimited, Tab:=True
        Set WB = ActiveWorkbook
        Set rng = ActiveSheet.UsedRange
        Set myWS = myWB.Sheets.Add
        myWS.Name = HelperFile & i
        rng.Copy myWS.Cells(1, 1)
        WB.Close False
        Next
        myWB.Save

        'Delete helper txt files
        Set Fso = CreateObject("Scripting.FileSystemObject")
        Set Fldr = Fso.GetFolder(myPath)
        For Each Filename In Fldr.Files
        If Filename Like "*" & HelperFile & "*" Then Filename.Delete
        Next
        Application.ScreenUpdating = True
        End Sub

        3) Press ALT+Q to Close Visual Basic


        As a final thought, I'll say it may be time to move up to Python or R.






        share|improve this answer












        You can query the data and apply some filtering logic.



        https://support.office.com/en-us/article/Use-Microsoft-Query-to-retrieve-external-data-42a2ea18-44d9-40b3-9c38-4c62f252da2e



        You can try delimit, and manage up to 100 million rows.



        http://www.delimitware.com/



        Or, use a file splitting tool.



        https://sourceforge.net/projects/simpletextsplit/
        https://www.makeuseof.com/tag/how-to-split-a-huge-csv-excel-workbook-into-seperate-files/



        You can try a VBA solution as well.



        step1

        Save As, your Workbook with extension .xlsm (macros enabled)

        step2

        1) press ALT+F11 to open Visual Basic

        2) Insert > module and paste the code below on the right

        (from Sub....End Sub)

        Sub SplitTxt_01()

        Const HelperFile As String = "ABCD" '<<< temp. helper text file Name
        Const N As Long = 700000 '<<< split each txt in N rows, CHANGE
        Dim myPath
        myPath = "c:Folder1Folder2" '<<< folder path, CHANGE
        Dim myFile
        myFile = "Data File.TXT" '<<< your text file. CHANGE txt file name as needed

        Dim WB As Workbook, myWB As Workbook
        Set myWB = ThisWorkbook
        Dim myWS As Worksheet
        Dim t As Long, r As Long
        Dim myStr
        Application.ScreenUpdating = False

        'split text file in separate text files
        myFile = Dir(myPath & myFile)
        Open myPath & myFile For Input As #1
        t = 1
        r = 1
        Do While Not EOF(1)
        Line Input #1, myStr
        If r > N Then
        t = t + 1
        r = 1
        End If
        Open myPath & HelperFile & t & ".txt" For Append As #2
        Print #2, myStr
        Close #2
        r = r + 1
        Loop
        Close #1

        'copy txt files in separate sheets
        For i = t To 1 Step -1
        Workbooks.OpenText Filename:=myPath & HelperFile & i & ".txt", DataType:=xlDelimited, Tab:=True
        Set WB = ActiveWorkbook
        Set rng = ActiveSheet.UsedRange
        Set myWS = myWB.Sheets.Add
        myWS.Name = HelperFile & i
        rng.Copy myWS.Cells(1, 1)
        WB.Close False
        Next
        myWB.Save

        'Delete helper txt files
        Set Fso = CreateObject("Scripting.FileSystemObject")
        Set Fldr = Fso.GetFolder(myPath)
        For Each Filename In Fldr.Files
        If Filename Like "*" & HelperFile & "*" Then Filename.Delete
        Next
        Application.ScreenUpdating = True
        End Sub

        3) Press ALT+Q to Close Visual Basic


        As a final thought, I'll say it may be time to move up to Python or R.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 at 14:26









        ryguy72

        3,6991619




        3,6991619






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219526%2fsplitting-adodb-recordset-to-excel-worksheet%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)