简体   繁体   中英

How can I tick an Excel checkbox from VBA Access

I have opened an Excel file via VBA Access and can read and write in the Excel Cells. How can I tick an Excel Ckeck Box?

My code:

Dim Excel_App  As Object
Dim strExcel As String
Set Excel_App = CreateObject("Excel.Application")
Excel_App.Visible = True
Excel_App.Workbooks.Open fready
With Excel_App
    .CheckBox3.Value = True 'This line is used in VBA Excel and I need in  Access
End With

Checkboxes belong to a specific collection on a specific Worksheet . Which collection this is, depends on the type of control you're looking for.

Your code is written as if the checkbox belonged to the Excel.Application object - that can't work.

First you need to keep a reference to that Workbook object you're opening, so instead of this:

 Excel_App.Workbooks.Open fready 

You need this:

Dim book As Object ' early-bound: As Excel.Workbook
Set book = Excel_App.Workbooks.Open(fready)

If you don't know which worksheet to find the checkbox on, you'll have to iterate the workbook's Worksheets collection:

Dim sheet As Object ' early-bound: As Excel.Worksheet
For Each sheet In book.Worksheets
    'todo
Next

Form Controls

So we're looking for a CheckBox form control . We'll find that in the sheet's Shapes collection, and we'll know we're looking at a form control when its Type is msoFormControl ; we'll know that it's a checkbox control when its FormControlType property returns xlCheckBox :

Dim sheet As Object ' early-bound: As Excel.Worksheet
For Each sheet In book.Worksheets
    Dim shp As Object ' early-bound: As Excel.Shape
    For Each shp In sheet.Shapes
        If shp.Type = 8 ' early-bound: msoFormControl
            If shp.FormControlType = 1 ' early-bound: xlCheckBox
                'todo
            End If
        End If
    Next
Next

So now we know shp is a checkbox form control . The Value is accessible through the ControlFormat object/property, so you can set the value of a checkbox named Check Box 1 (that's the default name) like this:

If shp.Name = "Check Box 1" Then
    shp.ControlFormat.Value = 1 'checked
End If

Of course if you already know which specific sheet you're looking for, there's no need to iterate them all.

ActiveX Controls

If the control is an ActiveX control, it's a whole different story; you'll find it in the OLEObjects collection instead, which contains OLEObject instances, which have an Object property that returns the MSForms.CheckBox object; you can get the name of the checkbox from the OLEObject.ShapeRange.Name :

Dim ctrl As Object ' early-bound: As Excel.OLEObject
For Each ctrl In sheet.OLEObjects
    If TypeName(ctrl.Object) = "CheckBox" Then ' early-bound: If TypeOf ctrl.Object Is MSForms.CheckBox Then
        If ctrl.ShapeRange.Name = "CheckBox1" Then
            ctrl.Object.Value = True ' checked
        End If
    End If
Next

Note that the early-bound TypeOf ctrl.Object Is MSForms.CheckBox check is MUCH more robust than the late-bound TypeName check. You need to reference the MSForms type library through Tools > References to use it (it's already referenced if your VBA project has any UserForm component, in which case early-bound code is a no-brainer).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM