Remove hard-coding from macro/SQL

shre0047

Board Regular
Joined
Feb 3, 2017
Messages
53
Office Version
  1. 365
Platform
  1. Windows
Currently, I have a macro report to pull tests from a database which is hard-coded and would like it so the end user can change the values on the tests in Sheet1 and the formula will change.

Example:
Sheet1:
Test #14846
Test #265489
Test #31289

<tbody>
</tbody>


Within the macro where it extracts it from the database via SQL query, I have it hard-coded but would like to change it so it pulls it via the values of column B in 'Sheet1'

Code:
SQLquery = SQLquery & " Where t.TS_TEST_ID = '[B]4846[/B]' OR t.TS_TEST_ID = '[B]65489[/B]' OR t.TS_TEST_ID = '[B]1289[/B]' "
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Currently, I have a macro report to pull tests from a database which is hard-coded and would like it so the end user can change the values on the tests in Sheet1 and the formula will change.

Example:
Sheet1:
Test #14846
Test #265489
Test #31289

<tbody>
</tbody>


Within the macro where it extracts it from the database via SQL query, I have it hard-coded but would like to change it so it pulls it via the values of column B in 'Sheet1'

Code:
SQLquery = SQLquery & " Where t.TS_TEST_ID = '[B]4846[/B]' OR t.TS_TEST_ID = '[B]65489[/B]' OR t.TS_TEST_ID = '[B]1289[/B]' "

Try:
Code:
Dim Test1 as Long
Dim Test2 as Long
Dim Test3 as Long
Test1 = Sheets("Sheet1").Range("B2").value
Test2 = Sheets("Sheet1").Range("B3").value
Test3 = Sheets("Sheet1").Range("B4").value

SQLquery = SQLquery & " Where t.TS_TEST_ID = '[B]" & Test1 & "[/B]' OR t.TS_TEST_ID = '[B]" & Test2 & "[/B]' OR t.TS_TEST_ID = '[B]" & Test3 & "[/B]' "
 
Last edited:
Upvote 0
This worked perfectly and also works when B4 or any of the cells doesn't have a value. Thank you!
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top