[ Team LiB ] |
![]() ![]() |
Working with Built-in FunctionsVBA has a rich and comprehensive function library as well as tools to assist in their use. The subsections that follow introduce you to many of the commonly used functions. You will see examples of how they are used. Be aware that this is just a sampling of the functions available and provides you with just a few ideas of how you can use these functions. The potential use of the VBA function library is limited only by your needs and your imagination. Built-in FunctionsThe following sections describe some of the most commonly used functions and provide examples. On some rainy day, you should go through the online Help to become familiar with the rest of VBA's built-in functions.
The Format FunctionThe Format function formats expressions in the style specified. The first parameter is the expression you want to format; the second is the type of format you want to apply. Here's an example of using the Format function: Sub FormatData() Debug.Print Format$(50, "Currency") 'Prints $50.00 Debug.Print Format$(Now, "Short Date") 'Prints the current date Debug.Print Format$(Now, "DDDD") 'Displays the word for the day Debug.Print Format$(Now, "DDD") 'Displays 3 - CHAR Day Debug.Print Format$(Now, "YYYY") 'Displays 4 - digit Year Debug.Print Format$(Now, "WW") 'Displays the Week Number End Sub The result of using this function appears in Figure 23.11. Figure 23.11. The Format function, which formats expressions in the specified style.The Instr FunctionThe Instr function returns the position where one string begins within another string: Sub InstrExample() Debug.Print InStr("Alison Balter", "Balter") 'Returns 8 Debug.Print InStr("Hello", "l") 'Returns 3 Debug.Print InStr("c:\my documents\my file.txt", "\") 'Returns 3 End Sub The InStrRev FunctionInStrRev begins searching at the end of a string and returns the position where the compiler finds one string within another string: Sub InstrRevExample() Debug.Print InStrRev("c:\my documents\my file.txt", "\") 'Returns 16 End Sub Notice that the InStr function returns 3 as the starting position for the backslash character within "c:\my documents\my file.txt", whereas the InStrRev function returns 16 as the starting position for the backslash character in the same string. This is because InStr starts searching at the beginning of the string, continuing until it finds a match, whereas InStrRev begins searching at the end of the string, continuing until it finds a match. The Left FunctionLeft returns the left-most number of characters in a string: Sub LeftExample() Debug.Print Left$("Hello World", 7) 'Prints Hello W End Sub The Right FunctionRight returns the right-most number of characters in a string: Sub RightExample() Debug.Print Right$("Hello World", 7) 'Prints o World End Sub The Mid FunctionMid returns a substring of a specified number of characters in a string. This example starts at the fourth character and returns five characters: Sub MidExample() Debug.Print Mid$("Hello World", 4, 5) ''Prints lo Wo End Sub The UCase FunctionUCase returns a string that is all uppercase: Sub UCaseExample() Debug.Print UCase$("Hello World") 'Prints HELLO WORLD End Sub The DatePart FunctionDatePart returns the specified part of a date: Sub DatePartExample() Debug.Print DatePart("YYYY", Now) 'Prints the Year Debug.Print DatePart("M", Now) 'Prints the Month Number Debug.Print DatePart("Q", Now) 'Prints the Quarter Number Debug.Print DatePart("Y", Now) 'Prints the Day of the Year Debug.Print DatePart("WW", Now) 'Prints the Week of the Year End Sub The DateDiff FunctionDateDiff returns the interval of time between two dates: Sub DateDiffExample() Debug.Print DateDiff("d", Now, "12/31/99") ''Days until 12/31/99 Debug.Print DateDiff("m", Now, "12/31/99") ''Months until 12/31/99 Debug.Print DateDiff("yyyy", Now, "12/31/99") ''Years until 12/31/99 Debug.Print DateDiff("q", Now, "12/31/99") ''Quarters until 12/31/99 End Sub The DateAdd FunctionDateAdd returns the result of adding or subtracting a specified period of time to or from a date: Sub DateAddExample() Debug.Print DateAdd("d", 3, Now) 'Today plus 3 days Debug.Print DateAdd("m", 3, Now) 'Today plus 3 months Debug.Print DateAdd("yyyy", 3, Now) 'Today plus 3 years Debug.Print DateAdd("q", 3, Now) 'Today plus 3 quarters Debug.Print DateAdd("ww", 3, Now) 'Today plus 3 weeks End Sub The Replace FunctionReplace replaces one string with another: Sub ReplaceExample() Debug.Print Replace("Say Hello if you want to", "hello", "bye") 'Returns Say Bye if you want to Debug.Print Replace("This gets rid of all of the spaces", " ", "") 'Returns Thisgetsridofallofthespaces End Sub The StrRev FunctionStrRev reverses the order of text in a string: Sub StrReverseExample() Debug.Print StrReverse("This string looks very funny when reversed!") 'Returns !desrever nehw ynnuf yrev skool gnirts sihT End Sub The MonthName FunctionMonthName returns the text string associated with a month number: Sub MonthNameExample() Debug.Print MonthName(7) 'Returns July Debug.Print MonthName(11) 'Returns November Functions Made Easy with the Object BrowserWith the Object Browser, you can view members of an ActiveX component's type library. In plain English, the Object Browser enables you to easily browse through a component's methods, properties, and constants. You can also use it to copy information and add it to your code. The Object Browser even adds a method's parameters for you. The following steps let you browse among the available methods, copy the method you want, and paste it into your code:
The example in Figure 23.12 shows choosing a user-defined function selected from a module in a database, but you can select any built-in function. Figure 23.13 shows an example in which the DatePart function is selected from the VBA library. Figure 23.13. The Object Browser with the VBA library selected. |
[ Team LiB ] |
![]() ![]() |