Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88868

Create SQL query that contains values from Excel column

$
0
0

So I have an issue, i have a column with 1000+ values, and i need to create macro that takes all of the cells values and pass them to SQL query like "select * from table where name in ('here is values from column separates with comma')". Another issue that this query can contain only 500 values, and if the column contains 1000+ values query have to be split like "select * ...500 values...union all select * ....500 values " and so on. Here is some code i already have:

Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim queryString As String
Dim queryString2 As String
Dim queryString3 As String



Set wbBook = ActiveWorkbook
Set wsSheet = wbBook.Worksheets("Sheet1")
queryString = "select * from table where name in ("

With wsSheet

  Range("B2").Select

  Do Until IsEmpty(ActiveCell) And IsEmpty(ActiveCell.Offset(1, 0))

     i = ActiveCell.Value
     queryString = queryString & "'"& i & "',"

     ActiveCell.Offset(2, 0).Select

  Loop
  wsSheet.Range("N1") = queryString
End With

Thanks in advance for your help.


Viewing all articles
Browse latest Browse all 88868


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>