Consulting

Results 1 to 6 of 6

Thread: Solved: Select vs. Activate

  1. #1
    VBAX Regular
    Joined
    Feb 2006
    Posts
    12
    Location

    Solved: Select vs. Activate

    I am unclear on the differences between select and activate in the context of worksheet or range or cell. The below code snippet uses select. Visually it is distracting to see things flash around if you are doing selects in several different places in sequence. Can I use activate and elimiate the visual effect of select? Or is there another way without select to do the same thing?

    Please do concentrate on what the code does as the visual effect the code creates. I also want to apply your suggestions on other macros.

    [VBA] Sheets("S Rpt").Select
    Range("F3").Select
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
    Selection.RowHeight = 21
    '
    ' copy "task" column format
    '
    Range("H3").Select
    lSRptFirstRow = Selection.Row
    Selection.End(xlDown).Select
    lSRptLastRow = Selection.Row
    '
    Range("H3").Select
    Range(Selection, Cells(lSRptLastRow, Selection.Column)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Cells(3, lSRptTaskCol).Select
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    '
    ' copy "call" column format
    '
    Range("J3").Select
    Range(Selection, Cells(lSRptLastRow, Selection.Column)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Cells(3, lSRptCallCol).Select
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    '
    ' copy "start" column format
    '
    Range("L3").Select
    Range(Selection, Cells(lSRptLastRow, Selection.Column)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Cells(3, lSRptStartCol).Select
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False[/VBA]

  2. #2
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    To stop the visual effects put
    Application.ScreenUpdating = False
    and then when it has finished the code put in
    Application.ScreenUpdating = True.

  3. #3
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    You don't actually need select/activate.

    This code:
    [vba]
    Sheets("S Rpt").Select
    Range("F3").Select
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
    Selection.RowHeight = 21[/vba]
    Can be replaced with this.
    [vba]
    With Sheets("S Rpt").Range("F3")
    .PasteSpecial Paste:=xlPasteFormats
    Application.CutCopyMode = False
    .RowHeight = 21
    End With[/vba]

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    In plain English, you can Select multiple cells, but you can only Activate one at a time; the same goes for worksheets. That's the jist of it.


    As for your code, you might be able to use something like this (untested) ...

    [VBA]Sub test_me()
    Dim lSRptFirstRow As Long, lSRptLastRow As Long
    Dim lSRptTaskCol As Long, lSRptCallCol As Long, lSRptStartCol As Long
    With Sheets("S Rpt")
    .Range("F3").PasteSpecial xlPasteFormats
    .Range("F3").RowHeight = 21
    lSRptFirstRow = 3
    lSRptLastRow = .Range("F3").End(xlDown).Row
    .Range(.Range("H3"), .Cells(lSRptLastRow, 8)).Copy
    .Cells(3, lSRptTaskCol).PasteSpecial xlPasteFormats
    .Range(.Range("J3"), .Cells(lSRptLastRow, 10)).Copy
    .Cells(3, lSRptCallCol).PasteSpecial xlPasteFormats
    .Range(.Range("L3"), .Cells(lSRptLastRow, 12)).Copy
    .Cells(3, lSRptStartCol).PasteSpecial xlPasteFormats
    End With
    Application.CutCopyMode = False
    End Sub[/VBA]

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Also, when you select a range of cells or group of worksheets, the first cell/worksheet selected becomes the active one.

  6. #6
    VBAX Regular
    Joined
    Feb 2006
    Posts
    12
    Location
    As always the advice has been good, thank you.

Posting Permissions

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