While trying to export my tables from Access to Excel via `VB.NET`

, I want TRUE and FALSE values to appear as 1s an 0s in my excel sheet. Below is my code to export and write the data in excel.

`If(.Cells(d, e).value = True, 1, 0)`

throws an error ‘`conversion from type string to Boolean is not valid`

‘. I guess this is because I have ‘string’ data as well in my Access table. Can someone please help.

```
Dim e As Integer = 1
For col = 0 To ComDset2.Tables(0).Columns.Count - 1
d = 2
For row = 0 To ComDset2.Tables(0).Rows.Count - 1
.Cells(d, e).Value = ComDset2.Tables(0).Rows(row).ItemArray(col)
IIf(.Cells(d, e).value = True, 1, 0)
d += 1
Next
e += 1
Next
```

As mentioned by @varocarbas in the comment below your question, you will realise what the error in your code is.

The IIf(.Cells(d, e).value = True, 1, 0) line assumes that the Cell value is of Boolean type what is not the case (Excel cells are always String). The way to avoid this problem is either treating the cell as a string (IIf(.Cells(d, e).value.ToString().ToLower() = “true”, 1, 0)) or converting the cell into Boolean (via Convert.ToBoolean

However, instead of replacing that in the loop do it outside the loop in ONE GO.

**TRIED AND TESTED IN VB.NET 2010 + EXCEL 2010 (Sigh)**

```
rng.Replace(What:="TRUE", Replacement:="1", LookAt:=Excel.XlLookAt.xlWhole, _
SearchOrder:=Excel.XlSearchOrder.xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False)
rng.Replace(What:="FALSE", Replacement:="0", LookAt:=Excel.XlLookAt.xlWhole, _
SearchOrder:=Excel.XlSearchOrder.xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False)
```

**More FollowUp from Comments**

See this example

```
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'~~> Define your Excel Objects
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlWorkBook = xlApp.Workbooks.Open("C:\Sample.xlsx")
xlWorkSheet = xlWorkBook.Sheets(1)
Dim i As Integer = 1
Dim d As Integer = 0
Dim startRow As Integer = 0
With xlWorkSheet
For col = 0 To ComDset2.Tables(0).Columns.Count - 1
d = 2
startRow = d
For Row = 0 To ComDset2.Tables(0).Rows.Count - 1
.Cells(d, e).Value = ComDset2.Tables(0).Rows(Row).ItemArray(col)
d += 1
Next
'~~> Create your range here
Dim rng As Excel.Range = .Range(.Cells(startRow, i), .Cells(d - 1, i))
rng.Replace(What:="TRUE", Replacement:="1", LookAt:=Excel.XlLookAt.xlWhole, _
SearchOrder:=Excel.XlSearchOrder.xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False)
i += 1
Next
End With
End Sub
End Class
```

Tags: excel, vb.net, vb.net.net