SlideShare a Scribd company logo
1. Declare a variable in VBA using the Dim keyword.
1. Public Sub number()
Dim x As Integer
x = 36
Range("A5").Value = x
End Sub
2. Public Sub string_value()
Dim book As String
Country = "India"
Range("F3").Value = Country
End Sub
3. Public Sub double_value()
Dim book As Double
a = 10.5
MsgBox "Value of a is " & a //Displaying a message box
End Sub
4. Public Sub FunWithProcedures()
Dim userInput As String
userInput = "Excel VBA"
ActiveCell.Value = userInput
End Sub
2. Display your name when you click on a button
Sub name()
MsgBox (“My name is Daniel”)
End Sub
3. Display the number present in a cell when you run the macro.
Sub number()
MsgBox ("Entered value is " & Range("A1").Value)
End Sub
4. Display a message when you Open the Excel Workbook
Sub Auto_Open()
MsgBox ("Welcome to our Workbook")
End Sub
5. Click a button that will display your name.
Sub Entername()
Dim name As String
name = InputBox("Enter you name")
MsgBox "Hello my name is " + name
End Sub
6. Add a value to a cell
Public Sub addvalue()
Range("B3").Value = 3
End Sub
7. Add a value to a cell or a range of cells
Public Sub add_value()
Range("A1:A4").Value = 10
End Sub
Public Sub add_value()
Range("A1:A4").Value = 30
Range("C1:C4,D1:D4").Value = 20
End Sub
Public Sub add_value()
Range("qty").Value = 15
Range("C1:C4,D1:D4").Value = 20
End Sub
Public Sub add_value()
Cells(3, 2).Value = 10
Range(Cells(1, 2), Cells(3, 5)).Value = 30
End Sub
8. Declare a range object using Dim and Set.
Public Sub add_values()
Dim example As Range
Set example = Range("A1:C4")
example.Value = 10
End Sub
9. Select a range of cells in the same sheet
Public Sub Select_range()
Dim example As Range
Set example = Range("A1:C4")
example.Select
End Sub
10. Select a range of cells in another sheet
Public Sub Select_range()
Worksheets(5).Activate
Worksheets(5).Range("B2:C7").Select
End Sub
11. Copy cell contents and paste to another location
Public Sub copy_paste()
Range("A1:C4").Select
Selection.Copy
Range("E6").Select
ActiveSheet.Paste
End Sub
12. Count the number of cells in a range
Public Sub count_cells()
Dim example As Range
Set example = Range("A1:C4")
MsgBox example.Count
End Sub
13. To count the rows and columns in a selected range
Public Sub count_rows()
Dim example As Range
Set example = Range("A1:C4")
MsgBox (example.Columns.Count)
End Sub
14. Using the if statement in VBA
Public Sub If_stmt()
Dim score As Integer, result As String
score = Range("A1").Value
If score >= 60 Then result = "pass"
Range("B1").Value = result
End Sub
Public Sub If_stmt()
Dim userInput As String
userInput = "100"
If userInput > 50 Then
ActiveCell.Value = “It’s a valid entry”
Else
ActiveCell.Value = userInput
End If
End Sub
15. Using the if-else statement in VBA
Public Sub If_else_stmt()
Dim score As Integer, result As String
score = Range("A1").Value
If score >= 40 Then
result = "pass"
Else
result = "fail"
End If
Range("B1").Value = result
End Sub
16. Use the For loop to add the first 10 positive integers.
Sub AddNumbers()
Dim Total As Integer
Dim Count As Integer
Total = 0
For Count = 1 To 10
Total = Total + Count
Next Count
MsgBox Total
End Sub
17. Add all the positive integers in a certain range starting from 10.
Sub AddEvenNumbers()
Dim a As Integer
Dim Total As Integer
Dim Count As Integer
a = 25
Total = 0
For Count = 10 To a Step 2
Total = Total + Count
Next Count
MsgBox Total
End Sub
18. Using the do-while loop in VBA
Public Sub Loops()
Dim i As Integer
i = 1
Do While i < = 7
If ActiveCell.Value < 30 Then
ActiveCell.Interior.Color = RGB(200, 255, 150)
End If
ActiveCell.Offset(1, 0).Select
i = i + 1
Loop
End Sub
Use the do-while loop to add the first 10 positive integers
Sub AddFirst10PositiveIntegers()
Dim i As Integer
i = 1
Do While i <= 10
Result = Result + i
i = i + 1
Loop
MsgBox Result
End Sub
19. Concatenate strings in VBA
Sub join()
Dim text1 As String, text2 As String
text1 = "Hey"
text2 = "Rolee"
MsgBox text1 & " " & text2
End Sub
20. Check the length of string
Public Sub length()
Dim text As String
text = "Excel VBA Tutorial"
MsgBox Len(text)
End Sub

More Related Content

Similar to Excel Macros and VBA Demo.docx

The Ring programming language version 1.5.4 book - Part 179 of 185
The Ring programming language version 1.5.4 book - Part 179 of 185The Ring programming language version 1.5.4 book - Part 179 of 185
The Ring programming language version 1.5.4 book - Part 179 of 185
Mahmoud Samir Fayed
 
C Programing Arithmetic Operators.ppt
C Programing Arithmetic Operators.pptC Programing Arithmetic Operators.ppt
C Programing Arithmetic Operators.ppt
GAURAVNAUTIYAL19
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
vikrammutneja1
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
Rakotoarison Louis Frederick
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2
Warui Maina
 
Python Modules, Packages and Libraries
Python Modules, Packages and LibrariesPython Modules, Packages and Libraries
Python Modules, Packages and Libraries
Venugopalavarma Raja
 
Excel formulas
Excel formulasExcel formulas
Excel formulas
devangimehta
 
Python_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxPython_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptx
SahajShrimal1
 
Python Modules and Libraries
Python Modules and LibrariesPython Modules and Libraries
Python Modules and Libraries
Venugopalavarma Raja
 
Chapter 16-spreadsheet1 questions and answer
Chapter 16-spreadsheet1  questions and answerChapter 16-spreadsheet1  questions and answer
Chapter 16-spreadsheet1 questions and answer
RaajTech
 
The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189
Mahmoud Samir Fayed
 
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
Bottom of FormCreate your own FunctionFunctionsFor eac.docxBottom of FormCreate your own FunctionFunctionsFor eac.docx
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
AASTHA76
 
CMIS 102 Hands-On LabWeek 6OverviewThis hands-on lab .docx
CMIS 102 Hands-On LabWeek 6OverviewThis hands-on lab .docxCMIS 102 Hands-On LabWeek 6OverviewThis hands-on lab .docx
CMIS 102 Hands-On LabWeek 6OverviewThis hands-on lab .docx
clarebernice
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
Mohamed Ahmed
 
python_lab_manual_final (1).pdf
python_lab_manual_final (1).pdfpython_lab_manual_final (1).pdf
python_lab_manual_final (1).pdf
keerthu0442
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
Anonymous9etQKwW
 
Excel advanced formulas
Excel advanced formulasExcel advanced formulas
Excel advanced formulas
VikamSathish
 
Vbscript
VbscriptVbscript
Vbscript
Deepthi Reddy
 
Useful macros and functions for excel
Useful macros and functions for excelUseful macros and functions for excel
Useful macros and functions for excel
Nihar Ranjan Paital
 
The Ring programming language version 1.2 book - Part 11 of 84
The Ring programming language version 1.2 book - Part 11 of 84The Ring programming language version 1.2 book - Part 11 of 84
The Ring programming language version 1.2 book - Part 11 of 84
Mahmoud Samir Fayed
 

Similar to Excel Macros and VBA Demo.docx (20)

The Ring programming language version 1.5.4 book - Part 179 of 185
The Ring programming language version 1.5.4 book - Part 179 of 185The Ring programming language version 1.5.4 book - Part 179 of 185
The Ring programming language version 1.5.4 book - Part 179 of 185
 
C Programing Arithmetic Operators.ppt
C Programing Arithmetic Operators.pptC Programing Arithmetic Operators.ppt
C Programing Arithmetic Operators.ppt
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2
 
Python Modules, Packages and Libraries
Python Modules, Packages and LibrariesPython Modules, Packages and Libraries
Python Modules, Packages and Libraries
 
Excel formulas
Excel formulasExcel formulas
Excel formulas
 
Python_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxPython_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptx
 
Python Modules and Libraries
Python Modules and LibrariesPython Modules and Libraries
Python Modules and Libraries
 
Chapter 16-spreadsheet1 questions and answer
Chapter 16-spreadsheet1  questions and answerChapter 16-spreadsheet1  questions and answer
Chapter 16-spreadsheet1 questions and answer
 
The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189
 
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
Bottom of FormCreate your own FunctionFunctionsFor eac.docxBottom of FormCreate your own FunctionFunctionsFor eac.docx
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
 
CMIS 102 Hands-On LabWeek 6OverviewThis hands-on lab .docx
CMIS 102 Hands-On LabWeek 6OverviewThis hands-on lab .docxCMIS 102 Hands-On LabWeek 6OverviewThis hands-on lab .docx
CMIS 102 Hands-On LabWeek 6OverviewThis hands-on lab .docx
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
 
python_lab_manual_final (1).pdf
python_lab_manual_final (1).pdfpython_lab_manual_final (1).pdf
python_lab_manual_final (1).pdf
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
Excel advanced formulas
Excel advanced formulasExcel advanced formulas
Excel advanced formulas
 
Vbscript
VbscriptVbscript
Vbscript
 
Useful macros and functions for excel
Useful macros and functions for excelUseful macros and functions for excel
Useful macros and functions for excel
 
The Ring programming language version 1.2 book - Part 11 of 84
The Ring programming language version 1.2 book - Part 11 of 84The Ring programming language version 1.2 book - Part 11 of 84
The Ring programming language version 1.2 book - Part 11 of 84
 

Recently uploaded

一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
nuttdpt
 
State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023
kuntobimo2016
 
Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......
Sachin Paul
 
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
nyfuhyz
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
sameer shah
 
The Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series DatabaseThe Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series Database
javier ramirez
 
Analysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performanceAnalysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performance
roli9797
 
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdfUdemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
Fernanda Palhano
 
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging DataPredictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Kiwi Creative
 
Intelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicineIntelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicine
AndrzejJarynowski
 
Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
vikram sood
 
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
bopyb
 
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
v7oacc3l
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
Roger Valdez
 
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
zsjl4mimo
 
Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
jerlynmaetalle
 
Challenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more importantChallenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more important
Sm321
 
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
Timothy Spann
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
aqzctr7x
 
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
nuttdpt
 

Recently uploaded (20)

一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
 
State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023
 
Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......
 
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
 
The Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series DatabaseThe Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series Database
 
Analysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performanceAnalysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performance
 
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdfUdemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
 
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging DataPredictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
 
Intelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicineIntelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicine
 
Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
 
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
 
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
 
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
 
Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
 
Challenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more importantChallenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more important
 
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
06-12-2024-BudapestDataForum-BuildingReal-timePipelineswithFLaNK AIM
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
 
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
 

Excel Macros and VBA Demo.docx

  • 1. 1. Declare a variable in VBA using the Dim keyword. 1. Public Sub number() Dim x As Integer x = 36 Range("A5").Value = x End Sub 2. Public Sub string_value() Dim book As String Country = "India" Range("F3").Value = Country End Sub 3. Public Sub double_value() Dim book As Double a = 10.5 MsgBox "Value of a is " & a //Displaying a message box End Sub 4. Public Sub FunWithProcedures() Dim userInput As String userInput = "Excel VBA" ActiveCell.Value = userInput End Sub 2. Display your name when you click on a button Sub name() MsgBox (“My name is Daniel”) End Sub 3. Display the number present in a cell when you run the macro. Sub number() MsgBox ("Entered value is " & Range("A1").Value) End Sub 4. Display a message when you Open the Excel Workbook Sub Auto_Open() MsgBox ("Welcome to our Workbook") End Sub 5. Click a button that will display your name.
  • 2. Sub Entername() Dim name As String name = InputBox("Enter you name") MsgBox "Hello my name is " + name End Sub 6. Add a value to a cell Public Sub addvalue() Range("B3").Value = 3 End Sub 7. Add a value to a cell or a range of cells Public Sub add_value() Range("A1:A4").Value = 10 End Sub Public Sub add_value() Range("A1:A4").Value = 30 Range("C1:C4,D1:D4").Value = 20 End Sub Public Sub add_value() Range("qty").Value = 15 Range("C1:C4,D1:D4").Value = 20 End Sub Public Sub add_value() Cells(3, 2).Value = 10 Range(Cells(1, 2), Cells(3, 5)).Value = 30 End Sub 8. Declare a range object using Dim and Set. Public Sub add_values() Dim example As Range Set example = Range("A1:C4") example.Value = 10 End Sub 9. Select a range of cells in the same sheet Public Sub Select_range() Dim example As Range
  • 3. Set example = Range("A1:C4") example.Select End Sub 10. Select a range of cells in another sheet Public Sub Select_range() Worksheets(5).Activate Worksheets(5).Range("B2:C7").Select End Sub 11. Copy cell contents and paste to another location Public Sub copy_paste() Range("A1:C4").Select Selection.Copy Range("E6").Select ActiveSheet.Paste End Sub 12. Count the number of cells in a range Public Sub count_cells() Dim example As Range Set example = Range("A1:C4") MsgBox example.Count End Sub 13. To count the rows and columns in a selected range Public Sub count_rows() Dim example As Range Set example = Range("A1:C4") MsgBox (example.Columns.Count) End Sub 14. Using the if statement in VBA Public Sub If_stmt() Dim score As Integer, result As String score = Range("A1").Value If score >= 60 Then result = "pass" Range("B1").Value = result End Sub
  • 4. Public Sub If_stmt() Dim userInput As String userInput = "100" If userInput > 50 Then ActiveCell.Value = “It’s a valid entry” Else ActiveCell.Value = userInput End If End Sub 15. Using the if-else statement in VBA Public Sub If_else_stmt() Dim score As Integer, result As String score = Range("A1").Value If score >= 40 Then result = "pass" Else result = "fail" End If Range("B1").Value = result End Sub 16. Use the For loop to add the first 10 positive integers. Sub AddNumbers() Dim Total As Integer Dim Count As Integer Total = 0 For Count = 1 To 10 Total = Total + Count Next Count MsgBox Total End Sub 17. Add all the positive integers in a certain range starting from 10. Sub AddEvenNumbers() Dim a As Integer Dim Total As Integer Dim Count As Integer a = 25 Total = 0 For Count = 10 To a Step 2 Total = Total + Count
  • 5. Next Count MsgBox Total End Sub 18. Using the do-while loop in VBA Public Sub Loops() Dim i As Integer i = 1 Do While i < = 7 If ActiveCell.Value < 30 Then ActiveCell.Interior.Color = RGB(200, 255, 150) End If ActiveCell.Offset(1, 0).Select i = i + 1 Loop End Sub Use the do-while loop to add the first 10 positive integers Sub AddFirst10PositiveIntegers() Dim i As Integer i = 1 Do While i <= 10 Result = Result + i i = i + 1 Loop MsgBox Result End Sub 19. Concatenate strings in VBA Sub join() Dim text1 As String, text2 As String text1 = "Hey" text2 = "Rolee" MsgBox text1 & " " & text2 End Sub 20. Check the length of string Public Sub length() Dim text As String text = "Excel VBA Tutorial" MsgBox Len(text)