Classroom Codes For Vba Excel
Classroom Codes For Vba Excel
Sub abc()
ThisWorkbook.Sheets("Sheet1").Range("B5") =
Application.WorksheetFunction.Proper(ThisWorkbook.Sheets("Sheet1").Range("A1"))
End Sub
Program: To copy the contents of one cell in another using excel function
Sub test()
Sheets("Sheet1").Range("B5") = Application.WorksheetFunction.Proper(Sheets("Sheet1").Range("A1"))
End Sub
Program: To copy the contents of one cell in another using excel function
Sub test1()
Range("B5") = WorksheetFunction.Proper(Range("A1"))
OR
Range("B5") = Application.Proper(Range("A1"))
End Sub
Program: To take input and display output with the title for the dialog box
Sub ac()
Dim x As Integer
Dim y As Integer
x = InputBox("Enter the value of x", "Selling Price")
y = InputBox("Enter the value of x")
MsgBox "The sum of x and y is" & (x + y), vbInformation, "Output"
End Sub
Program: To change font size, color etc
Sub test()
Dim abc As Range
Set abc = ActiveWorkbook.Worksheets("Data").Range("Scores")
abc.Font.Size = 36
abc.Font.Color= vbBlue
End Sub
Program: To change the name of the Worksheet using VBA
Sub test()
Worksheets("Sheet2").Name = "Divya"
End Sub
Program: To delete the first row of every sheet in the current Workbook
Sub removeFirstRow()
Dim wksheet As Worksheet
For Each wksheet In ActiveWorkbook.Worksheets
wksheet.Rows(1).Delete
Next
End Sub
For i = 1 To 3
monthNames(i) = InputBox("Please enter the months")
MsgBox ("Thanks for entering the" & i & "values")
Next
For i = 1 To 3
MsgBox (i & "month entered was" & monthNames(i))
Next
End Sub
Program: To display numbers based on user input
Sub Q12()
Dim x As Integer
Dim y As Integer
Dim z As Integer
Dim i As Integer
x = InputBox("Enter the starting integer", "Starting value")
y = InputBox("Enter the step value", "Step Value")
z = InputBox("Enter the number of integers", "Number of integers")
MsgBox ("The user has asked for " & z & " integers to be generated with the step value of " & y)
Range("a1").Select
ActiveCell = "Number"
ActiveCell.Offset(1, 0) = x
For i = 1 To z - 1
ActiveCell.Offset(1, 0).Select
x=x+y
ActiveCell.Offset(1, 0) = x
Next
Range("b1").Select
ActiveCell.Bold = "Name"
For i = 1 To z
ActiveCell.Offset(1, 0).Select
ActiveCell = ("FM" & i)
Next
End Sub
Program: Editing in the msgbox
Sub abc()
a = MsgBox("Divya" & vbCrLf & "Chitte", vbMsgBoxRight, "Title")
End Sub
Program: Use of Arrays
Sub aray ()
Dim no As Integer
a = InputBox("Please enter the number of integers to be printed")
Range("a1").Select
For i = 1 To a
ActiveCell = WorksheetFunction.RandBetween(1, 5)
ActiveCell.Offset(1, 0).Select
Next
ReDim abc(1 To a) As Integer
Range("A1").Select
For i = 1 To a
abc(i) = ActiveCell.Value
ActiveCell.Offset(1, 0).Select
Next i
For i = 1 To a
ActiveCell = abc(i)
ActiveCell.Offset(1, 0).Select
Next i
End Sub
Program: Sort in Ascending Order
Dim xyz As Range
Set xyz = Range(Range("B1"), Range("B1").Offset(a, 0))
Range("xyz").Sort Range("xyz"), xlAscending
End Sub
Sub test()
On Error GoTo abc
Application.DisplayAlerts = False
Dim a As Integer
Dim b As Integer
Dim result As String
a = 30 / b
MsgBox a
Exit Sub
abc:
result = MsgBox("This action is not possible because of error number " & Err.Number & " which means "
& Err.Description)
End Sub
Program: To test Debug.Print
Sub test()
Debug.Print "My Name Is Divya"
End Sub
Program: Call function
Sub test()
Call abc
End Sub
Sub abc()
MsgBox ("Pista")
End Sub
Program: Going to a specific cell and making it the top left corner
Sub abc()
Application.Goto reference:=Range("H10"), scroll:=True
(OR) Application.Goto Range(H10), True
End Sub
Program: Finding the cell address
Sub abc()
Dim myAddress As String
myAddress = Cells.Find(What:="Bishal").Address
MsgBox myAddress
End Sub
Program: Error Handler when only one sheet is remaining in the workbook which cannot be
deleted
Sub test()
On Error GoTo ErrHandler
Application.DisplayAlerts = False
ThisWorkbook.Sheets("Sheet3").Delete
Exit Sub
ErrHandler:
MsgBox (Err.Number & " " & Err.Description)
End Sub
Result: 9 subscript out of range when sheet does not exist
1004 when the sheet is the only one in the workbook
Program: To sum odd integers Sub SumOddIntegers()
Sub SumOddIntegers()
Dim number As Integer
Dim number As Integer
Dim i As Integer
Dim i As Integer
Dim Sum As Integer
Dim Sum As Integer
Dim Sheet1 As Worksheet
number = 0
Set Sheet1 = Worksheets("Exercise2")
i=0
number = 0
Sum = 0
i=0
number = InputBox("Enter any positive integer")
For i = 0 To number Sum = 0
.Offset(((i + 1) / 2) - 1, 0) = i
End If
Next i
.Offset(((i + 1) / 2) - 1, 0) = Sum
End With
MsgBox "Sum of positive odd integers till " & number & " is " & Sum
End Sub
Program: To print odd integers and print
Sub the sum
SumOddIntegers()
Sub SumOddIntegers()
Dim number As Integer
Dim number As Integer
Dim i As Integer
Dim i As Integer
Dim Sum As Integer
Dim Sum As Integer
Dim Sheet1 As Worksheet
Dim Sheet1 As Worksheet
Set Sheet1 = Worksheets("Exercise2")
Set Sheet1 = Worksheets("Exercise2")
number = 0 number = 0
i=0 i=0
Sum = 0 Sum = 0
number = InputBox("Enter any positive
number integer") any positive integer")
= InputBox("Enter
With Sheet1.Range("A1") With Sheet1.Range("A1")
For i = 1 To number
For i = 1 To number
If (i Mod 2) > 0 Then
If (i Mod 2) > 0 Then
Sum = Sum + i
Sum = Sum + i
.Offset(((i + 1) / 2) - 1, 0) = i
.Offset(((i + 1) / 2) - 1, 0) = i
End If
End If
Next i
.Offset(((i + 1) / 2) - 1, 0) = Sum Next i
End Sub
End Sub
Program: To find a number in a range
Sub FindNum()
Dim search As Integer
Dim k As Integer
Dim m As Integer
search = 13
For k = 1 To 20
m = Range("e7").Offset(k, 0).Value
If m = search Then
Range("e7").Offset(k, 0).Activate
MsgBox "Found it!"
Exit For
End If
Next k
End Sub