0% found this document useful (0 votes)
49 views9 pages

IT Codes For Class 10

The document contains a series of VBA macros for Excel that perform various tasks such as calculating averages, finding the largest and smallest numbers, counting specific values, copying and deleting ranges, creating new worksheets, formatting ranges, printing worksheets, and calculating the sum of two numbers. Each macro prompts the user for input and provides feedback through message boxes. The document is intended for a Class 10 IT subject and includes code snippets for each function.

Uploaded by

anushkazambre26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views9 pages

IT Codes For Class 10

The document contains a series of VBA macros for Excel that perform various tasks such as calculating averages, finding the largest and smallest numbers, counting specific values, copying and deleting ranges, creating new worksheets, formatting ranges, printing worksheets, and calculating the sum of two numbers. Each macro prompts the user for input and provides feedback through message boxes. The document is intended for a Class 10 IT subject and includes code snippets for each function.

Uploaded by

anushkazambre26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SUBJECT – IT

CLASS – 10
1. CALCULATE RANGE AVERAGE
Sub CalculateRangeAverage()
Dim rng As Range
Dim total As Double
Dim count As Integer
Dim avg As Double

' Prompt user to select a range


On Error Resume Next
Set rng = Application.InputBox("Select a range of numbers:", Type:=8)
On Error GoTo 0

' Check if a valid range was selected


If rng Is Nothing Then
MsgBox "No range selected. Please try again!", vbExclamation,
"Error"
Exit Sub
End If

' Calculate the sum and count


total = Application.WorksheetFunction.Sum(rng)
count = Application.WorksheetFunction.count(rng)

' Calculate the average


If count > 0 Then
avg = total / count
Else
avg = 0
End If

' Display the result


MsgBox "The average of the selected numbers is: " & avg,
vbInformation, "Average Calculation"
End Sub
2. FIND THE LARGEST NUMBER
Sub FindLargestNumber()
Dim rng As Range
Dim maxNum As Double

' Prompt user to select a range


On Error Resume Next
Set rng = Application.InputBox("Select a range of numbers:", Type:=8)
On Error GoTo 0

' Check if a valid range was selected


If rng Is Nothing Then
MsgBox "No range selected. Please try again!", vbExclamation,
"Error"
Exit Sub
End If

' Find the maximum number in the range


maxNum = Application.WorksheetFunction.Max(rng)

' Display the result


MsgBox "The largest number in the selected range is: " & maxNum,
vbInformation, "Maximum Number"
End Sub
3. FIND THE SMALLEST NUMBER
Sub WriteSmallestNumber()

' Prompt user to select a range


On Error Resume Next
Set rng = Application.InputBox("Select a range of numbers:", Type:=8)
On Error GoTo 0

' Check if a valid range was selected


If rng Is Nothing Then
MsgBox "No range selected. Please try again!", vbExclamation,
"Error"
Exit Sub
End If

' Find the minimum number in the range


minNum = Application.WorksheetFunction.Min(rng)

' Prompt user to select a destination cell for output


On Error Resume Next
Set destCell = Application.InputBox("Select a cell to write the smallest
number:", Type:=8)
On Error GoTo 0

' Check if a valid cell was selected


If destCell Is Nothing Then
MsgBox "No destination cell selected. Please try again!",
vbExclamation, "Error"
Exit Sub
End If

' Write the smallest number to the selected cell


destCell.Value = minNum
' Notify the user
MsgBox "The smallest number in the selected range has been written
to " & destCell.Address, vbInformation, "Smallest Number Written"
End Sub
4. COUNT SPECIFIC VALUE
Sub CountSpecificValue()
Dim rng As Range
Dim cell As Range
Dim targetValue As Variant
Dim count As Integer

' Prompt user to select a range


On Error Resume Next
Set rng = Application.InputBox("Select a range of cells:", Type:=8)
On Error GoTo 0

' Check if a valid range was selected


If rng Is Nothing Then
MsgBox "No range selected. Please try again!", vbExclamation,
"Error"
Exit Sub
End If

' Prompt user to enter the specific value to count


targetValue = InputBox("Enter the value to count in the selected
range:")

' Check if a valid input was provided


If targetValue = "" Then
MsgBox "No value entered. Please try again!", vbExclamation,
"Error"
Exit Sub
End If

' Count occurrences of the specific value


count = 0
For Each cell In rng
If cell.Value = targetValue Then
count = count + 1
End If
Next cell
' Display the result
MsgBox "The number of cells containing '" & targetValue & "' is: " &
count, vbInformation, "Count Result"
End Sub

5. COPY RANGE
Sub CopyRange()
Dim sourceRange As Range
Dim destinationRange As Range

' Prompt user to select the source range


On Error Resume Next
Set sourceRange = Application.InputBox("Select the range to copy:",
Type:=8)
On Error GoTo 0

' Check if a valid range was selected


If sourceRange Is Nothing Then
MsgBox "No source range selected. Please try again!",
vbExclamation, "Error"
Exit Sub
End If

' Prompt user to select the destination range


On Error Resume Next
Set destinationRange = Application.InputBox("Select the destination
cell:", Type:=8)
On Error GoTo 0

' Check if a valid destination was selected


If destinationRange Is Nothing Then
MsgBox "No destination cell selected. Please try again!",
vbExclamation, "Error"
Exit Sub
End If
' Copy the range to the destination
sourceRange.copy
destinationRange.PasteSpecial Paste:=xlPasteValues ' Paste only
values, modify as needed

' Clear clipboard to avoid issues


Application.CutCopyMode = False

' Notify the user


MsgBox "Range copied successfully to " & destinationRange.Address,
vbInformation, "Copy Range"
End Sub

6. DELETE RANGE
Sub DeleteRange()
Dim rng As Range

' Prompt user to select the range to delete


On Error Resume Next
Set rng = Application.InputBox("Select the range to delete:", Type:=8)
On Error GoTo 0

' Check if a valid range was selected


If rng Is Nothing Then
MsgBox "No range selected. Please try again!", vbExclamation,
"Error"
Exit Sub
End If

' Clear the content and formatting of the selected range


rng.Clear

' Notify the user


MsgBox "Selected range has been deleted!", vbInformation, "Delete
Range"
End Sub
7. CREATE NEW WORKSHEET
Sub CreateNewWorksheet()
Dim ws As Worksheet
Dim sheetName As String

' Prompt user to enter a name for the new worksheet


sheetName = InputBox("Enter a name for the new worksheet:")

' Check if a name was provided


If sheetName = "" Then
MsgBox "No name entered. Please try again!", vbExclamation,
"Error"
Exit Sub
End If

' Create the new worksheet


On Error Resume Next
Set ws = ThisWorkbook.Sheets.Add
ws.Name = sheetName
On Error GoTo 0

' Check if the worksheet was created successfully


If ws Is Nothing Then
MsgBox "Error creating worksheet. The name may already exist!",
vbExclamation, "Error"
Else
MsgBox "New worksheet '" & sheetName & "' has been created!",
vbInformation, "Success"
End If
End Sub
8. FORMAT RANGE
Sub FormatRange()
Dim rng As Range

' Prompt user to select the range to format


On Error Resume Next
Set rng = Application.InputBox("Select the range to format:", Type:=8)
On Error GoTo 0

' Check if a valid range was selected


If rng Is Nothing Then
MsgBox "No range selected. Please try again!", vbExclamation,
"Error"
Exit Sub
End If

' Apply formatting


With rng
.Font.Bold = True ' Make text bold
.Interior.Color = RGB(200, 200, 255) ' Set background color (light
blue)
.Borders.LineStyle = xlContinuous ' Apply borders
.Borders.Weight = xlThin ' Set border thickness
.HorizontalAlignment = xlCenter ' Align text to center
End With

' Notify the user


MsgBox "The selected range has been formatted!", vbInformation,
"Formatting Applied"
End Sub
9. PRINT WORKSHEET
Sub pri()
ActiveSheet.PrintOut From:=1, To:=2
End Sub
10.SUM OF TWO NUMBERS
Sub CalculateSum()
Dim num1 As Double
Dim num2 As Double
Dim result As Double

' Assign values to numbers


num1 = InputBox("Enter the first number:")
num2 = InputBox("Enter the second number:")

' Calculate sum


result = num1 + num2

' Display result


MsgBox "The sum of " & num1 & " and " & num2 & " is " & result,
vbInformation, "Sum Result"
End Sub

NAME – ANUSHKA SAGAR ZAMBRE


ROLL NO – 5414 / TG

You might also like