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:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Test #1[/TD]
[TD]4846[/TD]
[/TR]
[TR]
[TD]Test #2[/TD]
[TD]65489[/TD]
[/TR]
[TR]
[TD]Test #3[/TD]
[TD]1289[/TD]
[/TR]
</tbody>[/TABLE]


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]' "
 
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:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Test #1[/TD]
[TD]4846[/TD]
[/TR]
[TR]
[TD]Test #2[/TD]
[TD]65489[/TD]
[/TR]
[TR]
[TD]Test #3[/TD]
[TD]1289[/TD]
[/TR]
</tbody>[/TABLE]


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

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