VBA Notes
VBA Notes
To insert a commant, use " ' " it will be indicated in green and not be part of the
code
*Keyboard ShortCut*
Ctrl+Spacebar - para mag pop up lahat ng mga commands
*Ctrl + space bar and then len command is short for length*
len(FilmName
*When we say mod it will return the remainder of the divided number*
___________________________________________________________________________
VARIABLES
*What is variable*
Basically dito na iistore yung information na nilagay mo
*Declare variables before sub routine or after option explicit for it to recognized
in whole module*
*Use public instead of dim if you want the variables to declare even in other
modules*
*Other things is pag public instead of dim ginamit na reretain yung value ng
variable unlike sa dim na nagrereset*
___________________________________________________________________________
MESSAGE BOXES
___________________________________________________________________________
INPUT BOXES
*How to capture the results of InputBox*
Dim YourName As string
YourName = inputbox ("Please type your name")
Msgbox "Hello " & YourName
___________________________________________________________________________
datFilmDate = CDate(strFilmDate)
___________________________________________________________________________
*Advantage of application.inputbox *
Yung mga limitation sa inputbox kaya ma gawan ng paraan sa application.inputbox
Kusa narin mag kakaron ng for example : You did not enter a valid number, etc.
Unlike sa inputbox na i cocode mo pa yung output na yan
CopyRange.copy PasteDestination
___________________________________________________________________________
WITH STATEMENT
*useful siya pag finoformat mo yung mga cell kasi pwedeng di mo na siya paulit ulit
na irefer*
With Range("C3:C15")
.Font.Color = vbBlue
.Font.Italic = True
End With
___________________________________________________________________________
LOOP
or
Range("A3").select
Do
Activecell.Offset(1,0).Select
Loop Until Activecell.value = ""
___________________________________________________________________________
Sub SimpleForNextLoop()
Dim LoopCounter As Integer
Dim RandomNumber As Double
For LoopCounter = 1 To 10 Step 1
Debug.Print "LoopCounter = " & LoopCounter
RandomNumber = Math.Rnd
If RandomNumber > 0.7 Then Exit For
Next LoopCounter
End Sub
___________________________________________________________________________
Sub ListOfWorksheetNames()
Dim SingleSheet As Worksheet
For Each SingleSheet In Worksheets
SingleSheet.Unprotect
Next SingleSheet
End Sub
Sub LoopOverCells()
ThisWorkbook.Activate
Range("E3").Select
Else
ActiveCell.Value = "Long"
ActiveCell.Offset(1, 0).Select
End If
Next SingleCell
*How to do Nested for each loops?*
dim SingleBook as Workbook
dim SingleSheet as Worksheet
SingleSheet.Protect
next SingleSheet
Next SingleBook
___________________________________________________________________________
FIND
Else
FilmCell.Select
End If
FilmName = Application.InputBox( _
Prompt:="Type the name of film", Type:=2)
Else
FirstCell = FilmCell.Address
Do
End if
End Sub
___________________________________________________________________________
FUNCTION
*What is a function?*
A function is simply a procedure which returns a value and come in handy
whenever you find yourself writing out the same calculations again and again
End Function
Sub CreateNewWorkSheet()
Worksheets.Add
Range("A1").Value = "Created on " & CustomDate
End Sub
End Function
Sub CreateNewWorkSheet()
Worksheets.Add
Range("A1").Value = "Created on " & CustomDate(#8/6/1998#)
End Sub
___________________________________________________________________________
EVENT PROCEDURES
Cancel = True
End Sub
___________________________________________________________________________
ARRAY
*what is an array?*
An array is like a variable which you can store more than 1 value using the
same
variable name
*Option base 1*
para sa 1 mag simula yung count ng array at hindi sa fixed na 0
Sub FixedArray()
Dim TopThreeList(1 To 3) As String
End Sub
TopTenFilms(0, 0) = Range("A3")
TopTenFilms(0, 1) = Range("B3")
TopTenFilms(0, 2) = Range("C3")
TopTenFilms(0, 3) = Range("D3")
TopTenFilms(0, 4) = Range("E3")
End Sub
For Dimension1 = 0 To 9
For Dimension2 = 0 To 4
TopTenFilms(Dimension1, Dimension2) = Range("A3").Offset _
(Dimension1, Dimension2).Value
Next Dimension2
Next Dimension1
D1 = Range("A3", Range("A3").End(xlDown)).Cells.Count - 1
D2 = Range("A3", Range("A3").End(xlToRight)).Cells.Count - 1
Sub QuickWayDynamicArray()
Worksheets.Add
Erase TopFilms
End Sub
___________________________________________________________________________
CHARTS
Application.DisplayAlerts = False
On Error Resume Next
Charts.delete
*If you select a single cells, the chart will be based on the entire region*
Worksheets("Awards 2015").Activate
Charts.Add
ChartCells.Select
Charts.Add
*Referencing a Chart*
dim ch as Chart
worksheets("Shee1").select
Range("A3:C19").select
set ch = Charts.add
dim ch as Chart
worksheets("Shee1").select
Range("A3:C19").select
set ch = Charts.add
ch.chartType = xl3dpie
ch.HasLegend = true
ch.Hastitle = true
ch.ChartTitle.Text = "Noms vs. Wins"
dim ch as Chart
worksheets("Shee1").select
Range("A3:C19").select
set ch = Charts.add
ch.axes(xlcategory).HasTitle = true
ch.axes(xlcategory).AxisTitle.Text = "Film Name"
ch.axes(xlvalue).HasTitle = true
ch.axes(xlvalue).AxisTitle.Text = "Quantity"
dim ch as Chart
worksheets("Shee1").select
Range("A3:C19").select
set ch = Charts.add
ch.seriescollection(1).hasdatacollections = true
ch.seriescollection(2).hasdatacollections = true
*how to add data labels when there is too many*
dim s as series
dim ch as Chart
worksheets("Shee1").select
Range("A3:C19").select
set ch = Charts.add
https://www.youtube.com/playlist?list=PLGs61FH9ETZPGqw-RIwteTlfsPPpOp-qP