Working with the Visual Basic Editor Objects in Visual Basic for Application Subs and Functions in Visual Basic for Application Macro Recorder tool in Visual Basic for Application Import
Trang 2EXCEL VBA
Trang 3Legal Notice
Copyright (c) 2020 Anton Melnyk.
All rights are reserved No portion of this book may be reproduced or duplicated using any form whether mechanical, electronic, or otherwise No portion of this book may be transmitted, stored in a retrieval database, or otherwise made available in any manner whether public or private unless specific permission is granted by the publisher.
This book does not offer advice, but merely provides information The author offers no advice whether medical, financial, legal, or otherwise, nor does the author encourage any person to pursue any specific course of action discussed in this book This book is not a substitute for professional advice The reader accepts complete and sole responsibility for the manner in which this book and its contents are used The publisher and the author will not be held liable for any damages caused.
Trang 4Excel VBA
Step by Step Guide to Learn Excel VBA Programming
with Screenshots and Example Code
Anton Melnyk
Trang 5Working with the Visual Basic Editor
Objects in Visual Basic for Application
Subs and Functions in Visual Basic for Application
Macro Recorder tool in Visual Basic for Application
Important Language Elements in Visual Basic for Application
Range objects in Visual Basic for Application
Workbook objects in Visual Basic for Application
Worksheet objects in Visual Basic for Application
OLEObject objects in Visual Basic for Application
Worksheet Functions in Visual Basic for Application
Control Program Flow and Making Decisions in Visual Basic for Application Events in Visual Basic for Application
Error Handling in Visual Basic for Application
Communication with users in Visual Basic for Application
Add-ins in Visual Basic for Application
Tips and Tricks used in Visual Basic for Application
Code examples used in Visual Basic for Application
Trang 6Excel VBA
VBA stands for Visual Basic for Applications and is the programming language used in all office programs (Excel, Word, Outlook, etc.) This programming language can be used to create all kinds of automation tools and to create relationships between Office programs move data from
one tool to another.
This book will show you how you can harness some of the power of Excel
VBA for yourself
Trang 7Working with the Visual Basic Editor
Visual Basic Editor is the interface where the code can be written To open Visual Basic Editor, you need to follow this steps:
Trang 81 Click on the Developer tab from the top ribbon menu of any Outlook program
If the Developer tab is not visible you have to right click on Home button and click on Customize Quick Access Toolbar Select Customize Ribbon option on the left pane and check Developer checkbox in the right pane
2 Click on the Visual Basic button
OR
Trang 9Press Alt + F11 buttons from the keyboard on Windows configuration
or Opt + F11 buttons from the keyboard on MAC configuration
Trang 10After the Visual Basic Editor is open there can be found multiple sections described below:
insert modules, procedures or user forms format your code (align, horizontal or vertical spacing, arrange buttons, etc.)
debug your code by: step into (run the code line by line), step over (skip some lines of code when running), add a variable to the watch window (check the variable value while running the code)
run the code, break the code or stop the code add some references References are very important when the code uses some other applications For example, if you want
to include some Word references you need to add Microsoft Word as a reference.
include some Add-ins change Window view get some help if is needed All the commands described above have also keyboard shortcuts.
Tool Bar
Tool Bar contains most of the useful commands that are used while
codding This toolbar can be customized with most needed commands Edit Toolbar Buttons:
List Properties / Methods (CTRL + J) – When something from the code (Sub, Function, Variable, Method, etc.) is clicked and
Trang 11this option pressed shows a list of all properties and methods
of that object from the code
List Constants (CTRL + Shift + J) – Displays all the constants available in the code
Quick Info (CTRL + I) - When something from the code (Sub, Function, Variable, Method, etc.) is clicked and this option pressed shows a list of information of that object from the code
Parameter Info (CTRL + Shift + I) – Displays all the
parameter that are needed after click on a Sub/Function name
Toggle Breakpoint (F9) – Inserting a breakpoint in the selected line
Toggle Bookmark – Inserting a bookmark in the selected line Next Bookmark – Move the cursor to the next bookmark in the code
Previous Bookmark – Move the cursor to the previous
bookmark in the code
Clear All Bookmarks – Remove all bookmarks from the code Comment Block – Comment the selected text (used for
commenting multiples lines of code at once)
Uncomment Block – Uncomment the selected text
Standard Toolbar Buttons:
View Microsoft Excel (Alt + F11) – Switch to Microsoft Excel application from Visual Basic Editor
Insert User Form – Allows the user to insert new User Form, Module, Class Module or Procedures
Save (CTRL + S) – Saves the progress (including Excel
Workbook and Visual Basic Macro)
Cut (CTRL + X) – Standard cut functionality
Trang 12Copy (CTRL + C) – Standard copy functionality
Paste (CTRL + V) – Standard paste functionality
Find (CTRL + F) – Standard find functionality
Undo (CTRL + Z) – Reverse the last action
Redo – Restores the last action
Run (F5) – Runs the current part of the code If the cursor is inside a Sub or Function, it will run the Sub or Function and it stops only if there is a Breakpoint in the code If a User Form
is open in the Visual Basic Editor, then it will run the User Form
Break (CTRL + Break) – Pause the execution of the code and switch to break mode (in this way the code can be run line by line)
Reset / Stop – Stops the execution of the code, resets the
project and clear all variables/clipboard items/objects
Design Mode – Switch to design mode view (edit buttons / labels / text boxes, etc.)
Project Explorer (CTRL + R) – Displays the Project Explorer window on the screen
Properties Window (F4) – Displays the Properties window on the screen
Object Browser (F2) – Displays the Object browser on the screen
Toolbox – Displays toolbox when a User Form is active This toolbox is used to insert new items on the User Form (Text, Dropdowns, Images, Buttons, Tables, etc.)
Help (F1) – Displays the Office assistant window
Debug Toolbar Buttons:
Design Mode – Switch to design mode view (edit buttons / labels / text boxes, etc.)
Run/Continue(F5) – Runs the current part of the code If the cursor is inside a Sub or Function, it will run the Sub or
Function and it stops only if there is a Breakpoint in the code.
Trang 13If a User Form is open in the Visual Basic Editor, then it will run the User Form
Break (CTRL + Break) – Pause the execution of the code and switch to break mode (in this way the code can be run line by line)
Reset / Stop – Stops the execution of the code, resets the
project and clear all variables/clipboard items/objects
Toggle Breakpoint (F9) – Inserting a breakpoint in the selected line
Step Into (F8) – Runs the next line of code which is
executable and steps into procedures or functions (this is
helpful if some actions are done inside some procedures or functions because the code is run step by step)
Step Over (Shift + F8) – Runs the next line of code which is executable without stepping into procedures or functions
Step Out (CTRL + Shift + F8) – Runs the remaining lines of code from the current procedure and move back to the next line of code where the procedure is called
Locals Window – Displays the locals window where all the local variables are visible
Immediate Window (CTRL + G) – Displays the immediate window to allow the user to run code or query while the code
is in break mode Here are also showed the Debug.Print
messages / values.
Watch Window – Displays Watches window to see the
variables values while the code is in break mode To add a variable to watch window the user needs to right click on a variable and select “Add Watch…” option
Quick Watch (Shift + F9) – Displays the Quick Watch window while the code is in break mode
Call Stack – Displays the call stack dialog box while the code
is in break mode Here can be seen all the procedure that have been used in the project.
Userform Buttons:
Bring to front (CTRL + J) – Option that can be used after an object was selected Moves the selected object in front of all
Trang 14other objects.
Send to back (CTRL + K) – Option that can be used after an object was selected Moves the selected object behind all other objects.
Group – Option that can be used after some more objects were selected Create a group from the selected objects
Ungroup – Option that can be used after some more objects were selected Ungroup the selected grouped objects
Align – Option that can be used after some more objects were selected Align the selected objects left, center, right or to the grid
Center – Option that can be used after some more objects were selected Center the selected object horizontally or vertically Make Width Same Size – Option that can be used after some more objects were selected Set same width, height or both for the selected items
Zoom Percentage – Set zoom percentage of all controls from a user form (values between 10% and 400%)
Project Explorer Window
The Project Explorer Window can usually be found in the top left side
of VBA editor and it shows a hierarchical list of open projects This list contains Microsoft Excel Objects (Sheets and ThisWorkbook section), Forms (all User Forms created in the project), Modules (all macro modules) and Class Modules.
Trang 15time and some of them can be set only while running the code in break mode.
Immediate Window
The Immediate window is the window used for debugging or evaluate some expressions, execute some parts of the code without running the entire macro and print some variable values.
Watches Window
The watch window is the window where you can add some variables
to see the value while running the code When a variable is added to the watch window there are options to break/pause the code when the variable value is changed or becomes true.
Trang 16Objects in Visual Basic for Application
There are multiple types of objects in VBA:
Application Object
-it refers to entire Excel application and it sets properties for all
opened Excel files This type of objects improves the speed and
memory of the code It helps to set Application objects to
false/manual in the beginning of the code and set them back to
true/automatic at the end of the code Examples:
Application.WindowState = xlMaximized / xlMinimized – it maximizes/minimizes the Excel application
Application.DisplayAlerts = False / True – it disables / enables all Excel warning message / pop-ups
Application.ScreenUpdating = False / True – it disables / enables the Excel screen updating (mouse flickering)
Application.Calculation = xlCalculationManual / xlCalculationAutomatic – it auto calculates all cells formulas while the code is running
Workbooks Object
-it refers to all workbook objects that are opened in Microsoft Excel application With this type of objects, it can be created workbooks, save workbooks, close workbooks, rename workbooks, etc
Examples:
Trang 17Worksheets.Add – add a new worksheet to the specified
values/formats/borders/background color from/to a range of cells Examples:
Worksheet(1).Range(“A1:A5”).Select – it set focus to first 5 rows of column A from worksheet 1
Worksheet(3).Range(“B1:C3”).ClearContents – it clears cells values from worksheet 3 from cell B1 to cell C3
Worksheet(2).Range(“A1:A1048576”).Clear – it clears cells values/format/color from entire column A of worksheet 2
Worksheet(1).Range(“A1”).Value = 2 – it sets value of cell A1
to 2
Worksheet(1).Range(“A1:A2,B3:B4,C5:C6”).Value = 2 – it sets value of all three ranges to 2
Worksheet(1).Range(“B1:B2”).Value =
Worksheet(1).Range(“A1:A2”).Value – it copy values from range A1:A2 to B1:B2 inside the worksheet 1
Shape Object
-it refers to the collection of shapes from a worksheet With this type
of objects, it can be moved / delete / insert / copy all types of shape objects (tables, charts, pictures, etc.)
Excel VBA Object Model
-it refers to the hierarchy of all objects used in Microsoft Excel
Example:
Trang 18One”).Range(“A1”).Select – it selects cell A1 from workbook named Test worksheet named Sheet One
Trang 19Subs and Functions in Visual Basic for Application
In Excel Visual Basic it can be created a set of commands (lines of code) that needs to be executed one time or multiple times can be grouped in a sub or a function The difference between sub and
function is that sub doesn’t return something, and a function returns a value / result The functions defined in VBA can also runs as a
Worksheet function (define a function in the code and then call it inside a cell).
Examples:
Sub SetValueOfACell()
Worksheets(1).Cells(1,1).Value = 2;
End Sub Function SumOfValues()
SumOfValues = 2 + 3;
End Function Arguments
When subs or procedures needs to receive some data, the data can be passed to the sub or procedure using arguments Arguments needs to
be declared in the body of procedure.
Example:
Sub SetValueOfACell(val as Long)
Worksheets(1).Cells(1,1).Value = val;
End Sub Function SumOfValues(A as Long, B as Long)
SumOfValues = A + B;
End Function Arguments can be passed in two different ways: by value and by
reference
ByVal -the argument is passed by value which means that just the value is passed to the procedure If the value is modified
Trang 20inside the procedure the new value will be lost when exit the procedure.
ByRef
-the argument is passed by reference which means that th address of the argument is passed to the procedure If the value is modified inside the procedure the new value will
be saved after exit the procedure.
Calling a Sub or Function
Trang 22MultiplyOfNumbersParameters = A * B;
End Function
Sub Main() Var1 = SumOfNumbers Var2 = SumOfNumbersParameters (2,3) Var2_1 = SumOfNumbersParameters (5,10) Var3 = MultiplyOfNumbers
Var4 = MultiplyOfNumbersParameters(2,3) Var4_1 = MultiplyOfNumbersParameters(5,10) End Sub
User Defined Function (Custom Function) vs Worksheet Function
- Other type of functions are user defined functions that can be used in worksheet cells These types of functions are helpful when it’s needed a custom excel function in the worksheet.
Example 1:
A function that assign value of 5 to a specific cell:
1.Open Visual Basic Editor
2.Choose Insert -> Module
3.Insert the following code
Public Function Assign5Value()
Assign5Value = 5 End Function
4.Save the code
Trang 235.Return to Excel sheet and insert text “=Assign5Value()” on a cell.
Example 2:
A function that sum all the odd numbers from a range of cells:
1.Open Visual Basic Editor
2.Choose Insert -> Module
3.Insert the following code
Function SumOddNumbers(rng As Range)
Dim cell As Range
For Each cell In rng
If cell.Value Mod 2 = 1 Then SumOddNumbers = SumOddNumbers + cell.Value End If
Next cell
End Function
4.Save the code
5.Return to Excel sheet and populate a range of cells with values (check picture below)
6.Insert the following text on another cell “= SumOddNumbers ()” on a cell (check picture below)
Trang 26Macro Recorder tool in Visual Basic for Application
The Macro Recorder is a very useful functionality included in Excel Visual Basic for Application which can records a task that is done manually and convert it into a function in code This helps a lot when a task is done
manually and repetitive because it can automate manual tasks The task needs to be recorded and after that the code can be used to execute the task without doing it manually
Unfortunately there are some things/features that cannot be recorded by the record macro functionality (example : loop through a range of cells) but it helps because it creates a structure for the code which can be edited by the user
Useful information about recording a macro:
When a macro is recorded for performing a set of tasks with a range of cells, the macro will not extend if there are some extensions of the range (adding some more rows or columns)
If the task is big and it has a lot of steps is better to split it into smaller tasks to be easy to follow the macro
Macro recorder can be used in all Office programs (Excel, Outlook, PowerPoint, Word, etc.)
Steps that needs to be done to record a macro:
1 Go to Developer tab and click Record Macro button
2 In the Record Macro window can be added Macro name,
Shortcut key to run the code, place where to store the recorded macro and a description for the macro
3 Click OK and the recording will begin automatically
4 Perform all actions that needs to be recorded
Trang 28' Macro2 Macro
'
ActiveCell.FormulaR1C1 = "1" Range("A2").Select
ActiveCell.FormulaR1C1 = "2" Range("A3").Select
ActiveCell.FormulaR1C1 = "3" Range("A4").Select
ActiveCell.FormulaR1C1 = "4" Range("A5").Select
ActiveCell.FormulaR1C1 = "5" Range("A6").Select
ActiveCell.FormulaR1C1 = "6" Range("A7").Select
ActiveCell.FormulaR1C1 = "7" Range("A8").Select
ActiveCell.FormulaR1C1 = "8" Range("A9").Select
Trang 29End Sub
As a conclusion, Macro Recorder it helps when a user starts to learn VBA programming, but it also can be complicated sometimes to understand the macro recorded.
A good thing is that after the macro is recorded it can be customized
(change variable name, add more lines of code, extend the functionality of the recorded macro, etc).
Trang 30Important Language Elements in Visual Basic for Application
❖ Comments
Comments are the simplest type of VBA statement and they are used to give moreinformation about the code, to clarify some functions or simply split some functions.All the comments in the code are ignored by the code (not running the comments lineand statements)
A comment statement starts with apostrophe character ( ‘ ) Multiple lines can becommented using comment block function described in the beginning of the training
or by inserting apostrophe character in the beginning of each line that needs to becommented All the commented lines will be colored in green to be easily identifiedwhen scanning a long code
Comments are very useful to : describe a sub or a function, versioning a sub/
function/ module (keep track of changes in the code), describe variables or describeany workaround created in the code
convention is flexible so the variables can be named as descriptive as possible
As a good practice all the variables needs to be declared in VBA to don’t block tomuch memory if it’s not needed (variables can hold different amount of memorybased on the data type of the variable)
Syntax to declare a variable is:
Dim <<variable_name>> As <<data_type>>
Rules for variable_name:
➢ First character from the name must be a letter and the rest of the characters can
be letters, numbers and some punctuation characters
➢ Space character, punctuation character, “#”, “$”, “%”, “&”, and “!” cannot beused in the name
Trang 31➢ Visual Basic for Application doesn’t distinguish between uppercase and
lowercase
➢ Variable name cannot be longer than 254 characters
➢ Cannot use any reserved words (keywords) in variable name (ex Sub, With, For,And, Byte, Call, etc.)
Examples:
Dim a As Byte
Dim cond As Boolean
Dim name As String
Dim dateOfBirth As Date
Dim longitude As Double
Dim bigVariable As Variant
Procedure only By using Dim statement in the procedure that uses the
variableModule only By using Dim statement before the first Sub or
Function statement in the moduleAll procedures in all
Syntax to declare a constant is:
Dim <<constant_name>> As <<data_type>> = <<value_of_constant>>
Rules for constant_name:
➢ First character from the name must be a letter and the rest of the characters can
be letters, numbers and some punctuation characters
➢ Space character, punctuation character, “#”, “$”, “%”, “&”, and “!” cannot beused in the name
➢ Visual Basic for Application doesn’t distinguish between uppercase and
lowercase
➢ Constant name cannot be longer than 254 characters
➢ Cannot use any reserved words (keywords) in constant name (ex Sub, With, For,And, Byte, Call, etc.)
Trang 32Const pi_value As Double = 3.14
Const my_age as Byte = 30
Const country_Name as String = “Romania”
❖ Data Types
Data type is the characteristic of a variable that determines what kind of data can behold by the variable If the variables are not defined as the user wish (ex declare allvariables as Variant when the variable holds only small numbers inside), this willcause slower execution and inefficient memory usage In large applications this will
be a big issue
VBA built-in data types:
Boolean 2 bytes True or False
Byte 1 byte 0 to 255
Currency 8 bytes -922,337,203,685,477.5808 to
922,337,203,685,477.5807Date 8 bytes 1 January 100 to 31 December 9999Double
4.94065645841247E-324 to1.79769313486232E308 for positive valuesInteger 2 bytes -32,768 to 32,767
valuesString 10 bytes + string
length
0 to approximately 2 billion
Variant 16 bytes Any numeric value up to the range of a Double
In VBA there are built in functions to verify data types:
Trang 33Function Name Returning Value
IsArray (varname) Returns true if the variable is an array, otherwise returns
falseIsDate(expression) Returns true if the expression is a date or is recognized as
a valid date or time, otherwise returns falseIsEmpty(expression) Returns a Boolean value indicating if the variable has
been initialized or notIsError(expression) Returns a Boolean value indicating whether an expression
is an error valueIsMissing(argname) Returns a Boolean value indicating whether an optional
variant argument has been passed or not to a procedureIsNull(expression) Returns a Boolean value indicating whether an expression
contains no valid data (Null value)IsNumeric(expression) Returns a Boolean value indicating whether an expression
can be evaluated as a numberIsObject(identifier) Returns a Boolean indicating whether an identifier
represents an object variable
There are also functions to convert from one data type to another (if it’s possible):
CBool(expression) Returns Boolean of a valid string or numeric expression
argumentCByte(expression) Returns Byte value after conversion of the expression
CCur(expression) Returns Currency value after conversion of the
expressionCDate(expression) Returns Date value after conversion of the expression
CDbl(expression) Returns Double value after conversion of the expression
CDec(expression) Returns Decimal value after conversion of the
expressionCInt(expression) Returns Integer value after conversion of the expression
CLng(expression) Returns Long value after conversion of the expression
CSng(expression) Returns Single value after conversion of the expression
CStr(expression) Returns String value after conversion of the expression
CVar(expression) Returns Variant value after conversion of the expression
To force yourself to declare all the variables used in the code include the followingstatement in the beginning of the code:
Trang 34Option Explicit – this won’t allow the user to run the code if it contains any
An assignment statement is a VBA statement that assigns the result of an expression
or evaluation of the expression to a variable or an object
An assignment can be used to perform a calculation, manipulate characters,
manipulate set of data, evaluate some expressions or test some data
Assignment statements always contains an equal sign ( = )
Examples:
name = “John” -> assign “john” to the name variable
inputName = InputBox(“What is your name?”) -> show a pop-up/input box andask the user to insert a name and assign that name to the variable
age = 25 -> assign 25 value to the age variable
age = InputBox(“What is your age?”) -> show a pop-up/input box and ask the user
to insert age and assign that number to the variable
❖ Visual Basic Statements
A VB statement is a complete set of instructions
If a statement it needs to be separated to multiple line it can be done by insert “_”character at the end of each line that will be continued to the next line :
Dim longText as String
longText = “qwerty123456qwerty123456” _
& “asdfgh1234567asdfgh1234567” _
& “zxcvb1234zxcvb1234zxcvb”
Trang 35❖ VBA Operators
Operators are used to create connections / operations between variables or
expressions
There are multiple types of VBA operators : arithmetic, comparison, logical
(relational) and concatenation
Arithmetic Operators : mathematical operators used to execute mathematical
operation
+ Adds two or more operands 2 + 5 (returns 7)
- Subtracts two or more operands 5 – 2 (returns 3)
* Multiplies two or more operands 2 * 5 (returns 10)
/ Divides the numerator by the denominator 5 / 2 (returns 2.5)
% Modulus operator and the reminder after an integer
division 5 % 2 (returns 1)
^ Exponentiation operator 5 ^ 2 (returns 25)
Comparison Operators : mathematical operators used to compare values of twooperands (variables)
=
Checks if two operands are equal or not Ifyes then returns true, otherwise returnsfalse
(10 = 10) returns true(“Name1” = “Name2”)returns false
<>
Checks if two operands are equal or not Ifnot equal returns true, otherwise returnsfalse
(10 <> 10) returns false(“Name1” <> “Name2”)returns true
>=
Checks if the value of the left operand isgreater or equal than the value of the rightoperand If yes, then returns true, otherwisereturns false
(10 >= 10) returns true(“Name2” >= “Name1”)returns true
<
Checks if the value of the left operand isless than the value of the right operand Ifyes, then returns true, otherwise returnsfalse
(10 < 10) returns false(“Name2” < “Name1”)returns false
Trang 36<= Checks if the value of the left operand is
less or equal than the value of the rightoperand If yes, then returns true, otherwisereturns false
(10 <= 10) returns true(“Name2” <= “Name1”)returns false
Logical Operators : operators used to check some conditions or combine multiplestatements (variables)
AND Checks if two or more conditions are True
then returns True, otherwise returns False
10 = 10 AND 10 = 5returns false
“Name1” = “Name1”AND “Name1” <>
“Name2” returns true
OR
Checks if one of the two or more conditions
is True If any of the conditions is True thenreturns True, otherwise returns False
10 = 10 OR 10 = 5returns true
“Name1” = “Name1” OR
“Name1” <> “Name2”returns true
(10 = 10 XOR 10 = 5)returns true
(“Name1” = “Name1”XOR “Name1” <>
“Name2”) returns false
Concatenation Operators : operators used to concatenate some operands (variables)
+ Concatenates two Values (numeric
concatenation)
10 + 10 returns 20
“Name1” + “Name2”returns Name1Name2
& Concatenates two Values (stringconcatenation)
10 & 10 returns 1010
“Name1” & “Name2”returns Name1Name2
Trang 37Array that contains a list of names:
Dim nameList(5) as String
Array that contains month numbers:
Dim monthNumber(13) as Byte
Array that contains business card:
Dim businessCard(5) As Variant
businessCard(0) = “John Smith”
Trang 38Dim numberMatrix (1 To 3, 1 To 3) As Byte
ReDim Preserve <<arrayName>> <<arrayLength>>
Preserve word allow the user to re-dimension an array but keep the existing values of thearray
Trang 39Lowest and highest index numbers (margins) of an array are known as bounds Arraysize can be identified using LBound and UBound LBound returns the smallest subscript
in a given dimension of an array (long type) UBound returns the largest subscript in agiven dimension of an array (long type)
❖ Keywords / Reserved words
Reserved words are some predefined words that cannot be used to define variables,subroutines or functions These words are part of Visual Basic for Application
programming language itself
Fortunately VBA checks the code while the user type in and give warnings/errors(usually Expected Identifier warning) if a keyword it is not used correctly
Examples of reserved words :
Trang 40Range objects in Visual Basic for Application
Ranges represents a cell, a row, a column or a selection of cells of Excel worksheet.Ranges can be used to copy values, formats, colors or formulas from one range toanother Defining a range object needs to follow object hierarchy pattern using “.”operator as a separator between hierarchy items
2.Workbooks(“file.xlsm”).Worksheets(1).Range(“A1:A10”).Formula = _ Rand()-fills first 10 cells from first worksheet form file.xlsm workbook with somerandom numbers
3.ThisWorkbook.Worksheets(1).Range(“A1:A3”).Borders.LineStyle = _ xlThick-sets the border line style for the specified range of cells