VBA SQL Reading data from a column until empty cell.

dcg38524

Board Regular
Joined
Dec 4, 2013
Messages
113
Hello,

Need your help on a VBA code modification.

We have an existing excel SQL query that works, but there are group names hard-coded which I wanted to revisit. As you know this works great if nothing changes but can be very awkward and inconvenient when changes are made frequently.

Just was a little curious on how hard it would be to change the code, I searched the web and couldn't find any clear solutions, so I am reaching out for help.

If all possible I would like to change the current code to read these group names from the existing workbook

  • Tab called "valid names"
  • Starting from column "A2"
  • Until it reaches an empty cell.

Our code currently looks like below: 'Please Don't laugh,:eeek:'
SELECT "ASSIGNED_TO_GROUP_, ASSIGNED_TO_INDIVIDUAL_, TICKET_ID_ , etc...."
FROM WAREHOUSE_MANAGEMENT
WHERE ASSIGNED_TO_INDIVIDUAL_ <> 'John'
AND ASSIGNED_TO_GROUP_ IN ( 'GROUP1' , 'GROUP2', 'GROUP3', 'GROUP4', 'GROUP5')
ORDER BY
TICKET_ID_
 
Hi Don,

SQL's IN operator will work with one item. The reason your getting an error is because if there's only one item, then the transposed arrGroups will not be an Array. The Join function requires the first argument passed to it to be an array.

One way to get your code to work with either one or multiple items is...

Code:
 arrGroups = Application.Transpose(arrGroups)
 If IsArray(arrGroups) Then
   sGroups = "'" & Join(arrGroups, "','") & "'"
 Else
   sGroups = "'" & arrGroups & "'"
 End If
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi Jerry,

Always a pleasure to hear from you sir :biggrin:. Your array modification code worked great, the SQL query now can function using one or two group entries.

I really can't thank you enough, take care.

Kind regards,
Don
 
Upvote 0

Forum statistics

Threads
1,222,622
Messages
6,167,103
Members
452,094
Latest member
Roberto Saveru

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