Sunteți pe pagina 1din 16

Program: To copy the contents of one cell in another using excel function

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 Create button and link it to macro


Sub HNY()
ActiveCell = "Happy new year 2018"
Range("A1").Select
End Sub

Program: To perform a mathematical operation


Sub mathExample()
x = 67
y = 43
result = "Your result is" & x + y
MsgBox result
ActiveCell = result
End Sub
Program: To calculate the average of the numbers, autofill function
Sub Macro1Formulas()
ActiveCell.FormulaR1C1 = "=SUM(R[-3]C:R[-1]C)"
Range("B5").Select
Selection.AutoFill Destination:=Range("B5:F5"), Type:=xlFillDefault
Range("B5:F5").Select
Range("B8").Select
ActiveCell.FormulaR1C1 = _
"=""Average is "" & TEXT(AVERAGE(R[-6]C:R[-4]C[4]),""0.00"")"
Range("B9").Select
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 change the name of the Worksheet using VBA


Sub Macro2()
ActiveCell.Select
ActiveCell.FormulaR1C1 = "Sunday"
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "Monday"
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "Tuesday"
ActiveCell.Offset(1, -2).Range("A1").Select
End Sub
Program: To perform mathematical operations using variables
Sub mathExample()
Dim y As Double
Dim result As String
x=x+5
y = 43
result = "Your answer is " & x + y
MsgBox result
End Sub
Program: Error Handler if a file is not found in the specified path
Sub test()
On Error GoTo Jammu
Workbooks.Open "E:\divya\IIMJ\Term IV\Spreadsheet modelling\7.doWhile_Result.xlsm"
Exit Sub
Jammu: MsgBox ("Wrong Path")
End Sub
Program: To test the usage of keyword Global
Sub testVar()
MsgBox x
Sub test()
MsgBox x
End Sub
End Sub
Sub GlobalTest()
Dim x As Integer
x = 95
MsgBox x
Sub test2()
MsgBox x
End Sub
End Sub
Program: To display date and time
Sub dateTest()
Dim myDate As Date
Dim myTime As Date
myDate = #8/13/2017#
myTime = #7:13:00 PM#
MsgBox myTime
MsgBox myDate
End Sub

Program: If then loop


Sub hello()
If Hour(Time) < 12 Then MsgBox "Goodmorning"
Else: MsgBox "Good afternoon"
End If
End Sub
Program: Use For Loop to print numbers
Sub testFill()
Dim x As Integer
For x = 5 To 1 Step -1
ActiveCell.Offset(x, 0) = x
Next
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

Program: For and Range


Sub reValue()
Dim cell As Range
For Each cell In Range("A3:D6")
cell.Value = cell.Value * 10
Next
End Sub
Program: To color the rows which are divisible by 2
Sub Color()
Dim Data As Worksheet
Set Data = Worksheets("Data")
Dim Row As Integer
Dim Column As Integer
For Row = 2 To 41
If (Row Mod 2) > 0 Then
For Column = 2 To 13
Cells(Row - 1, Column).Interior.ColorIndex = 15
Next Column
End If
Next Row
End Sub

Program: Do while Loop


Sub copyDown()
Dim myValue As Double
myValue = ActiveCell.Offset(1, -1).Value
Do While myValue <> Empty
ActiveCell.Offset(1, 0).Value = myValue * 2
ActiveCell.Offset(1, 0).Select
myValue = ActiveCell.Offset(1, -1).Value
Loop
End Sub
Program: For Loop
Sub forNextMailing()
Dim counter As Integer
Range("B1").Select
For counter = 1 To 50
Selection.Font.Bold = True
Selection.Font.Underline = xlUnderlineStyleSingle
ActiveCell.Offset(6, 0).Range("A1").Select
Next counter
End Sub

Program: Illustrate Array


Sub monthUserInput()
Dim i As Integer
Dim monthNames(12) As String

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

Program: Sort in Descending Order


Sub abc()
Range("a1:a3").Sort Range("a1:a3"), xlDescending
End Sub
Program: To display the hierarchy in excel
Sub test()
Application.Workbooks("Book1").Worksheets("Sheet2").Range("d1").Select
ActiveCell = 45
End Sub

Program: Error Handler (Division by 0)

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: To select multiple sheets in this workbook


Sub abc()
Dim i As Integer
For i = 1 To 3
ThisWorkbook.Sheets(i).Select False
Next i
End Sub

Program: To select last cell that contains some data


Sub abc()
ActiveCell.SpecialCells(xlLastCell).Select
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: To count Bishal anywhere in the entire excel sheet


Sub test()
Sheets("Sheet2").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(Sheet1!R:R[1048575],""Bishal"")"
Range("A2").Select
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

number = InputBox("Enter any positive integer")


If (i Mod 2) > 0 Then
Sum = Sum + i With Sheet1.Range("A1")
End If
Next i For i = 1 To number
MsgBox "Sum of positive odd integers till " & number & " is " & Sum
If (i Mod 2) > 0 Then
End Sub
Sum = Sum + i

.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 With .Offset(((i + 1) / 2) - 1, 0) = Sum


MsgBox "Sum ofEnd Withof
positive positive
odd odd
integers tillintegers till " &
" & number & number & " is " & Sum
" is " & Sum

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

Program: Use of Timer


Sub countlarge()
Dim cell As Range, nlarge As Long
Dim secs1 As Single
Dim secs2 As Single
secs1 = Timer()
For Each cell In Range("Sales")
If cell.Value > 100 Then nlarge = nlarge + 1
Next
secs2 = Timer()
MsgBox nlarge & " cells in the sales range have a quantity larger than 100"
MsgBox " total time taken is" & vbNewLine & secs2 - secs1 & " seconds "
End Sub
A few commands:
Cells(7,2)= "A simple message"
Intellisense: Control + Space
Declaring a constant: Const x = 20
View Object Browser or F2
Use END IF only when IF is used, ELSEIF is a single thing and no need for END for it
activecell.Offset(2,2).resize(2,2).Select
activecell.EntireColumn.Select
activecell.Entirerow.Select
Worksheets("Sheet1").Activate
Range("A3").Select
?Cells.Find(WHat:="Profit").Address ($A$3)
Cells.Find(What:="Profit").Activate
?Cells.Find(What:="Profit").Row

S-ar putea să vă placă și