VBA for Excel – Syntax Examples and Samples

When switching between programming languages, it can be a pain to remember the syntax.

To help, here is a “cheat sheet” of syntax for VBA for Excel.

In addition, there are two handy samples of VBA code:

  • Identifying the worksheet’s lowest non-empty ROW (into a variable called “last_row”) and right-most non-empty COLUMN (into a variable called “last_col”)
  • Writing all the values of a worksheet’s column A (each value on a new row) as a text file to the active directory
Continue reading “VBA for Excel – Syntax Examples and Samples”

VBA code for creating text files based on data fields

I needed to create a lot of text files that were similar to one another except they were altered depending on a simple database. It would be something like an old-school MS Word “mail merge,” except it would create separate text files. I wanted an easy-to-use tool, so I put together something in Excel using VBA. The tool could create thousands of text files that vary on (up to) 15 different fields (i.e., dimensions). For example, this tool could create many HTML files that are all alike except for a product description, a photo, and a URL link. My original purpose for creating the tool was to setup the coarse-grained parallelization of a CPU-intensive Monte Carlo simulation.
Continue reading “VBA code for creating text files based on data fields”

VBA code for performing many searches-and-replaces in an Excel file

I had a long list of search-and-replace tasks that I needed to do on a text file. I wanted an easy-to-use tool, so I put together something in Excel using VBA. The tool starts with a “BEFORE replacing” worksheet and then creates an “AFTER replacing” worksheet. The replacements are performed based on a “Definitions of Replacements” worksheet, which directs the macro to replace everything in the first column with a paired item in the second column.
Continue reading “VBA code for performing many searches-and-replaces in an Excel file”

VBA code for removing all custom animations from a PowerPoint presentation

When I post my PowerPoint slides on my website for my students, I sometimes remove the animations. Animations are nice for controlling when things appear on the screen during the discussion. (Does anyone remember the old technique of covering part of the overhead slide with paper?) However, my purpose in releasing my PowerPoint slides is to give students a record of what we cover in the class discussion, so animations only get in the way for students.

To make it easier for me to create a student version of the slides, I use the following VBA code to delete all the custom PowerPoint animations in a file. Continue reading “VBA code for removing all custom animations from a PowerPoint presentation”