Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 61
Default CustomProperties

Hi Everyone:

I have created a custom property very similar to the example in the help
section of excel. Now, I want to change its value, and I get an error. Can
someone show me how I can make the change.

To create the custom property (works fine):
ActiveSheet.CustomProperties.Add "cmb1", 125

To change the value ( I get the error):
ActiveSheet.CustomProperties.items(1).Value = 2000

So, for the CustomProperty named "cmb1", I want to change the value from 125
to 2000. Thanks for all your help.

Bob


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default CustomProperties

Bob,

I've found CustomProperties to be a bit flakey, especially when trying
to access a property by name. I use a function called IndexOfProperty
that returns the numerical index into CustomProperties of the
specifiied property name. For example,

Dim N As Long
Dim S As String
N=IndexOfProperty(Worksheets("Sheet1"),"PropName")
With Worksheets("Sheet1").CustomProperties
If N<0 Then
S=.Item(N).Value
MsgBox "Property 'PropName' has a value of " & Cstr(S)
Else
Msgbox "Property 'PropName' not foundl."
End If
End With

The complete IndexOfProperty is show below:

Function IndexOfProperty(WS As Worksheet, PropertyName As String) As
Long
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
' IndexOfProperty
' Returns the 1-based index number corresponding to a CustomProperty
' of worksheet WS with a property name equal to PropertyName. Returns
' 0 if property name was not found.
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
Dim N As Long
With WS.CustomProperties
For N = 1 To .Count
If StrComp(.Item(N).Name, PropertyName, vbTextCompare) = 0
Then
IndexOfProperty = N
Exit Function
End If
Next N
End With
IndexOfProperty = 0 ' not found

End Function

The following coide illustrates deleting, creating, modifying, and
retrieving a CustomProperty:

Sub AAA()

Dim WS As Worksheet
Dim Props As Object
Dim PropName As String
Dim PropValue As Variant
Dim N As Long

Set WS = ActiveSheet
' delete property. ignore error if it doesn't exist.
On Error Resume Next
N = IndexOfProperty(WS, "TheProp")
If N < 0 Then
WS.CustomProperties(N).Delete
End If
On Error GoTo 0

' add a new property with a value
WS.CustomProperties.Add Name:="TheProp", Value:=12345

' change the property value
N = IndexOfProperty(WS, "TheProp")
If N < 0 Then
WS.CustomProperties(N).Value = 1357
End If


' retrieve the property
PropName = "TheProp"
PropValue = WS.CustomProperties(N).Value
Debug.Print PropValue
End Sub

Cordially,
Chip Pearson
Mirosoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
The San Diego Project Group, LLC
(email is on the web site)
USA Central Daylight Time (-5:00 GMT)

On Tue, 23 Sep 2008 13:32:52 -0700, "Bob" wrote:

Hi Everyone:

I have created a custom property very similar to the example in the help
section of excel. Now, I want to change its value, and I get an error. Can
someone show me how I can make the change.

To create the custom property (works fine):
ActiveSheet.CustomProperties.Add "cmb1", 125

To change the value ( I get the error):
ActiveSheet.CustomProperties.items(1).Value = 2000

So, for the CustomProperty named "cmb1", I want to change the value from 125
to 2000. Thanks for all your help.

Bob

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 61
Default CustomProperties

Thank you Chip. Your code and example showed me a lot of things. Thanks
for your help.

Bob

"Chip Pearson" wrote in message
...
Bob,

I've found CustomProperties to be a bit flakey, especially when trying
to access a property by name. I use a function called IndexOfProperty
that returns the numerical index into CustomProperties of the
specifiied property name. For example,

Dim N As Long
Dim S As String
N=IndexOfProperty(Worksheets("Sheet1"),"PropName")
With Worksheets("Sheet1").CustomProperties
If N<0 Then
S=.Item(N).Value
MsgBox "Property 'PropName' has a value of " & Cstr(S)
Else
Msgbox "Property 'PropName' not foundl."
End If
End With

The complete IndexOfProperty is show below:

Function IndexOfProperty(WS As Worksheet, PropertyName As String) As
Long
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
' IndexOfProperty
' Returns the 1-based index number corresponding to a CustomProperty
' of worksheet WS with a property name equal to PropertyName. Returns
' 0 if property name was not found.
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
Dim N As Long
With WS.CustomProperties
For N = 1 To .Count
If StrComp(.Item(N).Name, PropertyName, vbTextCompare) = 0
Then
IndexOfProperty = N
Exit Function
End If
Next N
End With
IndexOfProperty = 0 ' not found

End Function

The following coide illustrates deleting, creating, modifying, and
retrieving a CustomProperty:

Sub AAA()

Dim WS As Worksheet
Dim Props As Object
Dim PropName As String
Dim PropValue As Variant
Dim N As Long

Set WS = ActiveSheet
' delete property. ignore error if it doesn't exist.
On Error Resume Next
N = IndexOfProperty(WS, "TheProp")
If N < 0 Then
WS.CustomProperties(N).Delete
End If
On Error GoTo 0

' add a new property with a value
WS.CustomProperties.Add Name:="TheProp", Value:=12345

' change the property value
N = IndexOfProperty(WS, "TheProp")
If N < 0 Then
WS.CustomProperties(N).Value = 1357
End If


' retrieve the property
PropName = "TheProp"
PropValue = WS.CustomProperties(N).Value
Debug.Print PropValue
End Sub

Cordially,
Chip Pearson
Mirosoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
The San Diego Project Group, LLC
(email is on the web site)
USA Central Daylight Time (-5:00 GMT)

On Tue, 23 Sep 2008 13:32:52 -0700, "Bob" wrote:

Hi Everyone:

I have created a custom property very similar to the example in the help
section of excel. Now, I want to change its value, and I get an error.
Can
someone show me how I can make the change.

To create the custom property (works fine):
ActiveSheet.CustomProperties.Add "cmb1", 125

To change the value ( I get the error):
ActiveSheet.CustomProperties.items(1).Value = 2000

So, for the CustomProperty named "cmb1", I want to change the value from
125
to 2000. Thanks for all your help.

Bob



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
WorkSheet customproperties Nay Excel Programming 1 September 11th 07 04:40 PM
Worksheet.CustomProperties Derrick[_3_] Excel Programming 4 April 2nd 05 06:25 PM
providing a sheet-copy event or copy CustomProperties Carlos Cortes Excel Programming 2 November 11th 04 08:24 AM


All times are GMT +1. The time now is 11:45 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"