Tuesday, October 28, 2008

Using Auto_open and Auto_close function in macro

Hi All,

First of all HAPPY DIWALI to you.

Today we are learned about two more powerful function of macro in excel.
1) Auto_Open
2)Auto_Close

1)Auto_Open: auto_open is a function in macro of excel. This function call your desired vb code before a excel workbook is open.

2)Auto_Close: auto_close is a function in macro of excel. This function call your desired vb code before a excel workbook closed.


Here am make one one example for both functions. The following are the codes.

Auto_Open function: The following code show a welcome message when ever you open the workbook in which you save the macro code. You can describe your message for the first time you open your workbook.


Sub auto_open()

ThisWorkbook.Activate

Sheets("record").Select

Range("A1").Select

txt = ActiveCell.FormulaR1C1


Do While txt = ""
txt = InputBox("You not describe any msg please write a msg.", "Welcome Message")

Range("A1").Select

ActiveCell.FormulaR1C1 = txt

If txt = "" Then

MsgBox "Please enter a msg", vbOKOnly, "Alert"

End If


Loop

ActiveWorkbook.Save

MsgBox txt, vbOKOnly, "Welcome"

End Sub


Auto_Close function: The following code show a good bye message whenever you closed the workbook on which you save the vb macro code.


Sub auto_close()

ThisWorkbook.Activate

Sheets("record").Select

Range("A2").Select

txt = ActiveCell.FormulaR1C1

Do While txt = ""

txt = InputBox("Describe a good bye msg", "Good Bye Message")

Range("A2").Select

ActiveCell.FormulaR1C1 = txt

If txt = "" Then

MsgBox "Please enter a msg", vbOKOnly, "Alert"

End If

Loop

ActiveWorkbook.Save

MsgBox txt, vbOKOnly, "Good Bye"

End Sub


Try the both code and tell me what do you think about the more powerful function in excel.
You can also download a addins of the same macro code from the following links

Download the addins: Click Here

No comments:

Subscribe Now: From your browser

Search:

Learn Office Excel