Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Store variable in excel-file

Status
Not open for further replies.

wmbb

Technical User
Jul 17, 2005
320
NL
Is it possible to store a variable inside an excel-file to use this var when a file is re-opened.
I want to store the unlocked cells is a range and subsequently lock these cells for editing.
But I want to keep the range for future use to restore the cell-protection.
 
Can't you use a named range ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you for your response.
I was also thinking of that and for this purpose it is possible.
But if there is a possibility to store variables into the file I can also use this for other purposes.
So the question is more general then for this specific case.
 
You may store any values in an hidden sheet.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Is this what you mean?
Code:
Dim myVar
myVar = worksheet($Sheet1).range("A1").text

Never knock on Death's door: ring the bell and run away! Death really hates that!
 
Thank for pointing me in the right direction.
I think I will use this suggestion.
 
Since this would appear to be an application configuration setting, the registry might also be a suitable location (although obviously the setting would not persist if you sent the spreadsheet to another user)
 
You can also directly use names to store values, after setting the name's visibility to false it's not visible for the user.

combo
 
Hi Combo,

What exactly do you mean with "directly use names to store values" ?
Can you give an example of code to store a variable using names ?
Maybe this is the way to go....
 
An example with hiding name for the user:
Code:
With ThisWorkbook
    .Names.Add Name:="VariableInName", RefersTo:="=0.5"
    .Names("VariableInName").Visible = False
    MsgBox Evaluate(.Names("VariableInName").RefersTo)
    .Names("VariableInName").RefersTo = "=""Sample text"""
    MsgBox Evaluate(.Names("VariableInName").RefersTo)
End With

combo
 
I should really have said CustomDocumentProperties or CustomProperties. Either would do.

Here's an example of using CustomProperties:

Code:
[blue]Public Sub wombat()
    If GetCustomProperty("varExample") Is Nothing Then ActiveSheet.CustomProperties.Add "varExample", 5

    MsgBox GetCustomProperty("varExample").Value [green]' could rely on default property, but let's be explicit[/green]
    GetCustomProperty("varExample").Value = "An example"
    MsgBox GetCustomProperty("varExample").Value    
End Sub

Public Function GetCustomProperty(strPropertyName As String, Optional wshTarget As Worksheet) As CustomProperty
    Dim item As Variant
    If wshTarget Is Nothing Then Set wshTarget = ActiveSheet
    For Each item In wshTarget.CustomProperties
        If UCase(item.Name) = UCase(strPropertyName) Then
            Set GetCustomProperty = item
            Exit For
        End If
    Next
End Function[/blue]
 
I wanted to use this code but an error ocurred "object required"
What is wrong with this code ?
It seems the ActiveDocument is empty but how to solve this ?

Code:
Sub start()
If ReadProp("protection") = "" Then AddProp "protection", "false" Else WriteProp "protection", "true"
MsgBox ReadProp("protection")
End Sub

Sub WriteProp(sPropName As String, sValue As String)
ActiveDocument.CustomDocumentProperties(sPropName).Value = sValue
End Sub

Sub AddProp(sPropName As String, sValue As String, Optional lType As Long = msoPropertyTypeString)
ActiveDocument.CustomDocumentProperties.Add Name:=sPropName, LinkToContent:=False, Type:=lType, Value:=sValue
End Sub

Function ReadProp(sPropName As String) As String
On Error Resume Next
ReadProp = ActiveDocument.CustomDocumentProperties(sPropName).Value
End Function
 
ActiveDocument is a Word object, not an Excel one.
Use ThisWorkbook or ActiveWorkbook instead.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you for your quick response.
Changing ActiveDocument to ActiveWorkbook was the solution !

I've done some research of reading this customdocumentproperties from a closed file but I haven't found a solution for office 2010. I've found the option using dsofile.dll but this doesn't work in office 2010.
Is it possible to read the CustomDocumentProperties("protection") as set in the previous code from a closed file in vba of office 2010?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top