Book

Suggestions


Enter your email address:

Delivered by FeedBurner


Home Page

Bloglines

1906
CelebrateStadium
2006


OfficeZealot

Scobleizer

TechRepublic

AskWoody

SpyJournal












Subscribe here
Add to 

My Yahoo!
This page is powered by Blogger. Isn't yours?

Host your Web site with PureHost!


eXTReMe Tracker
  Web http://www.klippert.com



  Sunday, January 28, 2018 – Permalink –

Date Arithmatic

The drunken cousin


Working with dates has a few twists.

Excel believes that time began on January 1, 1900.

Each day since then is counted so that September 1, 2003 in Excel-speak would be → 37,865.
9/1/03 7:33 A.M. is a decimal → 37865.31458333333

When you subtract one date from another, for instance 9/1/2003 (A1)minus 7/4/2001 (A2),
Excel displays the odd answer of → 2/27/1902.

Excel formats the result of a formula with the same format as the source cells,
Right-click the formula cell (=A1-A2).
Select Format Cells ..., and then choose a Number format with zero decimals.

The correct number of days → 789 will now be displayed.

Another way is to use the rarely documented DATEDIF function. Chip Pearson calls it "the drunken cousin of the Function family."

=DATEDIF(EarliestDate,LatestDate,Interval)

=DATEDIF(A2,A1,"d")


Here's THE source for date math:
Chip Pearson:
All About Dates
Also:

John Walenbach:
Extended Date Functions Add-In
"Many users are surprised to discover that Excel cannot work with dates prior to the year 1900. The Extended Date Functions add-in (XDate) corrects this deficiency, and allows you to work with dates in the years 0100 through 9999."


MS Knowledge Base:
How To Use Dates and Times in Excel



See all Topics

Labels: , , , , , , ,


<Doug Klippert@ 3:42 AM

Comments: Post a Comment


  Sunday, October 08, 2017 – Permalink –

Undo Excel

Level talk


In Excel 2007+. the number of levels of the "undo stack" was increased from 16 levels to 100.

Setting AutoFilters, showing/hiding detail in PivotTables, and grouping/ungrouping in PivotTables are now reversible.

And the undo stack is not cleared when Excel saves, be it an AutoSave or a Save by the user.

If you think the number of undos should be changed, here's how:


  1. Close any programs that are running.

  2. Click Start, click Run, type regedit in the Open box, and then click OK.

  3. In Registry Editor, expand one of the following registry subkeys, as appropriate for the version of Excel that you are running:

    Microsoft Office Excel 2010
    HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Excel\Options

    Microsoft Office Excel 2007 uses Office\12.0\
    Microsoft Office Excel 2003 uses Office\11.0\
    Microsoft Excel 2002 uses Office\10.0\
    Microsoft Excel 2000 uses Office\9.0\


  4. On the Edit menu, point to New, and then click DWORD Value. Select New Value #1, type UndoHistory, and then press ENTER.

  5. On the Edit menu, click Modify.

  6. In the Edit DWORD Value dialog box, click Decimal under Base. Type a value between 0 and 100 in the Value box, click OK, and then exit Registry Editor.

  7. Start Excel. Excel stores an undo history for the number of actions that you specified in step 6.


Modify the number of undo levels
 

If you want to clear the undo stack, just run a macro such as:


Sub ClearUndo()
Range("A1").Copy Range("A1")
End Sub

Allen Wyatt:
Clearing the Undo stack


See all Topics

Labels: , , , ,


<Doug Klippert@ 3:14 AM

Comments: Post a Comment


  Friday, March 17, 2017 – Permalink –

Speed up your Macro

Wha's a macro?

"The purpose of this column is simply to provide information to anyone interested in how to make their work life more efficient through the use of macros. When your average computer user finds out that macros are all about little bits of code, the fear in their hearts becomes palpable, all the way from here. And so today I want to ease those fears and invite those of you who've been curious about macros to take the plunge and finally learn about them."

Dummies.com


See all Topics

Labels: , , , ,


<Doug Klippert@ 3:33 AM

Comments: Post a Comment


  Saturday, November 12, 2016 – Permalink –

Short Menu Change

In context

Right clicking an object can display a shortcut menu.
Here are some instructions about how to write a macro that will edit that menu.

Right Click


See all Topics

Labels: , , , , , , ,


<Doug Klippert@ 3:55 AM

Comments: Post a Comment


  Wednesday, August 10, 2016 – Permalink –

Hidden Macros Names and Shortcuts

Revealed


Word has built in macros to perform routine actions such as using the Format Painter to copy formatting.

Rather than trying to guess the name or look up the shortcut keys, use this seldom mentioned trick to find toolbar macro names.

Press the three key combination of Ctrl, Alt, and + (the plus sign on the Numbers keypad).

The mouse pointer changes to a 4-leaf clover.

Click on a toolbar icon. Word will display a form revealing the macro name and the assigned shortcuts.



(It works the same way in 2007+)


See all Topics

Labels: , , ,


<Doug Klippert@ 3:55 AM

Comments: Post a Comment


  Thursday, May 19, 2016 – Permalink –

Dynamic Tabs

Change tab names automatically


Changing the names of tabs is easy, just double click the tab or right click and choose rename.

Allen Wyatt has a small piece of code that will automatically update the tab name based on the value of a cell in the spreadsheet.


Sub myTabName()
ActiveSheet.Name = ActiveSheet.Range("A1")
End Sub

Allen also has some error checking code on his site:

Dynamic Worksheet Tabs


Dick Kusleika suggests another way using a change event:

Naming a sheet based on a cell


See all Topics

Labels: , , , , , , ,


<Doug Klippert@ 3:30 AM

Comments: Post a Comment


  Thursday, April 28, 2016 – Permalink –

Large Text Files

Split between worksheets


While this problem is alleviated in Excel 2007+ with its 1,048,576 rows by 16,348 columns, The old XL versions are still here.

Text files with a large number of records are better handled in a program like Access. Having said that, there can be times that these lists must be imported into Excel. If the file has over 65,536 records, the data will not fit on a single worksheet.

Here's a Microsoft Knowledge Base article with the macro code needed to bring oversized text data into Excel and split it into multiple worksheets:

Sub LargeFileImport() 'Dimension Variables Dim ResultStr As String Dim FileName As String Dim FileNum As Integer Dim Counter As Double 'Ask User for File's Name FileName = InputBox("Please enter the Text File's name, e.g. test.txt") 'Check for no entry If FileName = "" Then End 'Get Next Available File Handle Number FileNum = FreeFile() 'Open Text File For Input Open FileName For Input As #FileNum 'Turn Screen Updating Off Application.ScreenUpdating = False 'Create A New WorkBook With One Worksheet In It Workbooks.Add template:=xlWorksheet 'Set The Counter to 1 Counter = 1 'Loop Until the End Of File Is Reached Do While Seek(FileNum) <= LOF(FileNum) 'Display Importing Row Number On Status Bar Application.StatusBar = "Importing Row " & _ Counter & " of text file " & FileName 'Store One Line Of Text From File To Variable Line Input #FileNum, ResultStr 'Store Variable Data Into Active Cell If Left(ResultStr, 1) = "=" Then ActiveCell.Value = "'" & ResultStr Else ActiveCell.Value = ResultStr End If 'For Excel versions before Excel 97, change 65536 to 16384 If ActiveCell.Row = 65536 Then
'If On The Last Row Then Add A New Sheet
ActiveWorkbook.Sheets.Add
Else
'If Not The Last Row Then Go One Cell Down
ActiveCell.Offset(1, 0).Select
End If
'Increment the Counter By 1
Counter = Counter + 1
'Start Again At Top Of 'Do While' Statement
Loop
'Close The Open Text File
Close
'Remove Message From Status Bar
Application.StatusBar = False

End Sub


Importing Text Files Larger Than 16,384/65,536 Rows

Notice the code about 17 lines from the bottom of the macro.

'For xl97 and later change 16384 to 65536.

Also, after import, the data must be parsed. Use Data>Text to columns.

If you have not worked with macros before, Dave McRitchie has a tutorial:

Getting Started with Macros and User Defined Functions


See all Topics

Labels: , , , ,


<Doug Klippert@ 3:28 AM

Comments: Post a Comment


  Thursday, February 18, 2016 – Permalink –

Tabs with Numbers of the Week

Count to 52



Excel no longer has a theoretical limit on the number of worksheets in a workbook. One common use of this ability is to add a worksheet for each week in the year.

Here's a macro that does the trick:
Sub YearWorkbook()
Dim iWeek As Integer
Dim sht As Variant
Application.ScreenUpdating = False
Worksheets.Add After:=Worksheets(Worksheets.Count), _
Count:=(52 - Worksheets.Count)
iWeek = 1
For Each sht In Worksheets
sht.Name = "Week " & Format(iWeek, "00")
iWeek = iWeek + 1
Next sht
Application.ScreenUpdating = True
End Sub

ExcelTips.VitalNews.com:
Naming tabs for weeks


See all Topics

Labels: , , , , ,


<Doug Klippert@ 3:57 AM

Comments: Post a Comment


  Thursday, December 17, 2015 – Permalink –

Animate Window Size

So cool!


The following macro has little or no practical computing value, but it can add a "way cool" element when a worksheet is unhidden.
There are three states that a worksheet can be in; Minimized, Maximized, and Normal.

This macro will gradually resize a worksheet from small to Maximized. The worksheet appears to be growing:

Sub SheetGrow()
Dim x As Integer
With ActiveWindow
.WindowState = xlNormal
.Top = 1
.Left = 1
.Height = 50
.Width = 50

For x = 50 To Application.UsableHeight
.Height = x
Next x

For x = 50 To Application.UsableWidth
.Width = x
Next x

.WindowState = xlMaximized
End With
End Sub


From AutomateExcel.com:
ActiveWindow.WindowState
(By Mark William Wielgus)




Also fun:

Sub SheetGrow()

Dim x As Integer, xmax As Integer

With ActiveWindow

.WindowState = xlNormal

.Top = 1

.Left = 1

.Height = 50

.Width = 50



If Application.UsableHeight > Application.UsableWidth Then

xmax = Application.UsableHeight

Else

xmax = Application.UsableWidth

End If

For x = 50 To xmax

If x <= Application.UsableHeight Then .Height = x

If x <= Application.UsableWidth Then .Width = x

Next x

.WindowState = xlMaximized

End With

End Sub



# posted by Joerd


See all Topics

Labels: , , , , , ,


<Doug Klippert@ 3:45 AM

Comments: Post a Comment


  Friday, September 25, 2015 – Permalink –

Time Without Limits

No Delimiters


Excel is most happy when you enter dates and times with the correct separators.

1/1/2004 is a good date. So is 1-1-2004.

If you just entered 112004 in a cell formatted as a date you'll get:

Wednesday, August 27, 2206

the 112,004th day since January 1, 1900.

Chip Pearson has come up with VBA code, using the Worksheet_Change event procedure, that will allow you to enter dates without dashes or slashes.



See:
Date And Time Entry


See all Topics

Labels: , , , , , , ,


<Doug Klippert@ 3:55 AM

Comments: Post a Comment


  Friday, September 11, 2015 – Permalink –

Locate Duplicates with Conditional Formatting

Highlight entries


Conditional formatting can be set up by selecting the whole range, or for the first cell in the range and then copy down that conditional format. I find it is usually just as easy to select the whole range to start with. The formula will adjust itself.

In this example, cell B2 has a heading of Product Numbers.

Select cell B3 (or the entire targeted range) and from the menu.

Select Format > Conditional Formatting.

The Conditional Formatting dialog opens with the initial dropdown saying
"Cell Value Is".

Click the arrow next to this, and choose
"Formula Is".

After selecting "Formula Is", the dialog box changes appearance.
Instead of boxes for "Between x and y", there is now a single formula box.

You can type in any formula as long as that formula will evaluate to TRUE or FALSE.

The formula to type in the box is
=COUNTIF(B:B,B3)>1

Conditional Formatting

This says, "look through the entire range of column B.

Count how many cells in that range are the same value as what is in B3."
(In the graphic, B7 is the Active cell.)

That same comparison will be made in every cell that contains the conditional formatting.

(If your data is in column E and you are setting the first conditional formatting up in E5, the formula would be =COUNTIF(E:E,E5)>1.)

Anytime a duplicate appears in the range, it will receive the special formatting.

In this example, any time a duplicate number appears anywhere in column B, even if it is not itself formatted, the selected range will reflect the duplicate.

=COUNT(B:B,B3)>2 would count entries that appear more than two times.
=COUNT(B:B,B3)=2 would count entries that appear twice.

If you want only a part of the column in the formula, it is easier to use absolute addresses, such as =COUNT($B$3:$B$200,B3)>1

Adapted from MrExcel.com

Also see:
Chip Pearson's discussion of duplicates:
Duplicate And Unique Items In Lists
and:

Contextures.com:
Conditional Formatting
(See Hide Duplicate Values)


See all Topics

Labels: , , , , , ,


<Doug Klippert@ 3:11 AM

Comments: Post a Comment


  Thursday, September 10, 2015 – Permalink –

Sort worksheets

Order tabs


Worksheets can be dragged and dropped into any order required. They can be set up in numeric or alpha order, but doing it by hand is a bother.

Chip Pearson has written some macros that will do the job for you:
  • Sorting Worksheets In Alphabetical Order
  • Sorting In Custom Order
  • Grouping Sheets By Color

Here's the code to sort by tab color:

Sub GroupSheetsByColor()
Dim Ndx As Long
Dim Ndx2 As Long
For Ndx = 1 To Worksheets.Count - 1
For Ndx2 = Ndx To Worksheets.Count
If Worksheets(Ndx2).Tab.ColorIndex = _
Worksheets(Ndx).Tab.ColorIndex Then
Worksheets(Ndx2).Move after:=Worksheets(Ndx)
End If
Next Ndx2
Next Ndx
End Sub


Sorting Worksheets In A Workbook

(The colorindex variable chooses one of the 56 colors in Excel's basic palette.
Here are all the colors and numbers as compiled by F. David McRitchie:
Excel Colors )


See all Topics

Labels: , , , , ,


<Doug Klippert@ 3:26 AM

Comments: Post a Comment


  Friday, August 14, 2015 – Permalink –

Calendar

One day at a time


Here are some links with downloadable examples, and some code that can be used to create calendars in Excel:

Andrew Engwirda's Excel tips:

Excel Calendar

Calendar Toolbar

He also suggests:
"Tushar Mehta has a very nice calendar too which can be found here:

Calendar"

Erlandsen Data Consulting:
"Create a Create a simple calendar for each month or a small calendar (pocket) for a whole year.

The calendar automatically formats some holidays (included Easter Sunday etc.), you might need to customize the workbook to suit your needs."

MakeCalendarPlanner()
(Evelyn Woolston scroll down to entry #5)

Microsoft Templates:

Calendar templates


See all Topics

Labels: , , , , , ,


<Doug Klippert@ 3:29 AM

Comments: Post a Comment


  Tuesday, July 21, 2015 – Permalink –

List All Files

All files in a folder


Here is a macro that will produce a list of all the files in a selected folder.
  • The folder name for the listed files
  • The file names of the files found
  • The file sizes of the files found
  • The dates and times of the files found
  • The total number of files listed
Macro to List All Files in a Folder


See all Topics

Labels: , , , , ,


<Doug Klippert@ 3:41 AM

Comments: Post a Comment


  Saturday, June 06, 2015 – Permalink –

Formatting Code for Headers and Footers

Roll your own


From Microsoft support:

The following list contains the format codes that you can use in headers and footers.

Codes to format text ("&" is an ampersand - Shift+7)

&L



Left-aligns the characters that follow.



&C



Centers the characters that follow.



&R



Right-aligns the characters that follow.



&E



Turns double-underline printing on or off.



&X



Turns superscript printing on or off.



&Y



Turns subscript printing on or off.



&B



Turns bold printing on or off.



&I



Turns italic printing on or off.



&U



Turns underline printing on or off.



&S



Turns strikethrough printing on or off.



&"fontname"



Prints the characters that follow in the specified
(font. Be sure to include the quotation marks around the font name.)



&nn



Prints the characters that follow in the specified
(font size. Use a two-digit number to specify a size in points.)

Codes to insert specific data

&D
Prints the current date
&T
Prints the current time
&F
Prints the name of the document
&A
Prints the name of the workbook tab (the "sheet name")
&[File]
Also prints file name
&[Path]
Prints path
&[Picture]
Opens dialog box to select graphic (2003)
&P
Prints the page number
&P+number
Prints the page number plus number
&P-number
Prints the page number minus number
&&
Prints a single ampersand
&N
Prints the total number of pages in the document


In a macro, to use multiple lines in a header, use either of the following methods:
  • Use CHR(10) to insert a linefeed character.
  • Use CHR(13) to insert a carriage return character.
The article also includes the VBA to create a macro that will insert header/footer information:
Microsoft KB213618

Also: Daily Dose of Excel: Formatting Footers in VBA


See all Topics

Labels: , , , , , , ,


<Doug Klippert@ 3:42 AM

Comments: Post a Comment


  Thursday, May 28, 2015 – Permalink –

Run a Macro From a Cell

How to do the impossible (almost)



There are times when it might be nice to run a macro from a cell function.
Something like : if a cell has a certain value, a macro will run:

=IF(A1>10,Macro1)

You can not initiate a macro from a worksheet cell function. However, you can use the worksheet's Change event to do something like this:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Not Application.Intersect(Target, Range("A1")) Is Nothing Then
If Target.Value > 10 Then
MsgBox "Put your macro code here in place of the MsgBox line"
End If
End If
End Sub

When A1 is changed to a value greater than 10, the macro code will run.

To get to the Worksheet Event code, right-click the sheet tab and choose View Code.


Worksheet code

From CPearson.com
Also see:
Change Events

Also:
Microsoft KnowledgeBase:
How to Run a Macro When Certain Cells Change

After posting this, Ross Mclean came up with a great work around using a User Defined Function.

Public Function RMAC(ByVal Macro_Name As String,  _
ByVal Arg1 As Variant)
RMAC = Application.Run(Macro_Name, Arg1)
End Function


Keep in mind that some commands will be ignored. A macro run from the worksheet like this will not change the Excel environment.

For example (watch line wrap), this VBA code:
Public Function RMAC _
(ByVal Macro_Name As String, _
ByVal Arg1 As Variant)
RMAC = Application.Run _
(Macro_Name, Arg1)
End Function

Sub MyMacro(arg As String)
ActiveCell.Interior.ColorIndex _
= 3
Beep
End Sub


when invoked by this worksheet formula:

=rmac("MyMacro","yada")


runs the sub MyMacro with some modification. The Beep is executed, the cell color change is not.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
Tutorials and Custom Solutions
http://PeltierTech.com/


See all Topics

Labels: , , , , ,


<Doug Klippert@ 3:47 AM

Comments: Post a Comment


  Wednesday, May 27, 2015 – Permalink –

Signing Macros

Security levels


There are three levels of Macro security:

High:
A computer user can open without a prompt a digitally signed project from a trusted publisher. Otherwise, the application blocks opening signed projects from untrusted publishers as well as unsigned projects.
Medium:
A computer user can open without a prompt a digitally signed project from a trusted publisher. In addition, you can also designate the publisher of a signed project as trusted so their projects will open without a prompt in the future. Unsigned projects are always prompted with a reminder that the file may contain potentially harmful code, but users can elect to open them anyway.
Low:
A computer user can open an unsigned project without a prompt. When users make a Low security setting, they're reminded that they aren't protected from potentially unsafe macros.
Securing Access Databases
"If you've used Access 2003, you've probably seen several security warning messages - Access 2003 cares about your security. An important part of Access 2003 security is digitally signing your code. As Rick Dobson shows, you can do it, but preparing for digital signing is critical.

A digital signature acts like shrink-wrap on your project: Clients know that they're getting a copy directly from you that no one else modified. Clients will also know that they're working with "your" code and not any version of it modified by a third party. As computing moves forward into a "security conscious" era, learning how to acquire and use a digital certificate is also important for interfacing with organizations that adopt policies of only running digitally signed Access 2003 projects: Your users may refuse to accept software from you that isn't shrink-wrapped."
Also:
Signing Access Projects
Other links:

How to make sure that your Office document has a valid digital signature in 2007 Office products and in Office 2003


See all Topics

Labels: , , , ,


<Doug Klippert@ 3:14 AM

Comments: Post a Comment


  Thursday, May 14, 2015 – Permalink –

Show Formulas in Cell Comments

Display properties


Select the cells and then run this macro:

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:=" Address:  " & cell.Address(0, 0) & Chr(10) & _
" Value:  " & cell.Value & Chr(10) & _
" Format:  " & cell.NumberFormat & Chr(10) & _
" Formula: " & cell.Formula
On Error GoTo 0
End If
Next cell

End Sub

Formulas in Comments

by David McRitchie

Also:
Show FORMULA of another cell in Excel


See all Topics

Labels: , , , , , ,


<Doug Klippert@ 3:37 AM

Comments: Post a Comment


  Sunday, May 10, 2015 – Permalink –

Customize date in footer

Formatting



This subroutine inserts the current date in the footer of all sheets in the active workbook. This process can be accomplished without a macro, however, you'll need the macro if you want to specify the formatting of the current date. An example of the return generated by running this macro is Saturday, May 10, 2015.

Sub PutCurrentDateInCenterFooterAllSheetsInWorkbook()
For Each oSheet In ActiveWorkbook.Sheets
oSheet.PageSetup.CenterFooter = Format(Now(), "dddd mmmm dd, yyyy")
Next
End Sub


You can change the word CenterFooter to CenterHeader. You could also use LeftHeader, RightHeader, LeftFooter, or RightFooter.

Microsoft KnowledgeBase:
Macro to Change the Date/Time Format in a Header/Footer


See all Topics

Labels: , , , , , , ,


<Doug Klippert@ 3:48 AM

Comments: Post a Comment


  Wednesday, April 29, 2015 – Permalink –

Printing

Macro control


Here are some useful macros concerning Excel and printing.
They were written by Ole P. Erlandsen of:

ERLANDSEN DATA CONSULTING
Change the default printer
Change the default printer with a macro.
Insert headers and footers
This example macro inserts a header/footer in every worksheet in the active workbook. It also inserts the complete path to the workbook.
Print all workbooks in a folder
With these macros you can print all workbooks in a selected folder. You have more control with what is printed than you have if you do this from Windows Explorer.
Print multiple selections on one sheet
If selected multiple cell ranges is printed out on different sheets, you can use this macro example to print all the selected areas on one sheet.
Select a printer tray before printing
In Excel you don't have the opportunity to set the properties FirstPageTray or OtherPagesTray like you can in Word. It's possible to create a simple solution by using SendKeys.



See all Topics

Labels: , , , , , , , ,


<Doug Klippert@ 3:28 AM

Comments: Post a Comment