[Top] [GetFormula] [Install a Macro] [GetFormula Example] [GetFormat] [Ex] [Carpentry/Measurement] [Format/Fill characters] [HasFormula] [Ex] [BoldSum] [FontInfo] [FontStyle] [Select cells with formulas or constants] [UseFormula] [Remove all formulas from a workbook] [Formula in MsgBox] [Sheet Statistics] [More Notes] [Status Bar] [Large/Small WS Formulas] [GetFormulaInfo] [AddIn (incomplete)] [Related] [Bottom]
Not satisfied with the methods of showing formulas in Excel:
A simple VBA User Defined Function (UDF) is the solution. To show the formula of another cell, you can use a simple VBA function. GetFormula was written as my first contact with an Excel newsgroup. Most of the help came from Alan Beban, who offered me a more complicated version with offset. I managed to simplify it by trial and error to exactly what I really wanted in a more generic formula. It has proven very useful.
Function GetFormula(Cell) GetFormula = Cell.Formula End FunctionThe following variation might look better but would not match the Formula view of Excel.
Advantage is it shows the single quote, and it shows array formulas as array formulas.Function GetFormulaI(cell) If VarType(cell) = 8 Then GetFormulaI = "'" & cell.formula Else GetFormulaI = cell.formula End If If cell.HasArray Then _ GetFormulaI = "{" & cell.formula & "}" End Functionif you ONLY want to see a formula or nothing. My preference is for GetFormula or GetFormulaI
above but some people ask only to see an actual formula.Function ShowFormula(cell) If cell.HasFormula Then ShowFormula = cell.Formula End FunctionThe use of Cell.FormulaLocal in the above functions may work better for non English usage of Excel.Placing Formulas into Cell Comments is another approach but would not recommend it as being very practical.
Obtaining intermediate results for a formula is possible but complicated see GFRV user defined function, posted by Harlan Grove 2002-02-27 misc.
Example: (Formula view)
A B C 1 =4+5 =GetFormula(A1) =GetFormula(B1) 2 =NOW() =GetFormula(A2) =GetFormula(B2) Example: (Data view)
A B C 1 9 =4+5 =GetFormula(A1) 2 1/16/98 22:59 =Now() =GetFormula(A2) You can copy the =GetFormula(A1) downward to do the column.
Notice that the GetFormula(cellname) also works on GetFormula(cellname).
GetFormula has been very useful for me, hope it helps you as much.
GetFormula failures: On a protected sheet GetFormula will return #VALUE! if the cell being examined is hidden. If a cell is hidden you cannot see the formulas on the formula bar. If a cell is locked you cannot change the value or formula, but has no effect on GetFormula.
Highlight a part of the formula in the Formula Bar and press F9. The highlighted part of the formula is replaced by the result. If you press Esc then the formula re-appears, but if you press Enter the formula or part formula is permanently replaced. Charles Williams, 2001-04-20, and then mentions that... In Excel 2002 you can evaluate formulae step by step automatically.
Conditional Formatting is considerably harder to show what you want to see. It has a range that you can't just see, anyway the following is a start and will work best if it just has a formula, rather than "is less than" type of conditions.Function CondFormula(myCell, Optional cond As Long = 1) As String 'Bernie Deitrick programming 2000-02-18, modified D.McR 2001-08-07 Application.Volatile CondFormula = myCell.FormatConditions(cond).Formula1 End Function
Your User Defined Functions (UDF) can be found using the Paste Function Wizard (Shift + F3). Select "User Defined" which is near the bottom of the left-hand window and your UDF will appear on the right-hand window.
Entering a Description for the Function Wizard (Shift+F3): in XL2000
(directions to install a macro or function are below) The parameter cell will show up in the Function Wizard Help without any intervention, so choose your parameter names carefully. The description can be entered as follows: Alt+F11 (VBE), F2 (View, Object Browser), Select the module on left, select the function on right, RClick, properties, fill in description.For example the description for GetFormula reads as follows:
Displays the formula used in the referenced cell. For more information see http://www.mvps.org/dmcritchie/excel/formula.htm#getformulaLaurent Longre has a method for Creating new function categories (Excel 97) in the Function Wizard, in the English subset of his main site which is in French. You can also look at a Translator.Go.com assisted Translation to English
Function names can be seen in the Paste Function Wizard (Shift+F3). User Defined Functions are near the bottom of the left side window.
Restrictions on Functions: A Function invoked from a Worksheet cannot modify any other cell in any manner, nor can it modify the format or formula of any cell including itself. (No such restriction of macro functions -- functions called from a macro).
Can't get your function to work in XL97 and up
If you installed a function and get #NAME! then the function cannot be found. If you get #VALUE1 then your operands are incorrect. The Functions described on this page and most macros are installed in a standard module, but that is not a exactly what appears as the choice on the menu in the Visual Basic Editor (Alt+F11, Insert, Module) used in Excel 97 and up.
If you would like to begin with a simple example take a look at this first.
Getting Started with Macros -- http://www.mvps.org/dmcritchie/excel/getstarted.htm
User Defined Functions and Macros are installed in (Normal) Modules in the same
manner even though used differently. The UDF will appear in the Function Wizard
![]() The easiest way to install a macro is to record a macro using the macro recorder. Regardless of your version of Excel, the macro recorder will install a macro for you. You can add additional macros and functions to the same module. Don't put all your macros in one module though since each module that is loaded will add to memory usage.
The Select ALL button can be found at the intersection of the row and column headings. You can record your macro in your Personal Macro Workbook (in your XLSTART directory) by choice in the Store Macro In: box Loss of the square Record Stop button with the [x] can be rectified with Right-click on a Toolbar --> Customize --> Toolbars - check the Stop Recording box. Close Customize dialog. Another way if Excel is still open is to open your latest *.XLB file (use START, Find, *.XLB). Version specific information
Common to XL95, XL97, XL98, XL2000
Summary of ways to invoke a macro «Alt+F8 --> Select Macro -- Run To invoke a User Defined FunctionSame as you invoke builtin Excel functions, as a formula Invoking Macros and Functions in another workbook from CodeThere are two ways to do this [Rob Bovey, misc 2001-03-24]. You can either add a reference to the VBAProject of the workbook containing the macro you want to call and then call it directly, or you can use the Excel Application object's Run method. In either case the macro must be public (e.g. it can't be preceeded by the Private qualifier and it can't reside in a module that contains the Option Private Module directive). The workbook containing the macro must also be open. The difference between call and run: RUN is by the application and call call via a variable; whereas, CALL is determined at compile time in this case meaning when the macro starts. (Run method shown below). Additional Information on MacrosMS KB directions to install a macro included with Q142140 XL: How to Add the Workbook Path to the Footer |
[Top] [HasFormula] [Related]
Function GetFormat(cell) GetFormat = cell.NumberFormat End Function
=HYPERLINK("www.mvps.org/dmcritchie/excel/excel.htm","My Excel Pages")
VALUE | =GetFormat(A...) | =GetFormula(A...) | + | ||
A | B | C | D | ||
1 | 17 | General | =4+5+8 | F | |
2 | 06/25/1998 09:50:55.83 | mm/dd/yyyy hh:mm:ss.00
US default for =NOW() is m/d/yy h:mm |
=NOW() | F | |
3 | (5,878.00) | #,##0.00_);[Red](#,##0.00) | -5878 | N | |
4 | 8.89E+10 | 0.00E+00 | 88888888888 | N | |
5 | (212) 555-1212 | [<=9999999]###-####;(###) ###-#### | 2125551212 | N | |
6 | 1.1.4 | @ | 1.1.4 | T | |
7 | 173.23.124.123 | General | 173.23.124.123 | T | |
8 | 123-45-6789 | 000-00-0000 | 123-45-6789 as text or 123456789 as number | T | |
9 | General | T | |||
10 | General | O | |||
11 | Yes | [Red][>0]"No";[Green]"Yes" | =-1 | F | |
12 | No | [Red][>0]"No";[Green]"Yes" | =1 | F | |
13 | 5.00 |
[Blue][>=5]0.00;[Red][<-2]-0.00;[Yellow] General;[magenta]"Text:"@ |
=5 | F | |
14 | 1.1M | 0.0,,"M"_);(0,,"M)";0"M"_);@ | 1100000 | N | |
15 | 0023 | 0000;(0000);0;@ | 23 | N | |
16 | 23 1/2" | # ??/??\" | 23.5 | N | |
17 | 72° 14' 32" | [h]° mm' ss\" | =72.2422 / 24 | F | |
18 | 17' 2.4" | General | =INT(17.2)&"' "&ROUND(12*MOD(17.2,1),1)&"'" | F | |
19 | 17' 2.4" | General | =INT(206.4/12)&"' "&ROUND(MOD(206.4,12),1)&"'" | F | |
20 | 17.256 | 0.????_);(0.????);0.????;@ | 17.256 | N | |
21 | 4 lb. 2.0 oz. | General | =INT(4.125)&"lb. "&ROUND(16*MOD(4.125,1),1)&"oz." | F | |
22 | 199 | General | =DATEDIF(DATE(1999,6,16),DATE(2000,1,1),"d") | F | |
23 | 15 | General | =5*ROUNDUP(10.1/5,0) | F | |
24 | My Excel Pages | General | =hyperlink("www.mvps.org/dmcritchie/excel/excel.htm","My Excel Pages") | F | |
25 | †††† †††† †††† || | General | =REPT(REPT(CHAR(134),4)&" ",INT(A1/5))&REPT("|",MOD(A1,5)) See Tally Bar (Five-Bar Gate) | F | |
26 | Begin............... | @*. | 'Begin | T | |
27 | .................End | *.@ | 'End | T | |
28 | 12 | Geneal | =COUNTA(E15:e26) (DCOUNT, COUNT, COUNTA, COUNTBLANK, COUNTIF -- Cell Counting Techniques, J-Walk, Tip 52) | T | |
29 | 63 | General | =SUM(D3:D14) | F | |
30 | 63 | General | =SUM(D3:OFFSET(D15,-1,0)) See reasons to use OFFSET | F | |
31 | 6 | General | =LARGE('Sheet One'!A14:A19,1) (Largest number in range) | F | |
32 | 15 | General | =SUMPRODUCT(LARGE(Sheet1!A14:A19,{1,2,3})) (Sum of the Largest 3 entries) | F | |
33 | 07710-1234 | [<100000]00000_-_0_0_0_0;[>0]00000-0000;;@ | 077101234 (5 & 9 digit US zip-codes, and should be left justified) | N | |
34 | 222.2E+6 | ##0.0E+0 | 222222222.22 (Engineering notation, powers of 3, posted by Bernard Liengme) | N | |
35 | 10 | General (-10 -15 +10 +20) | =INDEX(A4:D4,,MATCH(0,A4:D4,1)+1) (First positive number in a row, posted by Niek Otten +) | N | |
36 | 10,000,000 | #,##0.00_);[Red](#,##0.00) | 10000000 | N | |
37 |
1,00,00,000 |
[>9999999.99]##\,##\,##\,##0.00;[>99999.99]"##\,##\,##0.00;"##,##0.00
|
10000000
(This format separates groups for India/Thailand, format valid for
postive numbers up to 99,99,99,999.99 or 999,999,999.99)
in Excel XP see function BhatText(A37)as in Thai Bhat currency. |
N |
|
One way to simplify creating custom formats is to pick the best match say under number or fractions. Then keeping the same cell selected go to custom format. Also in custom note that as you change the formatting the example will change.
To provide blanks to right of numbers use two underscores. The first underscore indicates to copy the next character as is, while the second underscore will actually be a blank. Notice the ?? used in fractions can also be used to align decimal points in numbers like 129.43 and 10.125
  | A | B | C | D | E |
23 | Description | Feet | Inches | Format | Format |
24 | Descript 1 | 5' | 11" | #'__ | General\" |
25 | Descript 2 | 55' | 11.5" | #'__ | General\" |
26 | Total | 61' | 0.875" | #'__ | General\" |
27 | |||||
28 | Cell | Formulas used | |||
29 | B26: | =INT(SUM(B24:B25)+SUM(C24:C25)/12) | |||
30 | C26: | =MOD(SUM(B24:B25)+SUM(C24:C25)/12,1) |
This formula from a posting by Bernie Dietrich 2000-08-04 will round to the nearest 16 inch.
Conversions of Feet and Inches, decimal to 1/16th inch fractional measurements
|
This formula from a posting by Bernie Dietrich 2000-08-04 will round feet and decimal feet to feet, inches with rounded 1/16 inch fractions. =IF(A1>=1,INT(A1)&"' ","") & TEXT(MOD(A1,1)*12,"- 0"&IF(ABS(MOD(A1,1)*12-ROUND(MOD(A1,1)*12,0))>1/32," 0/"&CHOOSE(ROUND(MOD(MOD(A1,1)*12,1)*16,0),16,8,16,4,16,8,16,2,16,8,16,4,16,8,16),"")) &"""" | |||||||||||||
Bernie also supplied version from inches with decimal fractions to feet and inches with
rounded 1/16th inch fractions.
=INT(A1/12)&"' " &
TEXT(MOD(A1,12),"0"&IF(ABS(MOD(A1,12)-ROUND(MOD(A1,12),0))>1/32,"
0/"&CHOOSE(ROUND(MOD(MOD(A1,12),1)*16,0),16,8,16,4,16,8,16,2,16,8,16,4,16,8,
16),"")) & """"
and a version from inches with decimal fractions to inches only with rounded 1/16th inch fractions.
=TEXT(A1,"0"&IF(ABS(A1-ROUND(A1,0))>1/32,"
0/"&CHOOSE(ROUND(MOD(A1,1)*16,0),16,8,16,4,16,8,16,2,16,8,16,4,16,8,16),""))
&""""
and a formula to take Feet and Inches back to a number of feet (Bernie Deitrick)
=VALUE(LEFT(A1,FIND("'",A1)-1)) +
VALUE(MID(A1,FIND("'",A1)+1,FIND("""",A1)-FIND("'",A1)-1))/12
or a formula to take Feet and Inches back to a number of inches (based on Bernie Deitrick's previous solution)
=VALUE(LEFT(A1,FIND("'",A1)-1))*12 + VALUE(MID(A1,FIND("'",A1)+1,FIND("""",A1)-FIND("'",A1)-1))
For those doing carpentry or other things that require trying to get the most pieces cut from sheet stock (plywood) or linear stock (pipe, and 2x4s) see Sheet Layout & Lineal Layout in Wood Online Magazine software (pay for), produces a layout and a cut-sheet list of parts. identified by Gord Dibben don't know if anyone here has used it.
Additional Worksheet Formulas created by Bernie Deitrick « for
Fractions rounded to 1/4, 1/8, 1/16, 1/32, 1/64, and 1/128
For more information on formatting see Custom Number Formatting in Microsoft Excel [http://support.microsoft.com/support/excel/content/formats/default.asp]
-1 | [>=5]General;[Blue]-General;[Red]General |
0 | [>>=5]General;[Blue]-General;[Red]General |
2 | [>=5]General;[Blue]-General;[Red]General |
4 | [>=5]General;[Blue]-General;[Red]General |
5 | [>>=5]General;[Blue]-General;[Red]General |
6 | [>=5]General;[Blue]-General;[Red]General |
Format for ... (each format is separated by a semicolon)To color negative numbers RED and other numbers black.
Positive Numbers(default); Negative Numbers(default); Zero(All other numbers); Text
HELP -- ans wiz -- format number
For text simply use the toolbar button that looks like a T (for text) with a color block in front -- there is a pull down beside it. There is a similar button with a paint bucket behind the block for background; and another button with a pencil behind it for shading.
|
|
Format --> cells --> custom A1: 0*. number value 1 A1: @*. text value: '1 B1: General text value: Monday B1: *.dddd number value: 01/31/2000 (US)There is also format --> cells --> alignment --> horiz. --> fill which will fill the entire cell with repeated value
Function HasFormula(cell) HasFormula = cell.HasFormula End Function
Usage: (showing Formula, Number, Text or Other)
=IF(HasFormula(A1),"F",IF(ISNUMBER(A1),"N",IF(ISTEXT(A1),"T","O")))
A similar VBA function combining features of both of the above can be found on John Walkenbach's site Determining the Data Type of a Cell (tip 52). Keep in mind that the returned attribute may not be mutually exclusive to other attributes.
HasFormula can be used directly with Conditional Formatting to indicate cells that have formulas because it returns True or False which is what you need for Conditional Formatting.
Function BoldSum(rngCells As Range) As Double 'BoldSum is recalculated when any value on sheet changes Application.Volatile Dim cell As Range BoldSum = 0 On Error Resume Next For Each cell In rngCells If cell.font.Bold Then BoldSum = BoldSum + cell.Value Next cell End FunctionA similar function BoldCnt can be patterned on the above using BoldCnt + 1
A subroutine providing more information than FontStyle immediately below can be found in FormulaBox providing font information about the first cell in a range plus more general information for the other cells in a selection, and
in FontInfo providing font information for another cell.
Function FontStyle(cell)
'Won't change value until some value on sheet changes
Application.Volatile
FontStyle = cell.font.fontstyle
End Function
In VBA
cells.SpecialCells(xlCellTypeConstants).Select 'In Excel 97
cells.SpecialCells(xlConstants).Select 'In Excel 5/95/97
Had you started with a range instead of a single cell you would have selected from the range rather than from the entire sheet.
The following will color font red for all cells with formulas in range, or for entire sheet if only a single cell is selected.
Sub ColorFormulas() 'xl97 up use xlcelltypeformulas Selection.SpecialCells(xlFormulas).Font.ColorIndex = 3 End SubThe manual equivalent using Go To under Edit
Formulas would be xlFormulas or xlCellTypeFormulas depending on your version. Some other cell types in D10SE697 -- SpecialCells Method (gone, see information in HELP, below).
SpecialCells Method
F1 (HELP) --> Index --> SpecialCells Method
Returns a Range object that represents all the cells that match the specified type and value.Syntax
expression.SpecialCells(Type, Value)
expression Required. An expression that returns a Range object.
Type Required Long. The cells to include. Can be one of the following XlCellType constants.
Constant Description xlCellTypeAllFormatConditions Cells of any format xlCellTypeAllValidation Cells having validation criteria xlCellTypeBlanks Empty cells xlCellTypeComments Cells containing notes xlCellTypeConstants Cells containing constants xlCellTypeFormulas Cells containing formulas xlCellTypeLastCell The last cell in the used range xlCellTypeSameFormatConditions Cells having the same format xlCellTypeSameValidation Cells having the same validation criteria xlCellTypeVisible All visible cells Prior to XL97 the Constants did not include CellType in their naming.
As just indicated: using Go To Select a single cell, then Edit=>Goto=>Special and then pick formulas and then OK. Then use shading cell or color text with the either of those buttons, or using format menu. Again, had you started with a range instead of a single cell you would have selected from the range rather than from the entire sheet. |
|
For presentation purposes. Since this will purposely destroy all formulas in a workbook ou would only want to run this on a copy of a live workbook.Sub Remove_All_Formulas_In_All_Sheets 'No_formula_Macro() 'Goodnight 2000-10-27 programming -- Striping a workbook of formulas Dim SH As Worksheet For Each SH In Worksheets SH.Activate Application.ScreenUpdating = False Cells.Select Selection.Copy Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, _ SkipBlanks:= False, Transpose:=False Next End Sub
Replacing formulas with external links with their values similar to copy, and paste special, values. This solution by Tom Ogilvy 2001-07-27 in programming.
Sub Tester3() Dim cell As Range For Each cell In ActiveSheet.UsedRange.SpecialCells(xlFormulas) If InStr(cell.Formula, "[") Then cell.Formula = cell.Value End If Next End SubIf you have Left brackets in any other formulas, then they will also be changed - but a link to an external workbook should contain a Left bracket. Could include cells with HYPERLINK worksheet formulas for instance.
You will get an error if there are no cells with formulas on the worksheet.
Function UseFormula(cell) '-- Usage: Not recommended, see notes UseFormula = Application.Evaluate(cell.formula) End Function
Literal with formula | =UseFormula(A1) |
'=3+4 | 7 |
As previously stated I prefer to use GetFormula for documentation purposes because you know you are working with valid formulas that way. The following attempts to make some limited checks for validity by checking that the formula begins with an equal sign, and will not display if the source cell appears to be blank.
Function UseFormula2(cell) 'Documented in http://www.mvps.org/dmcritchie/excel/formula.htm ' UseFormula Jul 20, 1998, UseFormula2 Jun 13, 2000 'Application.Volatile = True -- DO NOT DO THIS If Trim(cell.Value) = "" Then UseFormula2 = "" Exit Function ElseIf Left(cell.Value, 1) = "=" Then UseFormula2 = Application.Evaluate(cell.Formula) Exit Function Else UseFormula2 = "'#bad formula" End If End Function
FormulaBox: formula & formats | |
First Character of (345) is = CHR(0051) or Hex=x'33' Arial 12 Bold G28: 345 General G29: =D26+D27 _(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_) G30: 188121234 000-00-0000 G31: 7777777777 [<=9999999]###-####;(###) ###-#### |
|
Caution: The results may be truncated, you may not see the full range |
Sub FormulaBox() 'David McRitchie 1998-08-12 1999-08-17 ' http://www.mvps.org/dmcritchie/excel/excel.htm 'Place material into MsgBox [ctrl+n] 'Will process ranges of one or more columns 'Application.ScreenUpdating = False Dim MsgBoxx As String MsgBoxx = "First Character of (" _ & ActiveCell.Value & ") is " & "=CHR(" _ & Right("0000" & Asc(ActiveCell.Value), 4) & ") or Hex=x'" _ & Hex(Asc(ActiveCell.Value)) & "'" & Chr(10) _ & ActiveCell.Font.Name & " " & ActiveCell.Font.Size _ & " " & ActiveCell.Font.FontStyle _ & Chr(10) & Chr(10) For ix = 1 To Selection.Count 'Selection.Item(ix).NoteText _ ... vGetFormulaI = "" If VarType(Selection.Item(ix)) = 8 Then vGetFormulaI = "'" & Selection.Item(ix).Formula Else vGetFormulaI = Selection.Item(ix).Formula End If If Selection.Item(ix).HasArray Then _ vGetFormulaI = "{" & Selection.Item(ix).Formula & "}" 'include below if VarType wanted -- don't include for distribution ' & " " & VarType(Selection.Item(ix)) _ .. MsgBoxx = MsgBoxx _ & Selection.Item(ix).Address(0, 0) _ & ": " & vGetFormulaI _ & Chr(10) & " " & Selection.Item(ix).NumberFormat & Chr(10) Next MsgBoxx = MsgBoxx & Chr(10) & "***" 'to verify you've seen everything xyx = MsgBox(MsgBoxx, , "FormulaBox: Formula & Format & Text for " & Selection.Count & " selected cells") 'Application.ScreenUpdating = True End Sub
Place comment cells for cells that have a value.Sub CommentThem() Dim cell As Range On Error Resume Next Selection.ClearComments On Error GoTo 0 For Each cell In Intersect(Selection, ActiveSheet.UsedRange) If cell.Formula <> "" Then cell.AddComment cell.Comment.Visible = False On Error Resume Next 'fails on invalid formula cell.Comment.Text Text:=cell.Address(0, 0) & _ " value: " & cell.Value & Chr(10) & _ " format: " & cell.NumberFormat & Chr(10) & _ " Formula: " & cell.Formula On Error GoTo 0 End If Next cell End Sub
SheetStats provides sheet statistics in a MsgBox. The following code is modified from a posting by Tom Ogilvy Tom Ogilvy is a frequent poster to the Excel Newsgroups. |
|
Sub SheetStats() 'Tom Ogilvy 1999-02-24 Tues excel programming 'Note in XL97 and above xlcelltypeconstants and xlcelltypeformula Set rng1 = ActiveSheet.UsedRange On Error Resume Next numConstants = rng1.SpecialCells(xlConstants).Count If Err <> 0 Then numConstants = 0: Err = 0 numerrors = rng1.SpecialCells(xlConstants, xlErrors).Count If Err <> 0 Then numerrors = 0: Err = 0 numLogical = rng1.SpecialCells(xlConstants, xlLogical).Count If Err <> 0 Then numLogical = 0: Err = 0 numText = rng1.SpecialCells(xlConstants, xlTextValues).Count If Err <> 0 Then numText = 0: Err = 0 numNumbers = rng1.SpecialCells(xlConstants, xlNumbers).Count If Err <> 0 Then numNumbers = 0: Err = 0 numformulas = rng1.SpecialCells(xlFormulas).Count If Err <> 0 Then numformulas = 0: Err = 0 numBlanks = rng1.SpecialCells(xlBlanks).Count If Err <> 0 Then numBlanks = 0: Err = 0 Msg = "Address: " & Chr(9) & rng1.Address & Chr(10) & _ "Last Row: " & Chr(9) & rng1.Rows(rng1.Rows.Count).Row & Chr(10) & _ "Last Column: " & Chr(9) & rng1.Columns(rng1.Columns.Count).Column & Chr(10) & _ "Total Cells: " & Chr(9) & rng1.Count & Chr(10) & _ " Formulas: " & Chr(9) & numformulas & Chr(10) & _ " Blanks: " & Chr(9) & numBlanks & Chr(10) & _ " Constants:" & Chr(9) & numConstants & Chr(10) Mg2 = "Errors: " & Chr(9) & numerrors & Chr(10) & _ "Logical: " & Chr(9) & numLogical & Chr(10) & _ "Text: " & Chr(9) & numText & Chr(10) & _ "Numbers: " & Chr(9) & numNumbers title1 = "SheetStats for " & Application.ActiveSheet.Name & _ " in " & Application.ActiveWorkbook.FullName iANS = MsgBox(Msg & Mg2, , title1) End SubSome additional related code:
MsgBox _ "Address: " & Chr(9) & _ Selection.Address & Chr(10) & _ "First Cell: " & Chr(9) & _ Selection(1).Address & Chr(10) & _ "Active Cell: " & Chr(9) & _ ActiveCell.Address & Chr(10) & _ "Last Cell: " & Chr(9) & _ Selection(Selection.Cells.Count).Address & Chr(10) 'This really doesn't do anything, it might change activecell. 'Just demos getting and selecting a range Dim strt As Range, eend As Range Set strt = Selection.Cells(1) Set eend = Selection(Selection.Cells.Count) Range(strt, eend).Select
The default font is identified with Tools, Options, General, Standard Font:Function fontinfo(cell As Range) As String fontinfo = cell.FONT.Name & " -- " & cell.FONT.Size If Left(cell.FONT.FontStyle, 7) = "Regular" Then fontinfo = Trim(fontinfo & Mid(cell.FONT.FontStyle, 8, 100)) Else fontinfo = Trim(fontinfo & " " & cell.FONT.FontStyle) End If End FunctionAn example of use, depending on what font you actually used in your table the formula shown might be substituted by something such as:
Arial -- 12 Bold
Webdings -- 12
Wingdings 2 -- 12A table such as the following can be created using
C2: =CHAR(32) D2: =CHAR(32+1) C3: =CHAR(32+16) D3: =CHAR(32+17)
  A B C D E F G H I J K L M N O P Q R 1 =FontInfo(C3) 2 32 ! " # $ % & ' ( ) * + , - . / 3 48 0 1 2 3 4 5 6 7 8 9 : ; < = > ? 4 64 @ A B C D E F G H I J K L M N O 5 80 P Q R S T U V W X Y Z [ \ ] ^ _ 6 96 ` a b c d e f g h i j k l m n o 7 112 p q r s t u v w x y z { | } ~ 8 128 € ‚ ƒ „ … † ‡ ˆ ‰ Š ‹ Œ Ž 9 144 ‘ ’ “ ” • – — ˜ ™ š › œ ž Ÿ 10 160 ¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ¬ ® ¯ 11 176 ° ± ² ³ ´ µ ¶ · ¸ ¹ º » ¼ ½ ¾ ¿ 12 192 À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï 13 208 Ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ü Ý Þ ß 14 224 à á â ã ä å æ ç è é ê ë ì í î ï 15 240 ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ
See my Symbols page.
- References in this document
- BoldSum, Sum the cells having Bold format attribute
FontStyle, Display Font Style used in referenced cell
FormulaBox font information for first cell, and general information for all cells in a selection- Fonts, Getting a List of Installed Fonts (tip 79) -- John Walkenbach (tip 79)
- A simple technique (for Excel 97 or later) to retrieve a list of installed font names, and an alternative to an API function on Stephen Bullen's site for those who don't have XL97.
- The Font Thing -- Sue Fisher [update notes], thanks to Jim Rech (2000-03-15) for telling us.
- Provides information on installed and uninstalled fonts, font samples for your text, including use of two fonts in samples.
WorksheetFunction Property Example
This example displays the result of applying the Min worksheet function to the range A1:A10.
Set myRange = Worksheets("Sheet1").Range("A1:C10")
answer = Application.WorksheetFunction.Min(myRange)
MsgBox answer
Sub Dmax()
MsgBox Application.WorksheetFunction.Max(Range("d1:d18"))
End Sub
Sub Dmax2()
MsgBox Application.WorksheetFunction.Max(Worksheets("## 33 ##").Range("d1:d18"))
End Sub
,,,1,16 ,,,2,32 ,,,4,64 ,,,8,128 Select cell C1 before invoking the following: Sub Macro7() Range(cells(1, 1), cells(4, 1)) = 1 [a5] = "=sum(a1:a4)" iValue = "d1:e4" iValue = InputBox("Supply range, example: d1:e4") If iValue = "" Then iValue = "d1:e4" ActiveCell.formula = "=SUM(" & iValue & ")" cells(5, 2).formula = "=SUM(" & iValue & ")" [e5] = "=SUM(" & iValue & ")" End Sub Results: a1:a4 all contain 1 a5: 4 b5: 255 c1: 255 (and is still the active cell) e5: 255
Subj: Re: formula shown in cell Date: 98-01-17 00:46:57 EST From: dana2@msn.com (Dana De) To: DMcRitchie@msn.com (DMcRitchie) I use a slightly different function that adjusts for the reference style in use. Just another idea. Function FormulaText(cell_ref) 'Allow formula to be updated if changes are made on the sheet Application.Volatile 'Test for reference style in use If Application.ReferenceStyle = xlA1 Then 'Set the return value of the function to the A1 style formula FormulaText = cell_ref.Formula Else ' xlR1C1 --Set the return value of the function to the R1C1 style formula FormulaText = cell_ref.FormulaR1C1 End If End Function
status message like | SUM=3.44 | EXT | CAPS | NUM | SCRL | END | FIX | |
Ready Calculating % Found A22 Circular Status from Excel or Macro |
Rclick on status bar choices Average Count Count Num Max / Min / Sum |
ADD | ||||||
Extended mode (F8). Click on a "start" cell (Anchor), press F8, now
click on another cell and this will highlight *all* cells between the
'anchor' cell and the one selected. To remove indication hit F8 once more.
Use of Shift+F8 shows ADD and allows you to keep adding more ranges until turn off with Shift+F8 (test with MarkSepAreas).
Caps Lock, Num Lock, Scroll Lock, and End are keys on the keyboard. For more information on use of End in shortcuts see http://www.mvps.org/dmcritchie/excel/shortx2k.htm Scroll Lock Key. Press this and the arrow keys will scroll the worksheet, rather than moving the selection down the page. FIX is an option to enter with a fixed number of decimals if no decimal point is keyed in. This is an option that would mess most people up. Tools --> Options --> Edit --> Fixed decimals (number)
Count [WS Functions COUNTA + COUNTBLANK], Count Num [WS Function COUNT] |
Reporting progress on Status Bar
Application.StatusBar = 5*INT(20*n/ncnt)&"%" Application.StatusBar = False 'return control to ExcelIf formula bar is no longer being calculated, Jim Rech said this might fix
CommandBars("AutoCalculate").Enabled = True
|
Use LARGE to find to largest numbers in a range, and SMALL to find the smallest numbers in a range.
In the example at the left ROW(1:1) is used in place of 1, the advantage is that it allows the formula to be replicated down using the fill-handle. For more information on fill-handle see Fill-Handles and Replication. |
If the 2nd and 3rd largest is the same and you want to see both results you can make numbers a little different so that you can see both in a reply posting by Denny Campbell 2000/03/01.
I recommend the GetFormula() described at the beginning of this web page; however, the GetFormulaInfo that came with Excel presents an interesting solution and a lot more of an example in the way of VBA code. I prefer GetFormula() because it can show the coding of formulas on the same page as they are used.
G | H | I | J | |
9 | Sample List | |||
10 | Name | Salary | Dept. | Qty Sold |
11 | Nancy Davolio | 16,000 | 9 | 19 |
12 | Andrew Fuller | 25,000 | 3 | 129 |
13 | Michael Suyama | 28,500 | 3 | 234 |
14 | Janice Leverling | 30,000 | 12 | 199 |
15 | Linda Callahan | 50,000 | 9 | 126 |
16 | Johnathan King | 50,000 | 12 | 45 |
B | C | D | D | |
Summing, Counting, and Offsetting | ||||
9 | Use the COUNTIF and SUMIF functions to total data that meets specified criteria. | |||
10 | The formulas in the following table use the sample data located in cells G10:J16. | |||
11 | Functions and Operators | Using Cell References | Using Defined Names | Using OFFSET() |
12 | COUNTIF() | 2 | 2 | 2 |
13 | SUMIF() | 145 | 145 | 145 |
14 | SUM(IF()) | 2 | 2 | 2 |
15 | SUM(IF()) | 145 | 145 | 145 |
16 | SUM(IF(IF())) | 126 | 126 | 126 |
The GetFormulaInfo macro supplied with Excel
will create a separate spreadsheet that
looks like the following:
Formulas for Microsoft Excel Samples File: Worksheet Functions: | ||
Address | Formula | Value |
C12 | =COUNTIF($I$11:$I$16,9) | 2 |
D12 | =COUNTIF(Dept.,9) | 2 |
E12 | =COUNTIF(OFFSET(Data_Table,0,2,,1),9) | 2 |
C13 | =SUMIF($I$11:$I$16,9,$J$11:$J$16) | 145 |
D13 | =SUMIF(Dept.,9,Qty_Sold) | 145 |
E13 | =SUMIF(OFFSET(Data_Table,0,2,,1),9,OFFSET(Data_Table,0,3,,1)) | 145 |
C14 | =SUM(IF($I$11:$I$16=9,1,0)) | 2 |
D14 | =SUM(IF(Data_Table=9,1,0)) | 2 |
E14 | =SUM(IF(OFFSET(Data_Table,0,2,,1)=9,1,0)) | 2 |
C15 | =SUM(IF($I$11:$I$16=9,$J$11:$J$16,0)) | 145 |
D15 | =SUM(IF(Dept.=9,Qty_Sold,0)) | 145 |
E15 | =SUM(IF(OFFSET(Data_Table,0,2,,1)=9,OFFSET(Data_Table,0,3,,1),0)) | 145 |
C16 | =SUM(IF($I$11:$I$16=9,IF($H$11:$H$16 >26000,$J$11:$J$16,0),0)) | 126 |
D16 | =SUM(IF(Dept.=9,IF(Salary>26000,Qty_Sold,0),0)) | 126 |
E16 | =SUM(IF(OFFSET(Data_Table,0,2,,1)=9,IF(OFFSET(Data_Table,0,1,,1) >26000,OFFSET(Data_Table,0,3,,1),0),0)) | 126 |
Well I've been creating Macros subroutines and Functions and placing them into my personal.xls file, not realizing that there was more to it then that. If a function didn't work there I made another copy back into my test file, never realizing that I had two copies of everything until I deliberately deleted copies in my test file (1998/08/09). The functions no longer worked. A quick check on what is now Google Usenet Archives to find out if I had lost some connection between the two turned up that I needed an add-in. So thanks to Chip Pearson's posting (1998/04/23), I have created my first add-in. (see option 2 below in his posting)
1) Store the function in your Personal.XLS worksheet (or some other worksheet) and place this in the XLSTART directory. Since it will be opened whenever you start Excel, it will be available at all times. Even in this case, you'll have to precede (or "qualify") the function name with the workbook name: =Personal.XLS!MyFunction()
2) Store the function in your Personal.XLS worksheet (or some other worksheet) and save this worksheet as an Excel Add-In (Save As and change File Type to "Excel Add-In" at the very bottom of the list). Then load in this add in, from the Tools->Add Ins menu.
According to Norman Harker the better library to place an addin is C:\Program Files\Microsoft Office\Office\Library so that you don't have to go searching for where it is located. [ref.]
Found the following KB articles and it looks like everything is there.
Addin workbooks Add-in topics on Ole P's site: What is an Addin, How to install one
Tip 45, Excel 97: Creating Add-Ins John Walkenbach
How to create addins , reply posting of links by Tom Ogilvy, 2000-05-30
Another tip from Tom Ogilvy on add-ins:
Creating an Add-In
Digital Signature, Create your own digital certificate
This page was introduced on opening January 1, 1998.
Please send your comments concerning this web page to: David McRitchie mailto:DMcRitchie@msn.com.