i don't know how to access the checkbox 'directly' but i have used a workaround.
in the worksheet - put a checkbox - do not change the default name
do not put it over cell(3,3) C3
Public Sub CheckBox1_Click()
Cells(3, 3) = CInt(CheckBox1.Value)
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
CheckBox1.Value = CBool(Cells(3, 3))
End Sub
in the
VB app (on the form put a command button, a timer, two labels and a checkbox - do not change the default names for this)
put the controls on the
vb form wherever it is good for you.
Dim wkApp As Excel.Application
Dim wkBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim fLoadingSheetValue As Boolean
'i used this flag (fLoadingSheetValue)to stop the Check1_Click()
'procedure from sending data 'to' Excel
'when i first get the data 'from' Excel.
Private Sub Form_Load()
Command1.Caption = "Excel - click me first"
Label1.Caption = "The checkbox value is "
Label2.Caption = ""
Timer1.Interval = 100
End Sub
Private Sub Check1_Click()
If fLoadingSheetValue Then Exit Sub
xlSheet.Cells(3, 3) = CInt(Check1.Value)
End Sub
Private Sub Command1_Click()
Dim fname as String
fname = App.Path & "\" & "Chktest.xls" 'rename 'Chktest.xls' to fit your needs
Set wkApp = CreateObject("Excel.Application")
Set wkBook = wkApp.Workbooks.Open(fname, , False) '(I changed it from read only)
Set xlSheet = wkBook.Sheets("Sheet1")
wkApp.Visible = True 'make the excel window visible
Label2.Caption = xlSheet.Cells(3, 3)
fLoadingSheetValue = True
Check1.Value = (xlSheet.Cells(3, 3) And vbChecked)
fLoadingSheetValue = False
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
'This timer
Label2.Caption = xlSheet.Cells(3, 3)
Check1.Value = (xlSheet.Cells(3, 3) And vbChecked)
End Sub
Make the Excel window small enough to see your
VB app form.
you can change the checkbox on the
vb form or on the Excel sheet and see the update in both places (almost at the same time)
you will get an error if you kill the Excel window before killing the
VB form -
this is because the timer is still trying to locate the Excel sheet.
The timer on the
VB form and the Worksheet_Change(...) are used just for this demo to allow the two apps to see and change values. You won't need them
if you just wish to read the checkbox from the sheet.
HTH - ask if you need more, i may be able to help.