Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 82007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:32:58+00:00 2026-05-10T21:32:58+00:00

I am regularly required to compare data sent to me in Excel spreadsheets with

  • 0

I am regularly required to compare data sent to me in Excel spreadsheets with data that lives in SQL Server. I know that you can connect SQL Server to spreadsheets but it always seemed clunky

This is really a post to show off my solution but I would love to hear other peoples ideas.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T21:32:58+00:00Added an answer on May 10, 2026 at 9:32 pm

    For best results, paste the below code into a module in your personal.xls file. You will need to add a reference to the Microsoft Forms 2.0 Object Library.

    When you run this routine, it takes the currently highlighted region and creates an XML string. It also creates the TSQL to convert that XML into a temporary table called #tmp. It also pastes the TSQL into your clipboard. It makes a lot of assumptions and the default temporary table is all VARCHAR(100).

    I bound this routine to Cntl-Shift-X.

    The end result is if i highlight a reagion (with header), click Cntl-Shift-X, and past into a query window, I have immediate access to the spreadsheet data in SQL.

    I’t has save me tons of time.

    Recommendations for improvements are welcome :o)

    Sub CreateOpenXML()      Dim cols, rows As Long     cols = Selection.Columns.Count     rows = Selection.rows.Count     Dim Header() As String     ReDim Preserve Header(cols)     For i = 1 To cols  '''Each Column In Selection.Rows(0).Columns         Header(i) = CleanHeader(Selection.Cells(1, i).Value)         'Header(i) = Application.WorksheetFunction.Substitute(CleanString(Selection.Cells(1, i).Value), ' ', '_')         'Header(i) = Application.WorksheetFunction.Substitute(Header(i), '(', '_')         'Header(i) = Application.WorksheetFunction.Substitute(Header(i), ')', '_')         'i = i + 1     Next     Dim theXML As String, tmpXML As String, counter As Integer      theXML = 'DECLARE @DocHandle int' & vbCrLf     theXML = theXML & 'DECLARE @XmlDocument varchar(8000)' & vbCrLf     theXML = theXML & 'EXEC sp_xml_preparedocument @DocHandle OUTPUT, N'<theRange>' & vbCrLf     tmpXML = ''     counter = 0     For i = 2 To rows         tmpXML = tmpXML & vbTab & '<theRow>'         For j = 1 To cols             If Selection.Cells(i, j).Text <> 'NULL' And Selection.Cells(i, j).Text <> '' Then                 tmpXML = tmpXML & '<' & Header(j) & '>' & CleanString(Selection.Cells(i, j).Text) & '</' & Header(j) & '>'                 'tmpXML = tmpXML & CleanString(Selection.Cells(i, j).Text)                 'tmpXML = tmpXML & '</' & Header(j) & '>'             End If         Next j         tmpXML = tmpXML & '</theRow>' & vbCrLf         counter = counter + 1         If counter = 200 Then             theXML = theXML & tmpXML             tmpXML = ''             counter = 0         End If     Next i     theXML = theXML & tmpXML     theXML = theXML & '</theRange>'' & vbCrLf & vbCrLf     '''theXML = theXML & 'EXEC sp_xml_preparedocument @DocHandle OUTPUT, @XmlDocument' & vbCrLf     theXML = theXML & 'SELECT '     For i = 1 To cols         theXML = theXML & '[' & Header(i) & ']'         If i <> cols Then theXML = theXML & ', '     Next     theXML = theXML & vbCrLf     theXML = theXML & 'INTO #tmp'     theXML = theXML & vbCrLf     theXML = theXML & 'FROM OPENXML (@DocHandle, '/theRange/theRow',2) WITH (' & vbCrLf     For i = 1 To cols         theXML = theXML & vbTab & '[' & Header(i) & '] varchar(100)'         If i <> cols Then theXML = theXML & ','         theXML = theXML & vbCrLf     Next     theXML = theXML & ')' & vbCrLf     theXML = theXML & 'EXEC sp_xml_removedocument @DocHandle' & vbCrLf     theXML = theXML & vbCrLf     theXML = theXML & 'Select * from #tmp' & vbCrLf     theXML = theXML & vbCrLf     theXML = theXML & '--DROP TABLE  #tmp'     theXML = theXML & vbCrLf     MsgBox 'The XML has been copied to the clipboard'     Dim dob As New DataObject     dob.SetText (theXML)     dob.PutInClipboard  End Sub  Function CleanString(orig As String)     Dim tmp As String     tmp = orig     '''MsgBox InStr(orig, '&')     If InStr(orig, '&') > 0 Or InStr(orig, ''') > 0 Or InStr(orig, '<') > 0 Or InStr(orig, '>') > 0 Or InStr(orig, '''') > 0 Then         tmp = Application.WorksheetFunction.Substitute(tmp, '&', '&amp;')         tmp = Application.WorksheetFunction.Substitute(tmp, ''', '&apos;')         tmp = Application.WorksheetFunction.Substitute(tmp, '<', '&lt;')         tmp = Application.WorksheetFunction.Substitute(tmp, '>', '&gt;')         tmp = Application.WorksheetFunction.Substitute(tmp, '''', '&quot;')     End If     CleanString = tmp  End Function  Function CleanHeader(orig As String)     Dim tmp As String     tmp = Trim(orig)     If InStr(orig, ' ') > 0 Or InStr(orig, '(') > 0 Or InStr(orig, ')') > 0 Or InStr(orig, '$') > 0 Or InStr(orig, '/') > 0 Or InStr(orig, '?') > 0 Or InStr(orig, '&') > 0 Or InStr(orig, ''') > 0 Or InStr(orig, '<') > 0 Or InStr(orig, '>') > 0 Or InStr(orig, '''') > 0 Then         tmp = Application.WorksheetFunction.Substitute(tmp, '&', 'And')         tmp = Application.WorksheetFunction.Substitute(tmp, ''', '_')         tmp = Application.WorksheetFunction.Substitute(tmp, '<', '')         tmp = Application.WorksheetFunction.Substitute(tmp, '>', '')         tmp = Application.WorksheetFunction.Substitute(tmp, '''', '')         tmp = Application.WorksheetFunction.Substitute(tmp, ' ', '_')         tmp = Application.WorksheetFunction.Substitute(tmp, '(', '_')         tmp = Application.WorksheetFunction.Substitute(tmp, ')', '_')         tmp = Application.WorksheetFunction.Substitute(tmp, '$', '')         tmp = Application.WorksheetFunction.Substitute(tmp, '/', '')         tmp = Application.WorksheetFunction.Substitute(tmp, '?', '')     End If     CleanHeader = tmp  End Function  Sub MakeText()      ActiveCell.CurrentRegion.Select     Dim rng As Range     Set rng = Selection      Dim str As String     For i = 1 To rng.rows.Count         For j = 1 To rng.Columns.Count             str = Application.WorksheetFunction.Text(rng.Cells(i, j).Value, '#')             rng.Cells(i, j).NumberFormat = '@'             rng.Cells(i, j).Value = str         Next j     Next i  End Sub 

    As suggested, here’s an example. Consider this spreadsheet data:

    Name              DOB       Score   Comment John Smith        7/1/1990  93      Great effort Sue Jones         1/1/1989  95      Super achievement Robin Sixpack     12/1/1985 100     OK 

    This method will generate the following TSQL:

    DECLARE @DocHandle int DECLARE @XmlDocument varchar(8000) EXEC sp_xml_preparedocument @DocHandle OUTPUT, N'<theRange>     <theRow><Name>John Smith</Name><DOB>7/1/1990</DOB><Score>93</Score><Comment>Great effort</Comment></theRow>     <theRow><Name>Sue Jones</Name><DOB>1/1/1989</DOB><Score>95</Score><Comment>Super achievement</Comment></theRow>     <theRow><Name>Robin Sixpack</Name><DOB>12/1/1985</DOB><Score>100</Score><Comment>OK</Comment></theRow> </theRange>'  SELECT [Name], [DOB], [Score], [Comment] INTO #tmp FROM OPENXML (@DocHandle, '/theRange/theRow',2) WITH (     [Name] varchar(100),     [DOB] varchar(100),     [Score] varchar(100),     [Comment] varchar(100) ) EXEC sp_xml_removedocument @DocHandle  Select * from #tmp  --DROP TABLE  #tmp 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 117k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer class Program { static void Main(string[] args) { Timer timer… May 11, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer Use GROUP_CONCAT SELECT GROUP_CONCAT(bar) FROM TABLE GROUP BY foo; May 11, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer Your code doesn't work because the function is not returning… May 11, 2026 at 10:50 pm

Related Questions

Casus: How to edit and advance with the same code, from several distanced locations
I have switched to Vista recently and I wanted to keep UAC turned on,
I have a copy of emacs that I use on a couple of different
I am hoping that someone can help me with a problem I've got at

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.