Jelen, VBA and Macros; tip 1: don't select anything

mikecox39

Active Member
Joined
Mar 5, 2014
Messages
251
Office Version
  1. 365
Platform
  1. Windows
I'm at the end of chapter 2 of Jelen's book. VBA and macros: Microsoft excel 2010, where he lists several Tips.

I'm confused by the first tip: "don't select anything." Having to do with streamlining Recorded macro code

In the Excel interface you have to select something before you can format it.

But he starts with a Row example then continues his argument with a Column example.

In the row example he choses to format a Row, but in the column example he chooses a format a worksheet; all the columns.

This is confusing because the issue is with not having to select first. That makes sense when your formating the entire worksheet, but not when your streamlining the code created by the recording. What if he was formating a single column, or all the rows?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
mikecox39,

Here's an example of eliminating .Select...

Code:
Sub Macro2()
    Range("A1").Select
    With Selection.Font
        .Name = "Calibri"
        .FontStyle = "Bold"
        .Size = 11
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
    
    Columns("B:B").Select
    Selection.ColumnWidth = 10
    
    '''' The above can be condensed to the following, and will execute more quickly...
    Range("A1").Font.FontStyle = "Bold"
    Columns("B:B").ColumnWidth = 10
End Sub

Cheers,

tonyyy
 
Last edited:
Through user interface, all user actions are manual. User needs to select the cells to tell MS-Excel which cells to act on. Macro recorder records all user actions, which is why recorded code has a lot of select action statements.

In VBA, you don't need to select cells to work on them. The objects that you create (like Range("A:A") or Cells(1, 1)) have cell location information.

For example, when manually trying to format all cells in a column to Bold, user takes two steps:
1. Select column
2. Click on Bold button or hit Ctrl+B
This generates code for two actions/steps taken by user.
Code:
Columns("B:B").Select
Selection.Font.Bold = True

However, when formatting columns through VBA, you don't need to click-select cells. You can directly write statement to format the specific column.
Code:
Columns("B:B").Font.Bold = True
 
Ah, I see! Makes sense now! Thanks for quickly clearing that up.

Tonyyy, I'm really enjoying your book! I'm taking if VERY slowly, reading each chapter twice. I'm determined to learn how to make EX my slave (-:

I am the treasurer of a small condo association and, with the help of many on this forum, have created to very helpful spreadsheet. I've been working on it for several years, which is to say I keep adding "bells and whistles". Now I want to automate it, so when someone else takes over they won't be flumuxed.
 

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top