需求: Excel 中有多組checkbox復(fù)選框,需要把選中的復(fù)選框和未選中的復(fù)選框標(biāo)記入庫。
如果選中則給1沒有選中給所在單元格賦0
先上圖:
VB :
- Sub btn_onclick()
- Set myDocument = Worksheets(1) ' 即 Worksheets("Sheet1")
- Dim i As Integer
- Debug.Print "count:" & myDocument.Shapes.Count
- For i = 1 To myDocument.Shapes.Count
- If InStr(1, myDocument.Shapes(i).Name, "Check Box") Then
- Dim addr As String
- Dim irow1 As Integer
- Dim iCol1 As Integer
- addr = myDocument.Shapes(i).TopLeftCell.Address
- irow1 = myDocument.Shapes(i).TopLeftCell.Row
- iCol1 = myDocument.Shapes(i).TopLeftCell.Column
- irow1 = irow1 + 1 '如果出現(xiàn)錯(cuò)位可以自行調(diào)整,不支持合并單元格的情況
- Debug.Print "addr:" & addr & "=row:" & irow1 & "=Col:" & iCol1
- Dim b As String
- b = myDocument.Shapes(i).DrawingObject.Value
- Debug.Print "is checked :" & b
- If b = 1 Then
- '根據(jù)實(shí)際情況看看addr是不是能直接取到值
- 'myDocument.Range(addr).Value = 1
- myDocument.Range(Cells(irow1, iCol1), Cells(irow1, iCol1)).Value = 1
- Else
- myDocument.Range(Cells(irow1, iCol1), Cells(irow1, iCol1)).Value = 0
- End If
- 'Debug.Print "ok..."
- End If
- Next
- MsgBox "complate!"
- End Sub
備注:
'Sheet1.Range("G1:I16, B1:C5").Select'Dim rng As Range'Dim objexcel As Excel.Application'Set rng = Sheet1.Range("H9")'Dim rng As Range'Set rng = Sheet1.Range("A65536").End(xlUp)'Sheet1.OLEObjects("CheckBox1").Object.Value = 1'Worksheets("Sheet1").Shapes.SelectAll
參考資料:
http://club.excelhome.net/thread-395683-1-1.html
http://www.feiesoft.com/vba/excel/xlobjSheets.htm
聯(lián)系客服