Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
=Alarm(A1,">=1000")
The sound w ill play w hen the value in cell A1 is greater than or equal to 1,000.
C ontains more than 200 useful tips and
tricks for Excel 2007 | Other Excel 2007
books | Amazon link: John
=Alarm(C12,"<0")
Walkenbach's Favorite Excel 2007
Tips & Tricks
The sound w ill play w hen the value in cell C12 is negative.
Tips
The function is evaluated w henever any cell that depends on the reference cell is
changed. The sound can get annoying!
Normally, you w ill w ant to use this function in only one cell. If you use it in more than one
cell, you w ill not be able to tell w hich instance of the function triggered the sound.
converted by Web2PDFConvert.com
the type of a particular drive.
Example
The example below lists all drives, and their type. The information is sent to columns A and B of
the active w orksheet.
Sub ShowAllDrives()
Dim LetterCode As Long
Dim Row As Long
Dim DT As String
Row = 1
For LetterCode = 65 To 90 ' A-Z
DT = DriveType(Chr(LetterCode))
If DT <> "Non-existent" Then
Cells(Row, 1) = Chr(LetterCode) & ":\"
Cells(Row, 2) = DT
Row = Row + 1
End If
Next LetterCode
End Sub
The dow nloadable file also contains functions that return the total drive size, and space
available.
VBA's Split function, introduced w ith Excel 2000, can simplify many programming tasks. This
function accepts a text string, and returns a zero-based variant array that contains the
elements of the string (you specify the character that delimits the elements).
A simple example
The procedure below demonstrates how the Split function w orks.
Sub SplitDemo()
Dim txt As String
Dim x As Variant
Dim i As Long
txt = "The Split function is versatile"
x = Split(txt, " ")
For i = 0 To UBound(x)
Debug.Print x(i)
Next i
End Sub
converted by Web2PDFConvert.com
In this case, the delimiter is a space character. You can specify any character or string to be
used as the delimiter. The follow ing examples demonstrate some other uses for the Split
function.
Extracting an element
Split is a VBA function, so it can't be used in a w orksheet formula. The function below is simply
a "w rapper" for the Split function, so your formulas can make use of this handy function.
=ExtractElement("546-339-909-944",3,"-")
This formula returns 909, the third element in the string (w hich uses a "-" as the delimiter).
Counting words
The function below returns the number of w ords in a string. It uses Excel's TRIM function to
remove excess spaces (w hich w ould cause an incorrect result).
Splitting up a filename
The tw o examples in this section make it easy to extract a path or a filename from a full
filespec, such as "c:\files\w orkbooks\archives\budget98.xls"
converted by Web2PDFConvert.com
Function CountOccurrences(str, substring) As Long
' Returns the number of times substring appears in str
Dim x As Variant
x = Split(str, substring)
CountOccurrences = UBound(x)
End Function
This tip uses tw o W indow s API functions to return the name of the computer, and the name of
the user w ho is currently logged in. These functions can be used in a w orksheet formula, or
called from a VBA procedure.
NOTE: The logged-in user name may or may not be the name that is returned by
Application.User
API Declarations
Private Declare Function GetComputerName Lib "kernel32" _
Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) _
As Long
The Functions
Public Function NameOfComputer()
' Returns the name of the computer
Dim ComputerName As String
Dim ComputerNameLen As Long
Dim Result As Long
ComputerNameLen = 256
ComputerName = Space(ComputerNameLen)
Result = GetComputerName(ComputerName, ComputerNameLen)
If Result <> 0 Then
NameOfComputer = Left(ComputerName, ComputerNameLen)
Else
NameOfComputer = "Unknown"
End If
End Function
converted by Web2PDFConvert.com
The VBA function listed below (tw o versions) returns the name of the most recent file in a
directory. The function takes tw o arguments:
Directory: The full path of the directory (String). For example, "c:\files\excel\"
FileSpec: The file specification (String). For example, "*.xls" for Excel w orkbooks, or "*.*"
for all files.
If the directory does not exist, or if it contains no matching files, the function returns an empty
string.
Also, be aw are that the FileSearch object w as removed, beginning w ith Office 2007.
' Returns the full path and name of the most recent file in a Directory
' That matches the FileSpec (e.g., "*.xls").
' Returns an empty string if the directory does not exist or
' it contains no matching files
Dim NumFound As Long
NewestFile = ""
With Application.FileSearch
.NewSearch
.LookIn = Directory
.FileName = FileSpec
NumFound = .Execute(SortBy:=msoSortByLastModified, _
SortOrder:=msoSortOrderDescending)
If NumFound > 0 Then NewestFile = .FoundFiles(1)
End With
End Function
Usage Examples
This function can be called from a VBA procedure, or used in a w orksheet formula. The
statement below displays the name of the most recent Excel file in c:\myfiles\.
=NewestFile("c:\myfiles", "*.xls")
converted by Web2PDFConvert.com
You may have discovered that Excel 2000 (and later versions) supports automatic cell
hyperlinks. W henever you type something that resembles a URL or an e-mail address into a
cell, this feature automatically converts the text into a clickable hyperlink. But w hat if you don't
want to create a hyperlink?
If you use Excel 2000, you're out of luck. There is no w ay to turn this potentially annoying
feature off. But you can, how ever, override it. If Excel creates a hyperlink from your cell entry,
click the Undo button (or press Ctrl-Z) to restore the cell's contents to normal text. Or, you can
precede the cell entry w ith an apostrophe.
Note: If you're using Excel 2002 or later, you can turn automatic hyperlinks on or off in the
AutoCorrect dialog box
To create the macro, press Alt-F11 to activate the Visual Basic Editor, select Insert, Module to
insert a new VBA module into your project, and then enter the follow ing code:
Sub ZapHyperlinks()
Cells.Hyperlinks.Delete
End Sub
W hen you execute the ZapHyperlinks macro, all hyperlinks on the active sheet are deleted and
replaced w ith normal text.
I created a custom worksheet function. When I access this function using the Insert
Function dialog, it says Choose the Help button for help on this function and its
argum ents. How can I get Insert Function dialog box to display a description of my
function?
As you discovered the message displayed in the Insert Function dialog box is erroneous and
and misleading. To add a description for your custom function, select Tool - Macro - Macros to
display the Macro dialog box. Your function w on't be listed, so you must type it manually into
the Macro name box. After typing the function's name, click Options to display the Macro
Options dialog box. Enter the descriptive text in the Description box.
Can I also display help for the arguments for my custom function in the Paste Function
dialog box?
Unfortunately, no.
My custom worksheet function appears in the User Defined category in the Insert Function
dialog box. How can I make my function appear in a different function category?
You need to do this using VBA. The statement below assigns the function named MyFunc to
category 1 (Finanacial)
converted by Web2PDFConvert.com
15. Engineering (this category is valid only if the Analysis Toolpak add-in is installed)
How can I create a new function category?
You can't.
I have a custom function that will be used in a worksheet formula. If the user enters
arguments that are not appropriate, how can I make the function return a true error value
(#VALUE)?
If your function is named MyFunction, you can use the follow ing statement to return an error
value to the cell that contains the function:
MyFunction = CVErr(xlErrValue)
In this example, xlErrValue is a predefined constant. Constants for the other error values are
listed in the online help.
In most cases, yes. Excel's w orksheet functions are accessed via the WorksheetFunction
method of the Application object. For example, you could access the POW ER w orksheet
functions w ith a statement such as the follow ing:
Ans = Application.WorksheetFunction.Power(5, 3)
Generally, if VBA includes an equivalent function, you cannot use Excel's w orksheet version. For
example, because VBA has a function to compute square roots (Sqr) you cannot use the SQRT
w orksheet function in your VBA code.
Excel 95 doesn't support the WorksheetFunction method. Does that mean I can't make my
Excel 2000 application compatible with Excel 95?
No. Actually, using the WorksheetFunction method is superfluous. The follow ing statements
have exactly the same result:
Ans = Application.WorksheetFunction.Power(5, 3)
Ans = Application.Power(5, 3)
Is there any way to force a line break in the text of a message box?
Use a carriage return or a line feed character to force a new line. The follow ing statement
displays the message box text on tw o lines. vbCr is a built-in constant that represents a
carriage return.
Many users are surprised to discover that Excel cannot w ork w ith dates prior to the year
1900. I create an add-in that addresses this deficiency. The Extended Date Functions add-in
(XDate) allow s you to w ork w ith dates in the years 0100 through 9999.
W hen the XDate add-in is installed, you can use any of the follow ing new w orksheet functions
in your formulas:
XDATE(y,m,d,fmt): Returns a date for a given year, month, and day. As an option, you
can provide a date formatting string.
XDATEADD(xdate1,days,fmt): Adds a specified number of days to a date. As an option,
you can provide a date formatting string.
XDATEDIF(xdate1,xdate2): Returns the number of days betw een tw o dates.
XDATEY EARDIF(xdate1,xdate2): Returns the number of full years betw een tw o dates
(useful for calculating ages).
XDATEY EAR(xdate1): Returns the year of a date.
XDATEMONTH(xdate1): Returns the month of a date.
XDATEDAY (xdate1): returns the day of a date.
XDATEDOW(xdate1): Returns the day of the w eek of a date (as an integer betw een 1
and 7).
These are all VBA functions.
Applications:
converted by Web2PDFConvert.com
The XDate add-in is particularly useful for genealogists and others w ho need to perform simple
calculations using pre-1900 dates. The figure below , for example, show s the XDATEYEARDIF
function being used to calculate ages.
Requirements:
The XDate add-in requires Excel 97 or later.
Limitations:
Be careful if you use dates prior to 1752. Differences betw een the historical American, British,
Gregorian, and Julian calendars can result in inaccurate computations.
Note:
My Power Utility Pak also includes the XDATE functions. How ever, they are not packaged in an
add-in. Rather, you can add the functions directly to the VBA project for your w orkbook. As a
result, you can distribute the w orkbook w ithout a dependent add-in.
Documentation:
Complete context-sensitive online help is included.
Installation:
Installation is a tw o-step process:
Page 3 of 3 pages
[Previous page]
converted by Web2PDFConvert.com
This site is not affiliated with Microsoft Corporation.
Privacy Policy
converted by Web2PDFConvert.com