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

mikecox39

Board Regular
Joined
Mar 5, 2014
Messages
237
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?
 

tonyyy

Well-known Member
Joined
Jun 24, 2015
Messages
1,647
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:

V_Malkoti

Well-known Member
Joined
Jun 10, 2015
Messages
898
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
 

mikecox39

Board Regular
Joined
Mar 5, 2014
Messages
237
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,077,721
Messages
5,335,816
Members
399,051
Latest member
WPO

Some videos you may like

This Week's Hot Topics

Top