Passing Parameter Values from Excel to SQL Server

In this video we will perform what should be a relatively simple task but isn't intuitive at all. I'll show you how to pass the values from Excel cells to a dynamic SQL Server query that will execute and return the values to Excel. This will make a nice little Excel data portfolio project for those of you looking to learn. Check it out! I will say there are many ways to do this, but this method is fine for personal use only, not for a distributable deliverable due to the lack of stored proc usage.
★☆★ Join this channel to get access to perks: ★☆★
/ @anthonysmoak
Watch the entire playlist to learn additional techniques using stored procedures:
• SQL Meets Excel: Advan...
#SQL #analytics #tsql #excel #dataanalyst #data
Download the Database used in the video: learn.microsoft.com/en-us/sql...
★☆★ FOLLOW ME BELOW: ★☆★
Blog ► anthonysmoak.com/
Instagram ► / @anthonysmoakdata
Facebook ► / anthonybsmoak
Tableau Public ► bit.ly/3JMKsLY
1:48 Demonstrate worksheet
3:45 SQL Query Overview
4:45 Start worksheet Build
5:47 Record Macro
6:17 Connect to Data
8:17 Explain & Build VBA
16:06 Insert Button for Refresh
17:16 Impromptu Debugging
17:54 It Works!!
18:30 Adjust Column Width Property
19:24 Confirm Results
20:00 Date Picker Ad-In
20:48 Password Protect VBA
21:28 Wrap it Up B!

Пікірлер: 78

  • @rpopecpa
    @rpopecpa Жыл бұрын

    Awesome, awesome, awesome!! Wow, this is the best Excel/Power Query/VBA/SQL video I've ever seen. Many thanks.

  • @AnthonySmoak

    @AnthonySmoak

    Жыл бұрын

    Thanks Ryan, you just made my day! Make sure to check out the additional videos in this series if you need a bit more flexibility. kzread.info/head/PLRNrM8SIqgQZ4u5yGSlVNeGgej1lcwLwr

  • @rpopecpa

    @rpopecpa

    Жыл бұрын

    @@AnthonySmoak I love the select all feature! I often times use multiple criteria where at least one of my criteria is select all. Nice!!

  • @chi-bucks
    @chi-bucks Жыл бұрын

    Always learning something new from you. I appreciate.

  • @AnthonySmoak

    @AnthonySmoak

    Жыл бұрын

    Thank you for watching and commenting Ubani.

  • @kaiser6242
    @kaiser6242 Жыл бұрын

    This is fantastic!

  • @msamysobih680
    @msamysobih680 Жыл бұрын

    Thanks, I have been searching for this for a long time 🤝

  • @AnthonySmoak

    @AnthonySmoak

    Жыл бұрын

    Glad you found my video useful. Make sure to watch the other videos in the playlist for the more secure stored procedure method!

  • @user-ih7ot7vl8u
    @user-ih7ot7vl8u4 ай бұрын

    Thank you for this magic tutorial. Please make more videos. ❤❤

  • @AnthonySmoak

    @AnthonySmoak

    4 ай бұрын

    Thank you!

  • @alpeshshah891
    @alpeshshah8918 ай бұрын

    Mindblowing, thank you for showing us how to combine vba and power query to get end result, opens up a greater scope now for me to produce fantastic reports.

  • @AnthonySmoak

    @AnthonySmoak

    8 ай бұрын

    I really appreciate this comment. Thank you!

  • @juanmigueltobos9301
    @juanmigueltobos9301 Жыл бұрын

    That was what I was looking for , thx !!!

  • @AnthonySmoak

    @AnthonySmoak

    Жыл бұрын

    Glad to hear it. Make sure to watch the 2nd video if you want to learn how to use a more secure method involving stored procedures. kzread.info/dash/bejne/aXuD3NCMhqWqncY.html

  • @CecLevel
    @CecLevel2 ай бұрын

    Thanks! Very easy...appreciate you man.

  • @AnthonySmoak

    @AnthonySmoak

    2 ай бұрын

    I really appreciate the SUPER thanks!!!!!! Thank you 🤜

  • @user-fc8od3fx4h
    @user-fc8od3fx4h5 ай бұрын

    Man you saved the day with this video! Thanks!!!

  • @AnthonySmoak

    @AnthonySmoak

    3 ай бұрын

    Glad it helped!

  • @matusjaniga1165
    @matusjaniga11655 ай бұрын

    Great job Anthony, It is a briliant solution :) bravo

  • @AnthonySmoak

    @AnthonySmoak

    5 ай бұрын

    Thank you for the comment!

  • @costoncheatham7025
    @costoncheatham70252 ай бұрын

    Very helpful - thanks!!

  • @AnthonySmoak

    @AnthonySmoak

    2 ай бұрын

    Glad to hear it, thanks!

  • @kareldenecker1989
    @kareldenecker19893 ай бұрын

    magic! thanks!

  • @AnthonySmoak

    @AnthonySmoak

    3 ай бұрын

    Watch the rest of the series to build upon what you learned in this vid. Thanks for the comment!

  • @jalbert9675
    @jalbert96758 ай бұрын

    I needed to know how to do this and got lucky enough to learn it from Smoak himself. To quote my favorite Russian comedian, "What a country!"

  • @AnthonySmoak

    @AnthonySmoak

    8 ай бұрын

    I know a J Albert who happens to be a "helluva engineer". Is the one in the same?

  • @jalbert9675

    @jalbert9675

    8 ай бұрын

    @@AnthonySmoak Does a Whittingham timewarp in the woods?

  • @AnthonySmoak

    @AnthonySmoak

    8 ай бұрын

    @@jalbert9675 Indeed. I'll actually be catching up with him and Terence tomorrow.

  • @jalbert9675

    @jalbert9675

    8 ай бұрын

    @@AnthonySmoak Tell those two old married farts I said hello.

  • @MJacobsen
    @MJacobsen6 ай бұрын

    Great video, thanks! Any ideas for taking it to the next step? I could really use tips on how to disable the "you are now executing code" popup, and secondly to embed username & password in the document. This way the document can be setup and then passed on to a colleague who can get new data anytime. (the SQL user embedded would of course only have readonly rights). Hope to hear from you, greetings from Denmark :)

  • @abielwubet2587
    @abielwubet258711 ай бұрын

    Greatest

  • @AnthonySmoak

    @AnthonySmoak

    11 ай бұрын

    You're too kind, thanks!

  • @avaltewarehous
    @avaltewarehous8 ай бұрын

    Thank you so much, Anthony, this technique has enabled me to make my query dynamic. However, now I am left with a new head-scratcher I wonder if you could help with. I used PowerQuery to pivot the SQL data set, and return the pivoted data into my workbook. But when I refresh the query using updated parameters, it wipes out the pivot, and just returns back to the unpivoted data table from the database. I am guessing this is because there's no code in the VBA macro to put the data into the pivoted form, but when I try to record a macro of me using PowerQuery to pivot the data set, Excel does not record anything. So I am kind of stuck here. Have you ever encountered this issue, or have any suggestions? Thanks!

  • @YorkUniversityIT
    @YorkUniversityIT10 ай бұрын

    While does work and is GREAT for certain kinds of inputs, for the specific scenario of a monthly report you would be better off inferring the date range you need based on the current date, which can be done entirely in the SQL with no need to send an parameter.

  • @AnthonySmoak

    @AnthonySmoak

    10 ай бұрын

    Thanks for the comment. Yes, SQL is certainly capable of handling flexible dates. Think of this example as a teaching exercise to showcase the parameter capabilities between Excel and SQL if needed.

  • @emmanuel.aggrey
    @emmanuel.aggrey Жыл бұрын

    please can i have this excel file to modifiy according to my need without starting from strach thank you.

  • @Daulet2009
    @Daulet2009 Жыл бұрын

    Hi, i have a question. I have a dashboard in excel that has 20mb file size. If i switch to SQL and do not store the data in several sheets does it reduce file size significantly? I mean is it possible to create excel file that don't store all necessary data inside the file and fetches the data automatically from SQL everytime when it's opened.

  • @AnthonySmoak

    @AnthonySmoak

    Жыл бұрын

    I don't know your particular case but If you have the opportunity to use a real database like SQL Server then do so. However there is a limit to how much data can be stored in Excel. Also use Power BI or Tableau for dashboarding since they are optimized to handle large datasets. In Power BI you can connect directly to the data source without importing data by using DirectQuery. support.microsoft.com/en-us/office/create-a-memory-efficient-data-model-using-excel-and-the-power-pivot-add-in-951c73a9-21c4-46ab-9f5e-14a2833b6a70

  • @kewltopix
    @kewltopix Жыл бұрын

    Curious why use vba when you can just pass the parameters to power query via a function to read the cell where the parameter values are located. Unless it’s SP which power query wants a select. Haven’t tested this method yet myself. Is there a performance gain?

  • @AnthonySmoak

    @AnthonySmoak

    Жыл бұрын

    I am not sure of the exact method you are referring to in order to get values back from SQL Server based upon Excel cell values (send me a link if you have one, thanks). But I do know there are multiple ways to accomplish what I've shown. It comes down to preference. I am just simply more comfortable with SQL and VBA code as opposed to DAX/M/ Power Query GUI methods. Willing to keep an open mind if I see an exact process.

  • @kewltopix

    @kewltopix

    Жыл бұрын

    @@AnthonySmoak the way I do it is to name a cell with a named range like cellWithDate, then create a function in power query let Source = (rangeName) => Excel.CurrentWorkbook(){[Name=rangeName]}[Content]{0}[Column1] in Source then name the function somn like fnGetDate > call the function where the value is needed like fnGetDate("cellWithDate") Change the cell value, hit refresh, no vba needed

  • @nuclear1747
    @nuclear1747 Жыл бұрын

    Do you know how to bypass problem with data format, excel cell with date is formating to UK standard and SQL Date variable needs US one?

  • @AnthonySmoak

    @AnthonySmoak

    Жыл бұрын

    Try to change the date format settings in Windows. Or perform manipulation in VBA to format the date to your liking. Watch the 3rd video in this series where change the default formatting in a SQL stored procedure.

  • @sreedharraor
    @sreedharraor Жыл бұрын

    Hi Anthony, well explained. can you help me with my scenario? Example - Take oracle sample databease tables. If I have an excel sheet with only employee IDs, how can I pull their department and location from database into excel next to employee ids

  • @AnthonySmoak

    @AnthonySmoak

    Жыл бұрын

    I'm not sure of a practical way here. You can import the sheet into Oracle and run the query at the source. Or try to format the data so it is usable for the SQL IN clause.

  • @sreedharraor

    @sreedharraor

    Жыл бұрын

    Thank you. That was just an example. In real I have a weekly task to get details for certain range of values in excel from database. I was thinking if I can use power query and just refresh weekly instead of repeating

  • @AnthonySmoak

    @AnthonySmoak

    Жыл бұрын

    @@sreedharraor See if this technique would work if you want to use Power Query and start with a list of items: kzread.info/dash/bejne/gYh2pLptaJisl5s.html

  • @sreedharraor

    @sreedharraor

    Жыл бұрын

    @@AnthonySmoak thank you. What a coincidence!!!! When I searched, I found this video and it really helped me. I’m able to bring the data using where clause in power query

  • @Immortal34345
    @Immortal34345 Жыл бұрын

    hi anthony, is there a way i can choose multiple Cities?

  • @AnthonySmoak

    @AnthonySmoak

    11 ай бұрын

    Certainly, modify the CASE statement I use in this video to handle two cities in your dataset: kzread.info/dash/bejne/eYOqsLOtlq-6eNI.html

  • @workplacevids9977
    @workplacevids99777 ай бұрын

    Please share with us this file. Thanks in advance.

  • @user-dy6ei2hr6p
    @user-dy6ei2hr6p11 ай бұрын

    Thanks for the great explanation, but where are the codes? suptest

  • @AnthonySmoak

    @AnthonySmoak

    11 ай бұрын

    Thank you! Sorry I haven't posted my specific code any place but the important part is that the technique is flexible enough that you can make up your own example.

  • @VatsalSingh007
    @VatsalSingh007 Жыл бұрын

    I did all the steps same but getting Run-time error '1004': Application-defined or object-defined error. Any idea what could be happening?

  • @AnthonySmoak

    @AnthonySmoak

    Жыл бұрын

    Make sure you are referring to the correct cells in the VBA code. Make sure your code is in a module as well.

  • @VatsalSingh007

    @VatsalSingh007

    Жыл бұрын

    @@AnthonySmoak Not sure if it is right things to do or otherwise, "ActiveWorkbook.RefreshAll" worked for me. Thanks that you took time to respond.

  • @paolo.macatangay

    @paolo.macatangay

    Жыл бұрын

    @@AnthonySmoak I also got the same Run-time error "1004': Application-defined or object-defined error". I am able to verify via message box that it gets the cell value and also it is in module as well. Not sure why

  • @MostafaElgohary-vw6tp

    @MostafaElgohary-vw6tp

    Жыл бұрын

    @@paolo.macatangay same here

  • @MostafaElgohary-vw6tp

    @MostafaElgohary-vw6tp

    Жыл бұрын

    @@AnthonySmoakSorry, I still get the Run-time error "1004': Application-defined or object-defined error"

  • @abdullahquhtani4247
    @abdullahquhtani4247 Жыл бұрын

    Good BUT😊!! I think it’ll be great if you used stored procedure instead 😏

  • @AnthonySmoak

    @AnthonySmoak

    Жыл бұрын

    Yes a stored procedure will have better protection from sql injection. It's just harder to execute in this manner. This is a quick and easy way to get it done. I'd use this technique only for personal use.

  • @abdullahquhtani4247

    @abdullahquhtani4247

    Жыл бұрын

    @@AnthonySmoak thank you 🙏🏻

  • @AnthonySmoak

    @AnthonySmoak

    Жыл бұрын

    @@abdullahquhtani4247 Just for you I am going to make another video showing how to make the call with a stored procedure. Same process just a different statement passed to the Query formula in VBA.

  • @abdullahquhtani4247

    @abdullahquhtani4247

    Жыл бұрын

    @@AnthonySmoak Thank you so much. Highly appreciated 🌹🌹👍🏼

  • @AnthonySmoak

    @AnthonySmoak

    Жыл бұрын

    As promised, make sure to like and comment! kzread.info/dash/bejne/aXuD3NCMhqWqncY.html

  • @alexanderschu6933
    @alexanderschu69333 ай бұрын

    This is the bad solution, it spams security warnings and is slow. Just use Microsoft Query as DataSource and bind your params directly to a cells. No need to code any line of VBA. Its tricky to get it running first as the tutorials of MS do not work like they say ;)

  • @AnthonySmoak

    @AnthonySmoak

    3 ай бұрын

    Make sure to watch the other two videos where we execute the SQL with a stored proc which is a bit more secure from SQL injection. Regardless, this is an ad hoc solution, not meant for serious production uses. Take it or leave it 😂

  • @alexanderschu6933

    @alexanderschu6933

    3 ай бұрын

    @@AnthonySmoak No offense meant but writing VBA with SQLs is more the Office 97 era than the current ones ;)

  • @AnthonySmoak

    @AnthonySmoak

    3 ай бұрын

    @@alexanderschu6933 Send me a link to your new high tech solution, maybe I'll make a new video for this series ;)