• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

1st macro run then 2 macro is run

kalpeshpatel

New Member
my worksheet1 contain 2 macro name (1)addrecord (2)print


now , i want if first addrecord macro is run then print macro is run

otherwise message is shown first addrecord is run

e.g. print record do not run without run addrecord


how possible that
 
Something like this structure:

[pre]
Code:
Public Sub addrecord()
' do stuff
Call print
End Sub

Private Sub print()
'do some other stuff
End Sub
[/pre]
From the workbook, user should only be able to run the addrecord macro.
 
I have a stupid question about this luke:


I've got a series of functions that I'm trying to make play together: So


sub firstfunction()

dim somevar as int

dim somevar2 as int

dim somevar3 as int


somevar = somenumberfromsomewhere

somevar1 = somenumberfromsomewhere

somevar2 = somenumberfromsomeewhere


call secondfunction(somevar,somevar1,somevar2)


end sub


secondfunction(somevar as integer, somevar1 as integer, somevar2 as integer)


do a bunch of stuff

end sub


Are there a any rules associated with passing variables from one function to another? In general, this seems to cause a lot of headaches for me.
 
Hi dan_I,


The difference is whether it's a procedure, module, or project level variable.

http://www.ozgrid.com/VBA/variable-scope-lifetime.htm


With your example, it's a little confusing because, technically, you're using the same name to define 2 different things. Somevar is the variable name in Sub 1, but then it is redfined in the function. While this is legal, it's somewhat misleading, as the two don't necessarily go together. I.e., if the sub had said:

[pre]
Code:
call secondfunction(somevar1,somevar,somevar2)
The function would still run, but the values from the first somevar1 has not been relabeled as somevar. An alternative way might be:

sub firstfunction()
dim x as int
dim y as int
dim z as int

x= somenumberfromsomewhere
y= somenumberfromsomewhere
z= somenumberfromsomeewhere

call secondfunction(x,y,z)

end sub

secondfunction(height as integer, length as integer, width as integer)

do a bunch of stuff
end sub
[/pre]
Hopefully that, plus the article helps?
 
Luke,


I think you meant to 'dim' those variables as x, y, and z in firstfunction() too didn't you?
 
Right click on sheet tab, view code, paste this in.

[pre]
Code:
Dim MacroHidden As Boolean

Private Sub Worksheet_Activate()
'Hides macro when sheet is first activated
MacroHidden = True
End Sub

Sub AddRecord()
'do stuff

'Macro has been run, unhide the print macro
MacroHidden = False
End Sub

Sub PrintStuff()
'Check if hidden
If MacroHidden Then
'Give user a message telling why they can't run this
MsgBox "You can't do that yet", vbOKOnly, "Add record first"
Exit Sub
End If

'If macro is unhidden then
'do stuff

End Sub
[/pre]
Fill in the "do stuff" portions as desired.
 
Back
Top