Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA 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 April 14th, 2008, 03:48 AM
Authorized User
 
Join Date: Mar 2008
Posts: 74
Thanks: 2
Thanked 0 Times in 0 Posts
Send a message via ICQ to sektor
Default Looping thru CheckBoxes fails

The aim of my code is:
1) loop thru all CheckBoxes in ActiveSheet;
2) if CheckBox is checked (i.e. its Value property is True), then we add its Index to array vArray.
Here's the code:

1 Dim i As Integer
2 Dim vArray() As Variant
3 Dim cb As CheckBox
4 i = 1
5 For Each cb In ActiveSheet.OLEObjects
6 ReDim vArray(1 To i)
7 vArray(i) = cb.Index
8 i = i + 1
9 Next

VBA shows this error: Type mismatch in line 5. What's wrong?


 
Old April 14th, 2008, 05:51 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Check if the following works out:

Sub Chkbxes()



 Dim i As Integer
 Dim vArray() As Variant
 Dim cb As OLEObject
 i = 1
 For Each cb In ActiveSheet.OLEObjects
    If cb.progID = "Forms.CheckBox.1" Then
        ReDim vArray(1 To i)
        vArray(i) = cb.Index
        i = i + 1
    End If
 Next
End Sub

Cheers
Shasur

http://www.dotnetdud.blogspot.com

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old April 14th, 2008, 06:07 AM
Authorized User
 
Join Date: Mar 2008
Posts: 74
Thanks: 2
Thanked 0 Times in 0 Posts
Send a message via ICQ to sektor
Default

Shasur, thanks a lot!!!
But there was one error which I mentioned and corrected myself: you should use Preserve keyword after ReDim. When I corrected it, things went right :) Thanks once again!

 
Old April 14th, 2008, 06:19 AM
Authorized User
 
Join Date: Mar 2008
Posts: 35
Thanks: 0
Thanked 1 Time in 1 Post
Default

Make it
ReDim Preserve vArray(1 To i)
in order for the array items to be preserved through the consecutive ReDims
 
Old April 14th, 2008, 06:25 AM
Authorized User
 
Join Date: Mar 2008
Posts: 35
Thanks: 0
Thanked 1 Time in 1 Post
Default

Oh Ok, I see you got it yourself! Good work sektor!
Regards

 
Old April 14th, 2008, 06:50 AM
Authorized User
 
Join Date: Mar 2008
Posts: 74
Thanks: 2
Thanked 0 Times in 0 Posts
Send a message via ICQ to sektor
Default

And what about adding some condition? This code generates error 'cause VBA can't recognize Index property of CheckBox (line 7):

1 Dim ole As OLEObject
2 Dim vArray() As Variant
3
4 For Each ole In ActiveSheet.OLEObjects
5 If TypeName(ole.Object) = "CheckBox" Then
6 ReDim Preserve vArray(1 To i)
7 vArray(i) = ole.Object.Index
8 i = i + 1
9 End If
10 Next



 
Old April 14th, 2008, 07:14 AM
Authorized User
 
Join Date: Mar 2008
Posts: 35
Thanks: 0
Thanked 1 Time in 1 Post
Default

Dim ole As OLEObject
Dim i As Integer
Dim vArray() As Variant
i = 1
For Each ole In ActiveSheet.OLEObjects
   If TypeName(ole.Object) = "CheckBox" Then
      ReDim Preserve vArray(1 To i)
      vArray(i) = ole.Index
      i = i + 1
   End If
Next


 
Old April 14th, 2008, 07:42 AM
Authorized User
 
Join Date: Mar 2008
Posts: 74
Thanks: 2
Thanked 0 Times in 0 Posts
Send a message via ICQ to sektor
Default

I seem to slept few :) I forgot to write "i=1". Now all works fine! Thanks a lot!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Fails To Update sirmilt BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 12 May 8th, 2008 11:16 AM
This statement fails! pankaj_daga Oracle 1 July 6th, 2004 02:38 PM
xp_sendmail fails rvw SQL Server 2000 1 December 9th, 2003 06:25 PM





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