0% found this document useful (0 votes)
13 views4 pages

COMPROG MANUAL Topic5

The document outlines important tools and logic used in Excel macros including determining the last row and column of data, using if/then and elseif statements to compare cell values, and functions like offset and select case as alternatives to if/then statements. It also provides examples of gotos, message boxes, and user defined functions with optional parameters. The document concludes with an exercise to create a macro that asks the user a question and conditionally updates the sales amount based on their response.

Uploaded by

Shaine Inovero
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
13 views4 pages

COMPROG MANUAL Topic5

The document outlines important tools and logic used in Excel macros including determining the last row and column of data, using if/then and elseif statements to compare cell values, and functions like offset and select case as alternatives to if/then statements. It also provides examples of gotos, message boxes, and user defined functions with optional parameters. The document concludes with an exercise to create a macro that asks the user a question and conditionally updates the sales amount based on their response.

Uploaded by

Shaine Inovero
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

Page |8

TOPIC 5
Important Tools and Excel Logic

a. Determining the last rows in your data set


Ex. Last row in Column 1(row no.)
LastRow = cells(rows.count,1).end(xlup).row

b. Determining the last column in your data set


Ex. Last column in row 1
LastColumn = cells(1,columns.count).end(xltoleft).column

c. Determining the next row in your data set


Ex. Next row after the last row in Column 1(row no.)
NextRow = cells(rows.count,1).end(xlup).row+1

d. Absolute vs Relative references in Macro Recording

e. Using With and End With


With Range(“a1”)
.Font.Bold = true
.Font.Underline = true
.Font.Italic = true
End With

f. If then statement
Sub MyIfstatements()
If Range (“c6”) = 12 Then
MsgBox “Good!”
End If
End Sub
g. If not equal to (<>) statement
Sub MyIfstatements()
If Range (“c6”) <> 12 Then
MsgBox “Good!”
End If
End Sub

h. If , Then, ElseIF Statement


Sub MyIfstatements()
If Range (“c6”) = 12 Then
MsgBox “Good!”
Else
MsgBox “Bad”
Page |9

End If
End Sub

Another Example:
Sub MyIfstatements()
If Range (“c6”) = 12 Then
MsgBox “Twelve!”
Elseif Range(“c6”) = 5 then
MsgBox “Five”
Else
MsgBox “Please enter either a Five or a Twelve”
End If
End Sub

i. Comparative Operators with Text and Numbers


Note: VBA tend consider text somehow greater than any numbers so you'll have to
compensate

Sub MyIfstatements()
If Range("c6") > 3 And IsNumeric(Range("c6")) Then
MsgBox "Good!"
End If
End Sub

j. Select Case as alternative to IF THEN Statements


Sub myCase()
Select Case Range(“c6”)
Case 12
MsgBox “Twelve”
Case is <2
MsgBox “less than 2”
Case Else
MsgBox “else”
End Select

k. GoTo and Labels (Skip “underconstruction” codes)

Goto myEnding
.
.
myEnding:

l. Message box with Yes and No Options


Sub myMessage
P a g e | 10

Answer = MsgBox (“Do you like excel vba?”, vbyesNo)


If answer = vbYes Then
MsgBox “I like it too!”
Elseif answer = vbNo Then
Msgbox “I think you should like it, it’s cool!”
End If
End Sub

m. Relative positioning using Offset


Sub MyOffset
Selection.Offset(3,1) = Selection
Or
Cells(1,1).Offset(3,1)=”Mabuhay”
End Sub

n. User Defined function


Function MoI(b, d)
MoI = (b * (d ^ 3)) / 12
End Function

With optional parameter


Function MoI(b, d, Optional dplaces)
If IsMissing(dplaces) then
MoI = (b * (d ^ 3)) / 12
Else
MoI =Round(( (b * (d ^ 3)) / 12),dplaces)
End If
End Function
P a g e | 11

EXERCISE 4

NOTE: To get the row number of the Selected cell, use Selection.Row
When user clicks on a cell and clicks blue button, the macro will use items on that row in
various columns.
Create a Macro that does the following:
1. Asks user "Add 100 to current row sales?"
2. If Yes, add 100 to Sale Amount (current row, column 4)
3. If No, do nothing
4. Assign Macro to blue button

You might also like