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, February 21, 2008 – 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@ 7:56 AM

Comments: Post a Comment


  Monday, December 17, 2007 – Permalink –

Reset Shortcut Menu

Context menu redo


For some reason (to be determined later) my shortcut menu started to accumulate a string of "Edit Formula" and "Delete Formula" entries.

They didn't cause a problem, but were distracting.

This little VBA code puts it back to the original settings until I can find a cause.


Sub ResetShortcutMenu()
Application.CommandBars("Cell").Reset
End Sub


Macros that Customize and Control Shortcut Menus



See all Topics

Labels: ,


<Doug Klippert@ 5:15 AM

Comments: Post a Comment


  Sunday, December 16, 2007 – Permalink –

Office VBA Stuff

Video + Free code



Quick tips VBA Video


"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



See all Topics

Labels:


<Doug Klippert@ 5:07 AM

Comments: Post a Comment


  Saturday, November 03, 2007 – Permalink –

Automation - VBA - Help File

Office Wide


"Automation (formerly known as OLE Automation) is a feature of the Component Object Model (COM), an industry-standard technology that applications use to expose their objects, methods, and properties to development tools, macro languages, and other applications.

For example, a spreadsheet application might expose a worksheet, chart, cell, or range of cells--each as a different type of object. A word processor might expose objects such as an application, document, paragraph, bookmark, or sentence.


When an application supports Automation, the objects that the application exposes can be accessed through Visual Basic. You can use Visual Basic to manipulate the objects by invoking methods or by getting and setting properties of the objects."


Here's an example:


Inserting Data into a Microsoft Word Document

With Automation code, you can open a Microsoft Word document and move to a bookmark location in the document. The following example opens a Microsoft Word document and inserts text after a bookmark.

This example assumes that you have Microsoft Word on your computer, that you have an existing document called C:\My Documents\WordTest.doc, and that the document contains a pre-defined bookmark named City.


Sub FindBMark()

Dim wordApp As Word.Application
Dim wordDoc As Word.Document
Dim wordRange As Word.Range

Set wordApp = CreateObject("Word.Application")
Set wordDoc = wordApp.Documents.Open("C:\My Documents\Wordtest.doc")

wordApp.Visible = True

' Go to the bookmark named "City".
Set wordRange = wordDoc.Goto(What:=wdGoToBookmark, Name:="City")
wordRange.InsertAfter "Los Angeles"

' Print the document.
wordDoc.PrintOut Background:=False

' Save the modified document.
wordDoc.Save

' Quit Word without saving changes to the document.
wordApp.Quit SaveChanges:=wdDoNotSaveChanges

Set wordApp = Nothing

End Sub



Microsoft Support provides an entire Help file to assist you. It includes theory and examples.

The file is called XPAutomation.chm.

Download it and then double click on the file to run it. You could also set up a shortcut on the desk top, if it will be used frequently.


Microsoft Knowledge Base Article: 302460


This was aimed at Office 2002 but it can be used with later versions:

  • Microsoft Access
  • Microsoft Excel
  • Microsoft Outlook
  • Microsoft PowerPoint
  • Microsoft Word



See all Topics

Labels:


<Doug Klippert@ 6:50 AM

Comments: Post a Comment


  Tuesday, June 19, 2007 – 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@ 6:29 AM

Comments: Post a Comment


  Sunday, April 22, 2007 – Permalink –

Numbers to Words

Cardinal numbers



From the Microsoft Knowledgebase collection:


How to Convert a Numeric Value into English Words - 213360


This support article contains the VBA code needed to create a User Defined Function, You can, then, change 32.50 into "Thirty Two Dollars and Fifty Cents" by entering something like the following formula into a cell:

=SpellNumber(32.50)


Also:

From John Walkenbach:

BAHTTEXT?
If you use Excel 2002+, you may have discovered the new BAHTTEXT worksheet function. This function converts a number to Thai text, and adds a suffix of "Baht".
This is the only built in function that translates numbers to words and then only in Thai.

For Access see:
Access- Numbers to Words


To create cardinal numbers in Word, see:
Word - Numbers to Words



See all Topics

Labels: ,


<Doug Klippert@ 6:36 AM

Comments: Post a Comment


  Friday, April 13, 2007 – 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:
How to Spread Bad Habits



See all Topics

Labels:


<Doug Klippert@ 6:46 AM

Comments: Post a Comment


  Saturday, March 31, 2007 – Permalink –

Comment Code

Edit toolbar



You'll many times want to change blocks of code to comments in VBA modules; temporarily convert a block of VBA code to comments so that it's ignored during a trial run. Inserting an apostrophe before each line of code is a bother. Office 2000+ simplifies this task by letting you convert a block of code to comments with a click of a button.

Open any module in the Visual Basic Editor (VBE), and then choose View>Toolbars and choose Edit from the menu bar to display the Edit toolbar.

Select the lines of code that you want to turn into comments. Then, click the Comment Block button on the Edit toolbar (it's the sixth button in from the RIGHT end of the toolbar).
Each line of the selected code is now preceded with an apostrophe.




To convert the comments back to executable code, select the appropriate lines and click the Uncomment Block button, which is immediately to the right of the Comment Block button.
This, of course, works in any application that uses the VBE.

Ross, suggested that two or three apostrophes (sometimes called inverted commas) be placed around existing comments. When the Comment Block is used, the original comments will not be removed.



See all Topics

Labels: ,


<Doug Klippert@ 7:06 AM

Comments: Post a Comment