Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
|
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 27th, 2006, 04:36 PM
Registered User
 
Join Date: Jan 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to access checkbox on a Excel sheet with VB

In my VB code, I open an Excel workbook and try to get the value of a checkbox on Sheet1. Assume the name of the checkbox is "newUser".

Set wkApp = CreateObject("Excel.Application")
Set wkBook = wkApp.Workbooks.Open(fname, , True) 'read only
Set xlSheet = wkBook.Sheets("Sheet1")

In VBA code, I can simply do this,
If xlsheet.newUser.value="True" then....

But how to access checkbox in VB???

Please help.

 
Old October 18th, 2006, 08:58 PM
Registered User
 
Join Date: Oct 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
access to excel sheet conversion? ramniwas Visual Basic 2005 Basics 2 April 6th, 2008 08:38 AM
Insert Excel sheet content into Access table from stepdev Access VBA 2 December 8th, 2006 04:18 PM
Importing Excel sheet into Access using ASP prasanta2expert ASP.NET 1.0 and 1.1 Basics 1 October 5th, 2006 02:36 PM
Clipboard to excel sheet thorugh VB Kaustav Pro VB 6 0 March 20th, 2006 06:54 AM
Update Access data from excel sheet Swift_PC Access VBA 1 October 20th, 2004 01:51 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.