How to format a cell using VBA

Burrgogi

Active Member
Joined
Nov 3, 2005
Messages
423
Office Version
  1. 2010
Platform
  1. Windows
Is there any better way to write this code? I'm very much of a VBA novice.

VBA Code:
Sub FormatCell_test()
    Range("I1").VerticalAlignment = xlTop
    Range("I1").Select
    With Selection.Font
        .Name = "Calibri"
        .Size = 13
        .Bold = True
    End With
End Sub


I realize I'm working with 2 objects here, manipulating the font and changing the format of a cell. Just for my own enlightenment purposes, I'm wondering if there's a more efficient way to write the code.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Your code looks good, but you could simplify it and avoid using the Select method. Here's an updated version:
VBA Code:
Sub FormatCell_test()
    With Range("I1")
        .VerticalAlignment = xlTop
        .Font.Name = "Calibri"
        .Font.Size = 13
        .Font.Bold = True
    End With
End Sub
 
Upvote 0
Solution
Your code looks good, but you could simplify it and avoid using the Select method. Here's an updated version:
VBA Code:
Sub FormatCell_test()
    With Range("I1")
        .VerticalAlignment = xlTop
        .Font.Name = "Calibri"
        .Font.Size = 13
        .Font.Bold = True
    End With
End Sub

Ah, that's it. Thanks for the help.
 
Upvote 0

Forum statistics

Threads
1,216,166
Messages
6,129,260
Members
449,497
Latest member
The Wamp

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