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 7277865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:47:55+00:00 2026-05-28T22:47:55+00:00

I want to run a script in my macro multiple times by changing variable

  • 0

I want to run a script in my macro multiple times by changing variable values.
Below is an example of my code that I run for one value.
The line of code I would like to change is

sScript = sScript + "where m.outletid in ('" & sOutletId & "') " & vbCrLf

Sometime I want the where clause to be

where m.outletid in (‘12314’)

or

where m.chainid in (‘411’)…

Code:

Sub Report()
    Dim sScript As String
    Dim sServer As String
    Dim sDatabase As String
    Dim sTransTable As String
    Dim iVal As Integer
    Dim iReturnVal As Integer
    Dim SheetExists As Worksheet
    Dim WK_SHEET As String

       sServer = Trim(UserForm1.txtServer.Value)
       sDatabase = Trim(UserForm1.txtDatabase.Value)
       sTransTable = Trim(UserForm1.txtTransTable.Value)

       For Each SheetExists In Worksheets
          If SheetExists.Name = ("Report") Then
          Application.DisplayAlerts = False
          Sheets("Report").Delete
          Application.DisplayAlerts = True
      Exit For
      End If
      Next SheetExists

      Worksheets.Add after:=Sheets("Sheet1")
      ActiveSheet.Name = ("Report")

      WK_SHEET = "Report"

      Sheets(WK_SHEET).Select

       sOutletId = "12314"

       sScript = "Select top 10 m.CustNumber, m.Name, sum(t.Transvalue) " & vbCrLf        
       sScript = sScript + "from " & sTransTable & " t " & vbCrLf
       sScript = sScript + "where m.outletid in ('" & sOutletId & "') " & vbCrLf
       sScript = sScript + "Group by m.CustNumber, m.Name " & vbCrLf
       sScript = sScript + "order by sum(t.Transvalue)Desc " & vbCrLf

      iReply = MsgBox(Prompt:="Do you wish to continue with the following script for Top 10 Customers?" + sScript + "", _
    Buttons:=vbYesNo, Title:="Run MACRO Top 10 Reports")
    If iReply = vbNo Then
        End
    End If

    iVal = execute_sql_select(WK_SHEET, 2, 1, sServer, sDatabase, sScript)

    Sheets(WK_SHEET).Name = "Outlet" & sOutletId & "Top 10 by Spend"

Now I would like to re run the above with OutletId 12315…how can I do this? Do I use some sort of loop?

  • 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-05-28T22:47:55+00:00Added an answer on May 28, 2026 at 10:47 pm

    You can keep list of OutletId into Array. Then get each OutletId from Array (for loop) and execute your sql script.

    Pseudu code

    Array listOutid = new Array[12,13,14,15];
    
    for(int idx = 0; idx < listOutid.Length; idx++)
    {
        var OutletId = listOutid[idx];
    
        //put ur sql statement and execute here..
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to run my ruby script x times a day (the number might
I want to run a php script from the command line that is always
I have a zsh script that I want to run such that it also
I want to run a php script on christmas that will email my family
I have a sql script file and i want to run that i can
I have created a Python script that I want to run daily via a
i want to run the script behind the button of Google search that means...posting
I wrote a Ruby script that I want to run in the background repeatedly
I have a Ruby 1.9 script that I want to run as a long-running
I want to run a script that extracts an integer from the URL hash

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.