Convert selection to SQL ANY() format

Factotum

Board Regular
Joined
May 14, 2015
Messages
118
I frequently need to convert a range of Excel data into an ANY() function format (table below) for PeopleSoft queries. Once I convert the format, I copy/paste into PeopleSoft. First column is an example of original data and the second column is the end result. Maybe I can just use custom formatting, but I haven't found the right format setting. I'm thinking of two approaches depending on the circumstance:

  • Option A: convert the values to the new format in their existing location. I've been fiddling around with something like:
Code:
Sub ANY_Function_OptionA()
  Dim r As Range
  Set r = Application.Selection
  rangeValues = r.Value
  r.Value = "'" & rangeValues & "',"
End Sub

  • Option B is to use a formula and offset to the next empty column to the right. Problem is, the next empty column isn't always 3 columns to the right as in the code below. Any suggestions on how best to offset to the next empty column? Or any suggestions of how to improve this code - I threw it together in a hurry and I know it's a bit ugly.
Code:
Sub ANY_Function_OptionB()
Range(Selection, Selection.End(xlDown)).Select
For Each c In Selection
     c.Offset(0, 3).FormulaR1C1 = "=CHAR(39)& c.value &CHAR(39)&CHAR(44)"
Next
End Sub

Sample Data. The last row (closing parenthesis) could be tacked on to 'Oranges') or put below the data in a new cell
[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD]Product[/TD]
[TD]ANY([/TD]
[/TR]
[TR]
[TD]Apples[/TD]
[TD]'Apples',[/TD]
[/TR]
[TR]
[TD]Bananas[/TD]
[TD]'Bananas',[/TD]
[/TR]
[TR]
[TD]Oranges[/TD]
[TD]'Oranges'[/TD]
[/TR]
[TR]
[TD][/TD]
[TD])[/TD]
[/TR]
</tbody>[/TABLE]

Thank you in advance for any suggestions!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
In case anyone is interested, I've come up with a solution - it's ugly and I'm sure there are better ways to do it. I realize that Select and GoTo are not good practice and I usually don't resort to them, but couldn't think how to get around it this time.

The table below shows an example of what I start with and the results after the blank column. In a nutshell, the user selects the range (Category column) and then the values are copied to a blank column to the right of the data set. Pasted values are converted to include single quotes and commas, sorted, and duplicates removed. Then "ANY( " is entered as a header and the last comma is changed to a closing parenthesis. Finally the results are copied to the clipboard so the user can paste it over to our internal system as an SQL expression.

If anyone gets really bored, I'd love suggestions on how to improve this, but it's also functional for what I need.

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Original[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Results[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Category[/TD]
[TD]Status[/TD]
[TD]Description[/TD]
[TD]blank[/TD]
[TD]ANY([/TD]
[/TR]
[TR]
[TD]141[/TD]
[TD]Active[/TD]
[TD]Contracted Cleaning[/TD]
[TD][/TD]
[TD]'141',[/TD]
[/TR]
[TR]
[TD]141[/TD]
[TD]Active[/TD]
[TD]Contracted Cleaning[/TD]
[TD][/TD]
[TD]'205',[/TD]
[/TR]
[TR]
[TD]205[/TD]
[TD]Active[/TD]
[TD]Salaries[/TD]
[TD][/TD]
[TD]'213',[/TD]
[/TR]
[TR]
[TD]213[/TD]
[TD]Inactive[/TD]
[TD]Vehicle Depreciation[/TD]
[TD][/TD]
[TD]'332',[/TD]
[/TR]
[TR]
[TD]332[/TD]
[TD]Active[/TD]
[TD]Grounds keeping[/TD]
[TD][/TD]
[TD]'463')[/TD]
[/TR]
[TR]
[TD]463[/TD]
[TD]Active[/TD]
[TD]Electricity[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]141[/TD]
[TD]Active[/TD]
[TD]Contracted Cleaning[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]205[/TD]
[TD]Active[/TD]
[TD]Salaries[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]213[/TD]
[TD]Inactive[/TD]
[TD]Vehicle Depreciation[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]141[/TD]
[TD]Active[/TD]
[TD]Contracted Cleaning[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

Here is the code for the above example:

Code:
Sub ANY_Expression()


Selection.Copy


Dim i As Long
With Application.WorksheetFunction
    For i = 1 To Columns.Count
        If .CountA(Cells(1, i).EntireColumn) = 0 Then
            Columns(i + 1).Rows("2").PasteSpecial
            Application.CutCopyMode = False
            GoTo 1
        End If
    Next i
End With



1:


Selection.Sort Key1:=Selection.Cells, Header:=xlNo


Dim c As Range


Dim Original, First, Last As String
First = Chr(39) & Chr(39)
Last = "',"



For Each c In Selection
    Original = c.Value
    c.Value = First & Original & Last
    Original = vbNullString
Next


First = vbNullString
Last = vbNullString


Selection.RemoveDuplicates Columns:=1, Header:=xlNo


ActiveCell.Offset(-1, 0).Value = "ANY("


ActiveCell.End(xlDown).Replace What:=",", Replacement:=")"


ActiveCell.Offset(-1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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