Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7794379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:45:28+00:00 2026-06-01T22:45:28+00:00

I have different column names in a table in an Oracle database and I

  • 0

I have different column names in a table in an Oracle database and I am fetching the data from oracle database to excel using VB macro and sorting the data with different column names to different sheets

Currently I wrote a seperate code for each sheet but I want to loop that so the code will be easy.There are 60 collabnames and all are different names and I want to sort them to 60 different sheet

So if I loop it I need not write the same code 60 times

CURRENT CODE:

    cn.Open ( _
    "User ID=USERID" & _
    ";Password=PASSWORD" & _
    ";Data Source=xx.xx.xx.xxx:xxxx/xxxx" & _
    ";Provider=OraOLEDB.Oracle")


    rs.Open "select COLLABNAME,DATETIME,TOTALFLOWS from TABLE_NAME AND COLLABNAME like 'COLLAB_NAME1' ORDER BY DATETIME ASC", cn
    With Sheet1
            col = 0
             'First Row: names of columns
            Do While col < rs.Fields.Count
                .Cells(1, col + 1) = rs.Fields(col).Name
                col = col + 1
            Loop


            mtxData = Application.Transpose(rs.GetRows)
            .Range("A2").Resize(UBound(mtxData, 1) - LBound(mtxData, 1) + 1, UBound(mtxData, 2) - LBound(mtxData, 2) + 1) = mtxData




        End With
        rs.Close

  rs.Open "select COLLABNAME,DATETIME,TOTALFLOWS from TABLE_NAME AND COLLABNAME like 'COLLAB_NAME_2' ORDER BY DATETIME ASC", cn
    With Sheet2
            col = 0
             'First Row: names of columns
            Do While col < rs.Fields.Count
                .Cells(1, col + 1) = rs.Fields(col).Name
                col = col + 1
            Loop


            mtxData = Application.Transpose(rs.GetRows)
            .Range("A2").Resize(UBound(mtxData, 1) - LBound(mtxData, 1) + 1, UBound(mtxData, 2) - LBound(mtxData, 2) + 1) = mtxData




        End With
        rs.Close

 rs.Open "select COLLABNAME,DATETIME,TOTALFLOWS from TABLE_NAME AND COLLABNAME like 'COLLAB_NAME1_NAME2_3' ORDER BY DATETIME ASC", cn
    With Sheet3
            col = 0
             'First Row: names of columns
            Do While col < rs.Fields.Count
                .Cells(1, col + 1) = rs.Fields(col).Name
                col = col + 1
            Loop


            mtxData = Application.Transpose(rs.GetRows)
            .Range("A2").Resize(UBound(mtxData, 1) - LBound(mtxData, 1) + 1, UBound(mtxData, 2) - LBound(mtxData, 2) + 1) = mtxData




        End With
        rs.Close
    End Sub

NEW CODE:

Sub Load_data()
        Sheets("Sheet1").Select
        Dim cn As ADODB.Connection
        Dim rs As ADODB.Recordset
        Dim col As Integer
        Dim row As Integer
        Dim Query As String
        Dim mtxData As Variant


        Set cn = New ADODB.Connection
        Set rs = New ADODB.Recordset

     cn.Open ( _
    "User ID=USERID" & _
    ";Password=PASSWORD" & _
    ";Data Source=xx.xx.xx.xxx:xxxx/xxxxxx" & _
    ";Provider=OraOLEDB.Oracle")

       'Creates an Array with all the Collabnames.

Dim arrayCOLLABNAME(59) As String
Dim idx As Integer
idx = 0
Sheets("COLLABNAME").Select
Range("A1").Select
Do While Not ActiveCell.Offset((idx), 0) = ""
    arrayCOLLABNAME(idx) = ActiveCell.Offset(idx, 0).Value
    idx = idx + 1
Loop
'The name of the First Sheet
    Sheets("Sheet1").Select
'Loop for 60 Sheets
For i = 0 To 60
    'adds the new Sheet
    Sheets.Add
    rs.Open "select COLLABNAME,DATETIME,TOTALFLOWS from TABLE_NAME AND COLLABNAME like '" & arrayCOLLABNAME(idx) & "' ORDER BY DATETIME ASC", cn
    'use the new Sheet for inserting the Data
    With ActiveSheet
        col = 0
        'First Row: names of columns
        Do While col < rs.Fields.Count
            .Cells(1, col + 1) = rs.Fields(col).Name
            col = col + 1
        Loop
        mtxData = Application.Transpose(rs.GetRows)
        .Range("A2").Resize(UBound(mtxData, 1) - LBound(mtxData, 1) + 1, UBound(mtxData, 2) - LBound(mtxData, 2) + 1) = mtxData

    End With
    rs.Close
Next i


    End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:45:29+00:00Added an answer on June 1, 2026 at 10:45 pm

    i think that could work for you. But i don’t had the time to test it.

    **new Code

        'Creates an Array with all the Collabnames.
    Dim arrayCOLLABNAME(59) As String
    Dim idx As Integer
    idx = 0
    Sheets("COLLABNAME").Select
    Range("A1").Select
    Do While Not ActiveCell.Offset((idx), 0) = ""
        arrayCOLLABNAME(idx) = ActiveCell.Offset(idx, 0).Value
        idx = idx + 1
    Loop
    

    'The name of the First Sheet
        Sheets("Tabelle1").Select
    'Loop for 60 Sheets
    For i = 0 To 60
        'adds the new Sheet
        Sheets.Add
        rs.Open "select COLLABNAME,DATETIME,TOTALFLOWS from TABLE_NAME AND COLLABNAME like '" & arrayCOLLABNAME(idx) & "' ORDER BY DATETIME ASC", cn
        'use the new Sheet for inserting the Data
        With ActiveSheet
            col = 0
            'First Row: names of columns
            Do While col < rs.Fields.Count
                .Cells(1, col + 1) = rs.Fields(col).Name
                col = col + 1
            Loop
            mtxData = Application.Transpose(rs.GetRows)
            .Range("A2").Resize(UBound(mtxData, 1) - LBound(mtxData, 1) + 1, UBound(mtxData, 2) - LBound(mtxData, 2) + 1) = mtxData
    
        End With
        rs.Close
    Next i
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large (multi-GB) data file exported from an Oracle table. I want
I have different php output in jQuery-based tabs. This output is formed from database
If I have a column names called category_id and Category_Id , are they different?
I have a column 'names' and 'ids' in table OG and want to find
I have a different arraylist with column names. I want to have a generatized
I have a weird grid with different height columns. By this I mean, column
I have a table withe different columns as shown below Ref Number Service Numbers
I have a table with lots of different columns, like resolution, brightness, type, width,
I have a table that has three different date columns, so I set each
I have a question regarding a unified insert query against tables with different data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.