Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-14-2015, 10:55 PM
paik1002 paik1002 is offline Function or subroutine with counting memory Windows 7 64bit Function or subroutine with counting memory Office 2010 64bit
Advanced Beginner
Function or subroutine with counting memory
 
Join Date: Dec 2015
Posts: 63
paik1002 is on a distinguished road
Default Function or subroutine with counting memory

I'd like a subroutine/function to have memory, such that it keeps track of the number of times it has been called. The count can be passed back to the parent subroutine.

Here is roughtly what I would like to do(pseudo-code):



Public Function memory() as integer
count++
count = memory
End Sub

Sub main()
current_count = memory() ; current_count = 1
current_count = memory() ; current_count = 2
current_count = memory() ; current_count = 3
End Sub


Can it be done?
Reply With Quote
  #2  
Old 12-15-2015, 01:28 AM
gmayor's Avatar
gmayor gmayor is offline Function or subroutine with counting memory Windows 10 Function or subroutine with counting memory Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You can do it by storing the counter somewhere, ideally in the template containing the process you wish to count - here 'Main' - e.g. as follows.

I have included a macro to test and display the count.

Code:
Option Explicit

Public Function memory() As Integer
Dim oVar As Variable
Dim bVar As Boolean
    For Each oVar In ThisDocument.Variables
        If oVar.Name = "varMemory" Then
            memory = oVar.Value
            Exit For
        End If
        If Not bVar Then memory = 0
    Next oVar
lbl_Exit:
    Exit Function
End Function

Sub main()
Dim oVar As Variable
Dim bVar As Boolean
    For Each oVar In ThisDocument.Variables
        If oVar.Name = "varMemory" Then
            oVar.Value = oVar.Value + 1
            Exit For
        End If
    Next oVar
    If Not bVar Then ThisDocument.Variables("varMemory").Value = 1
    UpdateTemplate
lbl_Exit:
    Exit Sub
End Sub

Sub UpdateTemplate()
'Graham Mayor
Dim bBackup As Boolean
    bBackup = Options.CreateBackup
    Options.CreateBackup = False
    ThisDocument.Save
    Options.CreateBackup = bBackup
lbl_Exit:
    Exit Sub
End Sub

Sub Test()
    MsgBox memory
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Function or subroutine with counting memory Tab 'muscle memory'? markg2 Excel 1 01-09-2015 09:12 AM
Function or subroutine with counting memory Office Assistant needs more memory for gallery? ScottishPolarBear Word 1 11-30-2014 11:32 AM
Counting unique visitors by ward, counting monthly visits by status, editing existing workbook JaxV Excel 9 11-14-2014 12:25 AM
Memory Game with PowerPoint mikeinitaly PowerPoint 0 07-29-2012 09:46 PM
Old Age = Loss Of Memory oldtrout PowerPoint 0 01-02-2012 12:50 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:04 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft