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



  Thursday, March 22, 2018 – Permalink –

VBA Named Arguments

An easier read


Use named arguments for cleaner VBA code.

Most likely, you use positional arguments when working with VBA functions. For instance, to create a message box, you probably use a statement that adheres to the following syntax:

MsgBox(prompt[, buttons] [, title] [, helpfile, context])


When you work the MsgBox function this way, the order of the arguments can't be changed.

Therefore, if you want to skip an optional argument that's between two arguments you're defining, you need to include a blank argument, such as:
MsgBox "Hello World!", , "My Message Box"


Named arguments allow you to create more descriptive code and define arguments in any order you wish. To use named arguments, simply type the argument name, followed by :=, and then the argument value.

For instance, the previous statement can be rewritten as:

MsgBox Title:="My Message Box", _
Prompt:="Hello World!"


(To find out a function's named arguments, select the function in your code and press [F1].)


See all Topics

Labels:


<Doug Klippert@ 3:54 AM

Comments: Post a Comment


  Tuesday, January 23, 2018 – Permalink –

Office VBA Tricks

Video + Free code


"Learn tips and use sample code for several Office applications. These tips can help you to be more productive and can also be a starting point for developing your own tools, utilities and techniques."
  • Update Word Document Statistics in the Title Bar
  • Create Outlook Rules Programmatically
  • Delete Repeated Text Throughout a Word Document
  • Run Macros Based on the Value of One or More Excel Spreadsheet Cells
  • Disable Related Controls on a PowerPoint Slide After a User Clicks an Input Control
  • Display Reminder Information When a User Opens an Office Document
  • Synchronize an Access Main Form to a Subform and Vice Versa
  • Log Worksheet Changes to an XML File
  • Merge Body Text from Multiple Outlook E-mail Messages to a Word Document
  • Use the Office Assistant as an Alternative to Displaying and Retrieving User Input
Ten Tips for Office VBA Developers

VBA Tips & Tricks

Getting Started with VBA in Office 2010

Download Office 2013 VBA Documentation


(VBA is VBA and is, in most cases, usable in all versions of Office)


See all Topics

Labels: , , ,


<Doug Klippert@ 3:02 AM

Comments: Post a Comment


  Saturday, September 02, 2017 – Permalink –

Indent Code

Realign a bunch


Indenting blocks of VBA code, such as statements within loops or If...Then statements, makes reading a procedure much easier.

You probably indent a code statement using the [Tab] key, and outdent by using [Shift][Tab].

However, you may not be aware that the [Tab] and [Shift][Tab] techniques also work when multiple code lines are selected.

The Visual Basic Editor also provides Indent and Outdent buttons on the Edit toolbar that allow you to easily reposition blocks of code.


See all Topics

Labels: , , , ,


<Doug Klippert@ 3:35 AM

Comments: Post a Comment


  Tuesday, July 04, 2017 – Permalink –

Select by Code

Programmatically pick cells


Microsoft has provided 22 ways to select cells/ranges by using Visual Basic procedures in Excel.

Here are a few of the subjects covered:

  • How to Select a Cell on the Active Worksheet

  • How to Select a Cell on Another Worksheet in the Same Workbook

  • How to Select a Range of Cells on the Active Worksheet
  • How to Select a Named Range on a Worksheet in a Different Workbook

  • How to Select a Cell Relative to the Active Cell

  • How to Select the Union of Two or More Specified Ranges

  • How to Select the Intersection of Two or More Specified Ranges

  • How to Select the Last Cell of a Column of Contiguous Data

  • How to Select the Blank Cell at Bottom of a Column of Contiguous Data

How to select cells/ranges by using Visual Basic procedures

Dick Kusleika has some comments on the coding:
Spreadsheets are the Devil


See all Topics

Labels: , , , , ,


<Doug Klippert@ 3:43 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


  Thursday, September 22, 2016 – Permalink –

Help IDs

VBA code

When you build a macro, you can call up information from the Excel Help file.
Ron DeBruin has the information needed through 2007-2013.

Right clicking the Helpfile and choosing Properties will show the HP####### number in 2010+.

Help Context IDs for Excel


See all Topics

Labels: , , , ,


<Doug Klippert@ 3:50 AM

Comments: Post a Comment


  Monday, August 15, 2016 – Permalink –

Display Row, Column Headings

User Function



Here's an odd little use of functions.

If you want to display the Row number on a spreadsheet, the formula
=Row()
works just fine.
You could then hide the Row and Column headings and format the Row numbers any way you want. If a Row is deleted the numbers will automatically update.

Column headings are a little harder. The formula =Column() will show the number of the Column, not the letter, i.e. "2" instead of "B".

The following formula extracts the Column letter:

=SUBSTITUTE(ADDRESS(1,COLUMN(),4),"1","")


To break it down:

=ADDRESS(row_num,column_num,abs_num)

This finds the address at Row number "1" and current Column number. The abs_num of "4 " says make the result a relative address.

The formula will produce a result such as "AA1".

SUBSTITUTE(text,old_text,new_text)

This function looks at the address, i.e. "AA1".
It replaces the Row number character ("1") with a null or empty value ("").
The formula will produce a result such as "AA".

Also see Daily Dose of Excel by Dick Kusleika.

Dick mused:'

"Sometime before the year 3,000, Microsoft will hopefully increase the number of columns in Excel (Hey, I can dream can't I). The challenge before you is to write a function that converts a column number to its letter equivalent assuming columns go to ZZZZ. That’s about 450,000 columns - maybe more than I need."


Of course Office 2007+ has taken it up to 16,284 columns.


See all Topics

Labels: , , , , , ,


<Doug Klippert@ 3:27 AM

Comments: Post a Comment


  Saturday, June 11, 2016 – Permalink –

When 28 is 30

How long is a month?


When you use the banker's DAYS360 function to calculate the number of days between two dates, you can get an odd answer.

If you use the DAYS360 function with a start date of February 28 and with an end date of March 28, a value of 28 days is returned.
You expect a value of 30 days to be returned for every full month. (12*30=360)

This behavior may occur if you use the U.S. method, also known as the NASD method, with the DAYS360 function.

To work around this behavior, use the European method with the DAYS360 function. With the European method starting dates and ending dates that occur on the 31st of a month become equal to the 30th of the same month. To use the European method with the DAYS360 function, use the following syntax:

=DAYS360(cell number of start date,cell number of end date,TRUE)

Using FALSE or omitting the third parameter uses the U.S. method

Support.microsoft.com
An unexpected value with the DAYS360 function


See all Topics

Labels: , , , , ,


<Doug Klippert@ 3:43 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


  Sunday, April 17, 2016 – Permalink –

VBA Help

From Microsoft


Here is a Help reference covering the vagaries of VBA.


Office VBA Language Reference


See all Topics

Labels:


<Doug Klippert@ 3:11 AM

Comments: Post a Comment


  Tuesday, March 29, 2016 – Permalink –

Thirtieth Condition Formatting

Three is not always enough


Pre-2007 Excel gives the user the ability to specify up to three conditions under Format>Conditional Formatting.

If that is not enough, here's the code that extends the conditions to 30!




Extended Conditional Formatter

Also see:
Conditional Formatting (including 2007)


See all Topics

Labels: , , , , , , , ,


<Doug Klippert@ 3:09 AM

Comments: Post a Comment


  Sunday, March 06, 2016 – Permalink –

Count the Colors

I bid 3 Red

What if you would like to know the color name or to count or to sum cells by a fill color? There is no built-in function in Excel.

In this case you can make a User Defined Function (UDF).

Here is the sample UDF that you can use to analyze, count and sum the cells depending on their filled color.

These UDF function can be used in the same way as built-in functions that you can use in the worksheet.

  • AnalyzeColor Returns the color name, the color index or color index in RGB.
    Syntax: AnalyzeColor(color range, optional; "text" or "index" or "rgb". When it is omitted "text" is used.)


  • CountColor Counts the number of cells depending on their filled color.
    Syntax: CountColor(color range, target range)


  • SumColor Adds all the numbers in a range of cells depending on their filled color.
    Syntax: SumColor(color range, target range)
Sum and Count by fill color

Chip Pearson:
Working with Cell Colors


See all Topics

Labels: , , , , ,


<Doug Klippert@ 3:37 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


  Monday, January 18, 2016 – Permalink –

Excel-lent E-mail

Outlook, Excel, and VBA


Ron de Bruin, Microsoft MVP - Excel, has put together a collection of VBA routines to make Excel e-mail friendly.

See if these topics tempt you:

Example Code for sending mail from Excel
  • Mail Workbook
  • Mail one Sheet
  • Mail more than one Sheet
  • Mail the Selection or range
  • Mail Every Worksheet with Address in cell A1
  • Mail sheet or sheets to one or more people
  • Mail range or sheet in the body of the mail (Send personalized email)
  • Mail a message to each person in a range with Outlook
  • Mail a message to each person in a range with CDO (no security warnings)
  • Sending a different file to each person in a range with Outlook
  • Zip the ActiveWorkbook and mail it with Outlook
  • Security (Prevent displaying the dialog to Send or not Send)

Also Download Addins for Excel e-mail information

Also see:

John Walkenbach:
Sending Personalized Email from Excel


See all Topics

Labels: , , , , ,


<Doug Klippert@ 3:24 AM

Comments: Post a Comment


  Friday, January 15, 2016 – Permalink –

Where have all the Bytes gone?

Folder size list


You can create a list in Excel of all the folders on a drive and their sizes.
(The credit goes to Peter Beach, an Excel MVP.)

Get Folder Size code

  1. Copy the code and open Excel.
  2. Press Alt+F11 and, if necessary, on the Visual Basic Editor menu, Insert>Module
  3. Paste the code.
  4. You could use Alt+Tab to bring the worksheet forward.
  5. Go to Tools>Macros and run the Macro named "GetFolderListing".

It may take a little time to complete. BTW, if you feel geeky enough, here is a picture of some of the year 2005 MVPs from John Walkenbach's site.


See all Topics

Labels: , , , ,


<Doug Klippert@ 3:11 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