<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article SYSTEM "http://dtd.nlm.nih.gov/ncbi/kipling/kipling-jp3.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink" article-type="research-article" xml:lang="en">
	<front>
		<journal-meta>
			
			
			
			
		</journal-meta>
		<article-meta>
			
			<article-categories>
				<subj-group>
					<subject><bold>MS Excel Tips &amp; Tricks</bold></subject>
				</subj-group>
			</article-categories>
			<title-group>
				<article-title><bold>How do I write a simple User Defined Function (UDF) in Excel with VBA?</bold></article-title>
				<subtitle><bold>UDFs are a powerful tool customizing Excel. VBA is the easiest way to create them.</bold></subtitle>
			</title-group>
			<contrib-group>
				<contrib>
					<name>
						<surname>Grau</surname>
						<given-names>Andreas</given-names>
					</name>
						<bio><p>Andreas Grau is a passionate financial engineer with the drive to improve the tools for complex economic or financial models. Therefore, he coinvented the methodology Computer Aided Finance, the Monte Carlo Simulation Tool Theta Suite and the MS Excel Speed-up tool Theta Proxy.</p></bio>
						<ext-link ext-link-type="uri" xlink:href="https://computeraidedfinance.wordpress.com">https://computeraidedfinance.wordpress.com</ext-link>
				</contrib>
		</contrib-group>
			
				<pub-date pub-type="ppub">
					<day>30</day>
					<month>11</month>
					<year>2011</year>
				</pub-date>			
			<kwd-group kwd-group-type="simple"><kwd><bold>MS Excel</bold></kwd><kwd><bold>UDF</bold></kwd><kwd><bold>VBA</bold></kwd>
			</kwd-group>
			
		</article-meta>
	</front>
	<body>
		
<sec><title>MS Excel 2010 - UDF in VBA:Preparation</title>
<p>First we need to open the Visual Basic for Applications Editor. Choose either ALT + F11 or to press the "Visual Basic" button of the Developer tab in the Ribbon:</p>
<p><fig><label>MS Excel 2010 Developer tab</label><caption><para>Visual Studio Editor in MS Excel 2010 Developer tab. Sometimes, this tab is not presented in Excel by default.</para></caption>
<media xlink:href="https://computeraidedfinance.files.wordpress.com/2011/11/excel_dev_tab_in_ribbon1.png?w=300"><alt-text>MS Excel 2010 Developer tab</alt-text><long-desc></long-desc><permissions><copyright-statement></copyright-statement><copyright-holder></copyright-holder><license license-type="creative-commons"><license-p></license-p></license></permissions></media></fig></p>
<p>If the Developer tab is not enabled, you can open it following these instructions:</p>
<title>Open the Developer tab for Microsoft Excel 2010</title>
<p><list list-type="order"><list-item><p>Start MS Excel.</p></list-item><list-item><p>Click theFiletab.</p></list-item><list-item><p>Click Options.</p></list-item><list-item><p>In the categories pane, clickCustomize Ribbon.</p></list-item><list-item><p>In the list of main tabs, selectDeveloper.</p></list-item><list-item><p>Click OK to close theOptionsdialog box.</p></list-item></list></p></sec><sec><title>Open the Developer tab for Excel 2007</title>
<p><list list-type="order"><list-item><p>Start the application.</p></list-item><list-item><p>Click the Microsoft Office Button.</p></list-item><list-item><p>Click Excel Options.</p></list-item><list-item><p>In the categories pane, click Popular.</p></list-item><list-item><p>Select Show Developer tab in the Ribbon.</p></list-item><list-item><p>Click OK to close the Optionsdialog box.</p></list-item></list></p></sec><sec><title>Now, Insert the VB Code</title>
<p>Create a new Module:<fig><label>Module creation in MS Excel 2010</label><caption><para>Create a new module clicking on "Module" in the Visual Basic for Applications Editor.</para></caption>
<media xlink:href="https://computeraidedfinance.files.wordpress.com/2011/11/module.png?w=150"><alt-text>Module creation</alt-text><long-desc></long-desc><permissions><copyright-statement></copyright-statement><copyright-holder></copyright-holder><license license-type="creative-commons"><license-p></license-p></license></permissions></media></fig></p>
<p>Insert the following VBA Code</p>
<p><preformat>Function MySquare(X As Double)</preformat></p>
<p><preformat>MySquare = X * X</preformat></p>
<p><preformat>End Function</preformat></p>
<p>And you are done: You created your first MS Excel UDF and you can now start "MySquare":<fig><label>The first MS Excel UDF: MySquare</label><caption><para>Use MySquare in the same way as any other build-in Excel function.</para></caption>
<media xlink:href="https://computeraidedfinance.files.wordpress.com/2011/11/mysquare.png?w=150"><alt-text>MS Excel UDF</alt-text><long-desc></long-desc><permissions><copyright-statement></copyright-statement><copyright-holder></copyright-holder><license license-type="creative-commons"><license-p></license-p></license></permissions></media></fig></p></sec><sec><title>My function is different: It Will Return a Different Value for Every Call</title>
<p>What can you do if your function should return a different value for every calculation? Well, you can mark the function as volatile and it will be recalculated every time, e.g. every time you press F9. As an example, we want to look at a function which returns the time in seconds with millisecond accuracy:</p></sec><p><preformat><monospace>Function PreciseTime()<bold></bold></monospace></preformat></p>
<p><preformat><monospace><bold> Application.Volatile</bold></monospace></preformat></p>
<p><preformat><monospace> PreciseTime = Timer</monospace></preformat></p>
<p><preformat><monospace>End Function</monospace></preformat></p>
<p>Just add <monospace>Application.Volatile</monospace> to the second line of your UDF and it will be recalculated every time.</p>
	
	</body>
	<back>



	</back>

	<response response-type="reply">
		<front-stub><contrib-group>
				<contrib>
				</contrib>
			</contrib-group>
				<pub-date pub-type="ppub">
					<day>29</day>
					<month>4</month>
					<year>2015</year>
				</pub-date>	
		</front-stub>
		<body>
			<p>nice.expect more			</p>
		</body>
	</response>
	<response response-type="reply">
		<front-stub><contrib-group>
				<contrib>
				</contrib>
			</contrib-group>
				<pub-date pub-type="ppub">
					<day>30</day>
					<month>7</month>
					<year>2014</year>
				</pub-date>	
		</front-stub>
		<body>
			<p>[&#8230;]  [&#8230;]			</p>
		</body>
	</response>
	<response response-type="reply">
		<front-stub><contrib-group>
				<contrib>
				</contrib>
			</contrib-group>
				<pub-date pub-type="ppub">
					<day>7</day>
					<month>1</month>
					<year>2012</year>
				</pub-date>	
		</front-stub>
		<body>
			<p>At last! Someone who understands! Thanks for posintg!			</p>
		</body>
	</response>
</article>