title: VBA excel error converting the nvarchar value to int
body: <p>I created a excel VBA code to get data from the server. It worked very well over a year. However, somehow one of the criteria has been changed about "refused name(no name)" customers. Before we used to assign customer number for them automatically, but with the new changes the system does not assign any customer number customer numbers for those customer instead it gives them "R/N" code. Therefore now I have some problem extracting data from server. I tried to apply some of those "by-pass techniques that I found on net but they did not work for me (talking about CAST). 
      case WHEN isnumeric([CustId]) THEN CAST([CustId] AS int) else 0 end</p>
  
  <p>but it did not work. I get this error:"Run-time error ' -2147217913 (80040e07) 
  [Microsoft] [ODBC SQL Server Driver][SQL Server] Conversation failed 
  when converting the nvarchar value 'R/Nto data type int. "</p>
  
  <p>All I need is disregard those "R/N" in customer id field. Please have a look at my vba code below.
   Thank you in advance
   Baha</p>
  
  <pre><code>Private Sub Worksheet_Change(ByVal Target As Range)
  Dim DBFullName, TableName As String
  Dim TargetRange As Range
  Dim Conn As ADODB.Connection, intColIndex As Integer
  Dim cel As Range
  Dim TD As Double
  Dim TD2 As Double
  Dim qdate As Double
  Dim qdate2 As Double
  Dim lastrow As Long
  Dim custid 'As String+
  
  
  TD = DateSerial(Year(Now()), Month(Now()), 1)
  TD2 = Target.Offset(-4, -2).Value
  custid = Range("custid4").Text
  
  Target.Cells(1, 1).Select
  Selection.Activate
  Set TargetRange = Range("custid4").Offset(2, 0)
  Set Conn = New ADODB.Connection
  
  Conn.Open "driver={SQL Server};" &amp; _
  "server=XXXX01\RPTDB;database=Reporting_ODS;"
  
  Set RecSet = New Recordset
  RecSet.Open "SELECT TOP(1)Baha_PM.Lname +' '+ Baha_PM.Fname FROM Reporting_ODS.TG.Baha_PM Baha_PM " &amp; _
  "WHERE Baha_PM.CustId='" &amp; custid &amp; "'", Conn, , , adCmdText
  TargetRange.CopyFromRecordset RecSet
  
  RecSet.Close
  Set RecSet = Nothing
  Conn.Close
  ' Trip
  
  Set TargetRange = Range("custid4").Offset(14, 0)
  Set Conn = New ADODB.Connection
  
  Conn.Open "driver={SQL Server};" &amp; _
  "server=XXXX01\RPTDB;database=Reporting_ODS;"
   Set RecSet = New Recordset
  
  RecSet.Open "SELECT MAX(Baha_PM.Trip_Id)  FROM Reporting_ODS.TG.Baha_PM Baha_PM " &amp; _
  "WHERE Baha_PM.CustId='" &amp; custid &amp; "'", Conn, , , adCmdText
  TargetRange.CopyFromRecordset RecSet
  
  
  RecSet.Close
  Set RecSet = Nothing
  Conn.Close
  Set Conn = Nothing
  
  End Sub
  </code></pre>