SQL Server generate SELECT script using Excel VBA

This blog article shows you how to build the SQL statement using VBA. The code needs to follow the following format of the Excel sheet.

– The Sheet name is the table name.

– Column A is the column name.

– Column B is the value of the WHERE condition.

– Column C is the condition such as =. LIKE and so.

– Column D is to check whether the column is string or numeric data type.

This VBA code is as follow, you can download the code from the link at the end of this blog article.

Dim sql As String

Sub GenSQL()

    Dim sqlsub As String

    Dim strSheet As String

    sql = “”

    strSheet = ActiveSheet.Name    

    sqlsub = “SELECT * FROM ” & strSheet & ” WHERE ”

    Gencondidtion

    sqlsub = sqlsub & sql

    Worksheets(“result”).Activate

    Range(“A1”).Value = sqlsub    

End Sub

Sub Gencondidtion()    

    Range(“A1”).Select

    While (ActiveCell.Value <> “”)

        If (ActiveCell.Offset(0, 2).Value = “=”) Then

            If ((ActiveCell.Offset(0, 3).Value = “numeric”)) Then

                sql = sql & NumericType

            Else

                sql = sql & StringType

            End If

        ElseIf (ActiveCell.Offset(0, 2).Value = “LIKE”) Then

            sql = sql & StringTypeLIKE

        End If        

        ActiveCell.Offset(1, 0).Select

        If (ActiveCell.Value <> “”) Then

            sql = sql & ” AND ”

        End If

    Wend

End Sub

Function StringType() As String

    Dim str As String    

    ‘str = ActiveCell & ” = ‘” & ActiveCell.Offset(0, 1).Value & “‘”

    str = ActiveCell & ” = ” & “‘” & ActiveCell.Offset(0, 1).Value & “‘”

    StringType = str

End Function

Function NumericType() As String

    Dim str As String    

    str = ActiveCell & ” = ” & ActiveCell.Offset(0, 1).Value

    NumericType = str

End Function

Function StringTypeLIKE() As String

    Dim str As String    

    str = ActiveCell & ” LIKE ‘%” & ActiveCell.Offset(0, 1).Value & “%'”

    StringTypeLIKE = str

End Function

Also: SQL Server generate Create Table script using Excel VBA

Above is the SQL statement generated when I executed the VBA code.

Source code down: https://github.com/chanmmn/vba/blob/master/2024/SQLQueryGenerate.xlsm/?WT.mc_id=DP-MVP-36769

Reference: https://learn.microsoft.com/en-us/office/vba/library-reference/concepts/getting-started-with-vba-in-office/?WT.mc_id=DP-MVP-36769

About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress (http://www.pcworld.com/article/206455/Microsoft_Live_Spaces_Moves_to_WordPress_An_FAQ.html) till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net, Cloud, Community, Computers and Internet, Microsoft Office, Office 365, programming and tagged . Bookmark the permalink.

1 Response to SQL Server generate SELECT script using Excel VBA

  1. Pingback: VBA Loop to Office.js Loop | Chanmingman's Blog

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.