Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Better than .Select in VBA

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,558
US

This is actually code in VB 6 where I create a lot of reports in Excel.

Usually, to dump data into Excel I use:
Code:
Dim xlApp As Excel.Application

With xlApp
  ....
  .Range("A4:D4).Value = Array("X","Y",123,"AbC")
  ....
which works very well.

But many times I use .Select which may not be very efficient because of .Select (right???) :
Code:
  .Range("C5")[blue].Select[/blue]
  .Selection.Font.Bold = True
  .ActiveCell.FormulaR1C1 = "Some Info"
or to format Cells or Columns:
Code:
  .Cells[blue].Select[/blue]
  .Selection.NumberFormat = "@"

  .Columns("G:M")[blue].Select[/blue]
  .Selection.HorizontalAlignment = xlCenter
  .Columns("A:M").EntireColumn.AutoFit
  .Range("A1").Select

How can I improve this code and avoid use of .Select?


Have fun.

---- Andy
 
You may try something like this:
1) .Range("C5").Font.Bold = True

2) .Cells.NumberFormat = "@"

3) .Columns("G:M").HorizontalAlignment = xlCenter

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Correct - Select and Activate can slow things down.
Code:
  .Range("C5").Select
  .Selection.Font.Bold = True
can be shortened to
Code:
  .Range("C5").Font.Bold = True
Then you can use With to make things a little easier
Code:
    With Range("C5")
        .Font.Bold = True
        .FormulaR1C1 = "Some Info"
    End With

    With Cells
        .NumberFormat = "@"

        .Columns("G:M").HorizontalAlignment = xlCenter
        .Columns("A:M").EntireColumn.AutoFit
        .Range("A1").Select
    End With

[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ 181-2886 before posting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top