EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 120+ Courses All in One Bundle
  • Login
Home VBA VBA Resources VBA Logical Functions VBA Selection
Secondary Sidebar
VBA Logical Functions
  • VBA Logical
    • VBA Do While Loop
    • VBA IF Statements
    • VBA Loops
    • VBA Select Case
    • VBA Else If
    • VBA While Loop
    • VBA Select Cell
    • VBA Break for Loop
    • VBA IF Not
    • VBA Do Until Loop
    • VBA OR
    • VBA Boolean
    • VBA Like
    • VBA For Each Loop
    • VBA Operators
    • VBA Selection
    • VBA DoEvents
    • VBA Do Loop
    • VBA Not
    • VBA With
    • VBA AND

VBA Selection

By Ashwani JaiswalAshwani Jaiswal

VBA Selection

Excel VBA Selection Property

VBA Selection is used for selecting the range from excel worksheet. We can do anything and whatever with the selected range. But while using VBA Selection, we will not be seeing the list of functions we want to perform from where we can choose any as per our need. For this, we need to type that thing manually and it should be correct. So while using VBA Selection, we have to make sure that we will be using the correct function to avoid any kind of error.

How to Use Selection Property in Excel VBA?

Below are the different examples to use Selection property in excel by using VBA code.

You can download this VBA Selection Excel Template here – VBA Selection Excel Template

Excel VBA Selection – Example #1

In this example, we will see a very simple method of applying VBA Selection. For this follow the below steps:

All in One Financial Analyst Bundle(250+ Courses, 40+ Projects)
Financial ModelingInvestment BankingUS GAAPCFA-Level 1 & 2
Equity ResearchM & A ModelingPrivate Equity ModelingForex Trading
Price
View Courses
250+ Online Courses | 40+ Projects | 1000+ Hours | Verifiable Certificates | Lifetime Access
4.9 (86,389 ratings)

Step 1: Go to the VBA window and open a new Module from the list of Insert menu as shown below.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

VBA Selection Example 1-1

Step 2: Now in the newly opened module write the subcategory for VBA Selection as shown below.

Code:

Sub VBASelection()

End Sub

VBA Selection Example 1-2

Step 3: Select any range of cells. It can be any random or specific range of cells with the help of  Select command.

Code:

Sub VBASelection()

Range("A1:C3").Select

End Sub

Range of cells

Step 4: Now, we would try to insert some text in the selected range of cell. For this in the next line of code write Selection.Value. This is used for selecting the cells for putting the value in.

Code:

Sub VBASelection()

Range("A1:C3").Select
Selection.Value

End Sub

VBA Selection Example 1-4

Step 5: At last, we will write any text which we would like to see in the selected range of cell. Let that text be “Excel VBA Selection”.

Code:

Sub VBASelection()

Range("A1:C3").Select
Selection.Value = "Excel VBA Selection"

End Sub

VBA Selection Example 1-5

Step 6: Now run the code by clicking on the Play button which is placed below the menu bar as shown below.

VBA Selection Example 1-6

We will see, from cell A1 to C3, our selected text value has been inserted and the whole is selected as well.

Excel VBA Selection – Example #2

In this example we will see, how Offset function will work along with VBA Selection. For this, follow the below steps:

Step 1: Write the subcategory of VBA Selection as shown below.

Code:

Sub VBASelection2()

End Sub

VBA Selection Example 2-1

Step 2: Select the range of cell as per your need or else we can keep off using the same range of cells which we had seen in the above example.

Code:

Sub VBASelection2()

Range("A1:C3").Select

End Sub

VBA Selection Example 2-2

Step 3: Now choose Offset function along with Selection as shown below. By this Offset function, we would try to offset or move the selection from the next matrix.

Code:

Sub VBASelection2()

Range("A1:C3").Select
Selection.Offset

End Sub

Offset function

Step 4: After that select the offset location where we want to move the selection box from range cells A1 to C3. Let’s try to move it by 1 row and 1 column away from the current position.

Code:

Sub VBASelection2()

Range("A1:C3").Select
Selection.Offset(1, 1).Select

End Sub

Select offset location

Step 5: Now again run the code by pressing F5 key or by clicking on Play Button.

VBA Selection Example 2-5

We will see, now our new selection will be from cell B2 to D4 as shown below. And we had used Select command so that range is in a current selected mode.

Excel VBA Selection – Example #3

In this example, we will see how could we use and change the cell’s interior color with the help of VBA Selection. For this, follow the below steps:

Step 1: Write the subcategory of VBA Selection or in any name as per your choice as shown below.

Code:

Sub VBASelection3()

End Sub

VBA Selection Example 3-1

Again, we will keep on using the same range of cells as we have seen previously with the same inserted text.

Step 2: Select the range of required cell, which is from cell A1 to C3 here.

Code:

Sub VBASelection3()

Range("A1:C3").Select

End Sub

VBA Selection Example 3-2

Step 3: Now for changing the interior color of any cell, we need to use Interior.Color function together along with Selection. This will allow us to use all available vb Color we have in VBA.

Code:

Sub VBASelection3()

Range("A1:C3").Select
Selection.Interior.Color

End Sub

VBA Selection Example 3-3

Step 4: Here we have a variety of colors but all are BASE COLORS. To select any base color we will use vb followed by the name of the color. Let’s consider the interior color to be Green as shown below.

Code:

Sub VBASelection3()

Range("A1:C3").Select
Selection.Interior.Color = vbGreen

End Sub

Interior Color Green

Step 5: Now again run the code by pressing F5 key or by clicking on the Play Button.

VBA Selection Example 3-5

We will see, our selected range of cells are colored as Green.

Excel VBA Selection – Example #4

In this example, we will insert a text in any range of cells and simultaneously we will change the font color for those cell text as well. For this, follow the below steps:

Step 1: Write the subcategory of VBA Selection as shown below.

Code:

Sub VBASelection4()

End Sub

VBA Selection Example 4-1

Step 2: Again select the range of cells where we want to see all this happening. Here, we are again considering the same range.

Code:

Sub VBASelection4()

Range("A1:C3").Select

End Sub

Select the range of cells

Step 3: Again insert any text as we have seen in example-1. We will again use the same line of code here.

Code:

Sub VBASelection4()

Range("A1:C3").Select
Selection.Value = "Excel VBA Selection"

End Sub

VBA Selection Example 4-3

Step 4: Now with the help of Selection use Font.Color function together as shown below. By this, we would be able to select the color which we want to give to selected cell fonts.

Code:

Sub VBASelection4()

Range("A1:C3").Select
Selection.Value = "Excel VBA Selection"
Selection.Font.Color

End Sub

Font.Color function

Step 5: Again, we will use VBA base colors for cell font colors. Let’s use Red color this time starting with vb as shown below.

Code:

Sub VBASelection4()

Range("A1:C3").Select
Selection.Value = "Excel VBA Selection"
Selection.Font.Color = vbRed

End Sub

VBA base colors for cell font colors

Step 6: Now again run the code by pressing F5 key or by clicking on Play Button.

VBA Selection Example 4-6

We will see, in the selected range of cells we will have our text inserted and the color those cells are now Red from default Black.

Pros & Cons of Excel VBA Selection

  • This the direct way of selecting any function.
  • This is applicable to all type of functions.
  • VBA Selection is a much easier way of coding as compared to other forms.
  • As it does not allow the list of possible functions after it, so it becomes little complex to use VBA Selection.

Things to Remember

  • Make sure you use exact function after VBA Selection, to avoid getting error.
  • It is easy and possible to use complex code structure VBA Selection.
  • Once implementation is done, save the file in Macro enabled excel so that you will not lose written code ever.

Recommended Articles

This is a guide to VBA Selection. Here we discuss how to use selection property in Excel by using VBA code along with practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA Conditional Formatting
  2. VBA Get Cell Value
  3. VBA Named Range
  4. VBA RGB
Popular Course in this category
All in One Excel VBA Bundle (120+ Courses)
  120+ Online Courses |  500+ Hours |  Verifiable Certificates |  Lifetime Access
4.9
Price

View Course
1 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign In
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more