Final Tally (1) - Merged - Pagenumber
Final Tally (1) - Merged - Pagenumber
LAB
INDEX
Chapter-1 Introduction to
Accounting Packages
Introduction and
Chapter-2 Advanced functions of
M.S Excel
VBA Conditions,
Chapter-4 Functions, and
Exceptions: Conditions
& Loops
Advance VBA
Chapter-5
1
TALLY
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
VBA
Q1. Introduction to VBA in Excel
VBA (Visual Basic for Applications) is a programming language built into Microsoft Office applications,
including Excel. It allows users to automate repetitive tasks, enhance the functionality of Excel, and
create custom applications.
1. Open Excel.
2. Go to File > Options > Customize Ribbon.
3. Check the Developer option on the right pane and click OK.
1. Project Explorer: Displays all open Excel workbooks and their objects.
2. Code Window: Where you write your VBA code.
3. Immediate Window: Used to debug and test lines of code.
4. Properties Window: Displays properties of selected objects.
Basics of VBA
Recording Macros
33
Writing Your First VBA Code
Variables
Sub HelloWorld()
End Sub
String Text
i. if Loop
Else
34
MsgBox "Number is 5 or less"
End If
For i = 1 To 10
Next i
count = 1
count = count + 1
Wend
Sub SumRangeOfCells()
sumResult = Application.WorksheetFunction.Sum(rng)
35
' Display the sum result in a message box
End Sub
Dim rngAs Range: Declares a variable rng to store the range of cells that will be summed.
Dim sumResultAs Double: Declares a variable sumResult to store the result of the summation.
Set rng = Range("A1:A5"): Defines the range of cells (from A1 to A5) that will be summed. You
MsgBox "The sum of the range is: " &sumResult: Displays the sum in a message box.
Output:
36
Q5. Write A VBA Convert Text to Uppercase
The aim of the "Convert Text to Uppercase" program is to simplify the process of transforming text in
selected cells of an Excel worksheet into uppercase format. This program ensures consistency in text
formatting, reduces manual effort, and enhances productivity when working with large datasets.
Program:
Sub ConvertToUpperCase()
Dim cell As Range
' Loop through each selected cell
For Each cell In Selection
' Check if the cell contains text
If Not IsEmpty(cell) And VarType("i am mba student") = vbString Then
cell.Value = UCase("i am mba student")
' Convert text to uppercase
End If
Next cell
Output
37
Q6. Insert Different Color Index in VBA
Aim: The aim of the program "Insert Different Color Index in VBA" is to dynamically assign various
colors to cells, shapes, or objects in Microsoft Excel using VBA (Visual Basic for Applications). This
program enables users to:
1. Automate Formatting: Apply a diverse range of color indices to enhance the visual appeal and clarity
of Excel sheets.
2. Customization: Allow users to easily select and apply specific color indices to meet their design or
functional requirements.
3. Efficiency: Save time compared to manual color application, especially for large datasets or repetitive
formatting tasks.
4. Demonstrate VBA Functionality: Serve as a learning tool for understanding how to utilize Excel's color
index property programmatically.
Program:
Sub Insert_Different_Colours()
Dim i As Integer
For i = 1 To 56
Cells(i, 1).Value = i
Cells(i, 2).Interior.ColorIndex = i
Next
End Sub
Output:
38
Explanation:
• Variable Declarations:
• Worksheet Assignment:
• The line Set ws = ThisWorkbook.Sheets(1) assigns the first sheet in the workbook as the target.
You can change Sheets(1) to the name of your desired worksheet (e.g., Sheets("Sheet1")).
• For Loop:
• The loop For i = 1 To 56 iterates through the range of color indices available in Excel (1 to 56).
• Each iteration applies the current ColorIndex to a cell in column A, starting from row 1.
• Adding Text:
• cell.Value = "Color Index " & i inserts text into the cell to label it with the corresponding color
index for easy identification.
• Applying Color:
• cell.Interior.ColorIndex = i sets the background color of the cell to the current color index.
• Notification:
• After the loop completes, the MsgBox function displays a message to confirm the program's execution.
The aim of the program "Insert Serial Number From Top" is to automatically generate and insert
sequential numbers into a specified range of cells in an Excel worksheet, starting from the top. This
program is designed to:
1. Automate Serial Numbering: Simplify the process of numbering rows or entries in a dataset.
2. Improve Efficiency: Save time compared to manually entering serial numbers, especially for large
datasets.
3. Ensure Accuracy: Avoid mistakes in numbering by programmatically generating consecutive numbers.
39
4. Enhance Workflow: Provide a customizable and reusable VBA solution for structured and organized
data management.
Program Code:
Sub Insert_Numbers_From_Top()
Dim i As Integer
For i = 1 To 100
Cells(i, 1).Value = i
Next i
End Sub
Output
40