-4

I have populated a column of check boxes, and attempted to set the value of them to true like so:

worksheet.CheckBoxes("Check").Value = True

This is my code:

Public Sub AddCheckboxColumnToSpreadsheet(filePath As String)
    ' Create Excel application
    Dim excelApp As New Excel.Application
    excelApp.Visible = False

    ' Open the workbook
    Dim workbook As Excel.Workbook = excelApp.Workbooks.Open("C:\Desktop\Test.xlsx")
    Dim worksheet As Excel.Worksheet = workbook.Sheets(1)

    ' Find the last used column and row
    Dim lastCol As Integer = worksheet.Cells(1, worksheet.Columns.Count).End(Excel.XlDirection.xlToLeft).Column
    Dim lastRow As Integer = worksheet.Cells(worksheet.Rows.Count, 1).End(Excel.XlDirection.xlUp).Row

    ' Insert a header for the checkbox column
    worksheet.Cells(1, lastCol + 1).Value = "Check"

    ' Loop through each row and add a checkbox
    For row As Integer = 2 To lastRow
      Dim leftPos As Double = worksheet.Cells(row, lastCol + 1).Left + 2
      Dim topPos As Double = worksheet.Cells(row, lastCol + 1).Top + 2
      Dim cbWidth As Double = worksheet.Cells(row, lastCol + 1).Width - 4
      Dim cbHeight As Double = worksheet.Cells(row, lastCol + 1).Height - 4

      worksheet.CheckBoxes.Add(leftPos, topPos, cbWidth, cbHeight).Select()

      worksheet.CheckBoxes("Check").Value = True


      excelApp.Selection.Caption = ""

    Next

    ' Save and close
    workbook.Save()
    workbook.Close(False)
    excelApp.Quit()

    ' Release COM objects
    ReleaseComObject(worksheet)
    ReleaseComObject(workbook)
    ReleaseComObject(excelApp)
End Sub
3
  • What is the problem? Commented Aug 18 at 10:26
  • This part of code does not work, the checkbox is un-check and I would like it checked: worksheet.CheckBoxes("Check").Value = True Commented Aug 18 at 10:49
  • Why not use the new checkbox-in-cell? Commented Aug 18 at 12:14

1 Answer 1

0

For one box, use:

'Check box:
worksheet.CheckBoxes("Check").Value = 1
'Uncheck box:
worksheet.CheckBoxes("Check").Value = -4146

But you are adding multiple boxes, so:

    ' Find the last used column and row
    Dim lastCol As Integer = worksheet.Cells(1, worksheet.Columns.Count).End(Excel.XlDirection.xlToLeft).Column
    Dim lastRow As Integer = worksheet.Cells(worksheet.Rows.Count, 1).End(Excel.XlDirection.xlUp).Row

    ' Insert a header for the checkbox column
    worksheet.Cells(1, lastCol + 1).Value = "Check"

    ' Loop through each row and add a checkbox
    For row As Integer = 2 To lastRow
      Dim leftPos As Double = worksheet.Cells(row, lastCol + 1).Left + 2
      Dim topPos As Double = worksheet.Cells(row, lastCol + 1).Top + 2
      Dim cbWidth As Double = worksheet.Cells(row, lastCol + 1).Width - 4
      Dim cbHeight As Double = worksheet.Cells(row, lastCol + 1).Height - 4

      With worksheet.CheckBoxes.Add(leftPos, topPos, cbWidth, cbHeight)
          .Value = 1
      End With


      excelApp.Selection.Caption = ""

    Next

    ' Save and close
    workbook.Save()
    workbook.Close(False)
    excelApp.Quit()

    ' Release COM objects
    ReleaseComObject(worksheet)
    ReleaseComObject(workbook)
    ReleaseComObject(excelApp)
End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @jkpieterse this does work. However, to the right of the checkbox in every cell is the text 'check b' do you know why this is?
I have added the following to remove 'check b': .Characters.Text = ""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.