+ Reply to Thread
Results 1 to 4 of 4

Running an SQL query iterating through each worksheet

  1. #1
    Registered User
    Join Date
    03-07-2019
    Location
    Sydney
    MS-Off Ver
    Office365
    Posts
    22

    Running an SQL query iterating through each worksheet

    I have an SQL query which I need to run dedicated to each employee.

    Each employee is to have their own sheet on the workbook. I have created the VBA script which queries our database, pulls a list of employees, lists them on the first sheet in E1:E(x), then creates all the sheets based on that list. Now the task is to run a query for each employee and have the results in each sheet. Essentially:

    Employees: AA, AB, AC, AD, AE
    Variable = AA

    For each employee,
    Run query with Variable
    Insert query results
    Increment Variable
    Repeat until end of Employees.

    I've Googled it from every angle I can think of, but I can't find a solution. I'm drawing a blank. I've tried
    Please Login or Register  to view this content.
    just as a test, but this results in the value being put only in the last employee sheet. It doesn't iterate through the rest of the employees. If I use Worksheets("First sheet").Select in the middle, the results are only on the "First sheet".

    If I can insert a value into each sheet, I can just replace the inserting of the value with the query and iterate through that, replacing the employee variable to match the sheet.

    Can anyone help?

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Running an SQL query iterating through each worksheet

    Hi therufus,

    The following will loop through each tab in a workbook putting each tab name in a message box to show you where you'd insert your code:

    Please Login or Register  to view this content.
    I must say though that having separate identical tabs for each employee is not the way to go in my humble opinion. I would have a single data tab from which other reporting tabs would run from, similar to a database so instead of separate tabs you could use a drop-down in one tab to bring in the employee's data via lookups.

    HTH

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Registered User
    Join Date
    03-07-2019
    Location
    Sydney
    MS-Off Ver
    Office365
    Posts
    22

    Re: Running an SQL query iterating through each worksheet

    Hey Robert.

    I tried what you suggested, and replaced the Msg box with Range("A1").Value = "Change Value". Unfortunately, it didn't work. All that happened was "Change Value" was put in A1 of the last sheet. All the other sheets remained blank.

    I'm not sure if another approach would be to create a new sheet, rename to employee initials, run query, create new sheet, rename to employee initials...... etc until the list is exhausted. I'm not sure.

    I know the result needs to be one tab for each employee.

  4. #4
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Running an SQL query iterating through each worksheet

    I tried what you suggested, and replaced the Msg box with Range("A1").Value = "Change Value". Unfortunately, it didn't work. All that happened was "Change Value" was put in A1 of the last sheet. All the other sheets remained blank.
    You need to use the wsMySheet variable, i.e. the following will put the text "Change Value" in cell A1 of each sheet in the workbook:

    Please Login or Register  to view this content.
    I know the result needs to be one tab for each employee.
    In just about every case I've seen this is not the way to go but it's obviously your call. Depending on how many employees the company has the workbook could quickly become unwieldy.

    Robert

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] How to prevent worksheet code from running while Module Macro is running:
    By Chad Bateman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-11-2015, 09:26 AM
  2. Iterating over all the parameter query values? VBA task and I need help!
    By Rosiel in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-08-2013, 12:25 PM
  3. Running a query from excel
    By silverxx12 in forum Access Tables & Databases
    Replies: 0
    Last Post: 06-18-2012, 05:36 PM
  4. Replies: 0
    Last Post: 02-10-2009, 02:41 PM
  5. running and saving a scheduled query in ms query
    By DaVinci in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 04-16-2007, 04:26 AM
  6. [SOLVED] Help on Running Query
    By jackle in forum Excel General
    Replies: 5
    Last Post: 01-25-2006, 05:50 PM
  7. Running Query
    By Krish in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 11-10-2005, 03:40 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1