Thanks Thanks:  0
Likes Likes:  0
Results 1 to 4 of 4

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

  1. #1
    Board Regular mikecox39's Avatar
    Join Date
    Mar 2014
    Location
    San Diego
    Posts
    129
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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?

  2. #2
    Board Regular tonyyy's Avatar
    Join Date
    Jun 2015
    Location
    Grants Pass, Oregon
    Posts
    1,425
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)

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

    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 by tonyyy; Oct 3rd, 2017 at 05:34 PM.
    Windoze 7 / Excel 2010

    How to post your Excel data with Excel Jeanie
    How to post your vba code

  3. #3
    Board Regular V_Malkoti's Avatar
    Join Date
    Jun 2015
    Location
    California
    Posts
    869
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

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

    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
    Fus Ro Dah!

  4. #4
    Board Regular mikecox39's Avatar
    Join Date
    Mar 2014
    Location
    San Diego
    Posts
    129
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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.

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com