Vba Session3
Vba Session3
infotica
[email protected]
?ISDATE(#23-JUN-2014#)
True
?ISDATE(#29-FEB-2012#)
True
?ISDATE(#30-FEB-2012#)
Now
Time
Time$
Hour(time)
Minute(time)
Second(time)
StrReverse
?ISDATE(#23-JUN-2014#)
True
?ISDATE(#29-FEB-2012#)
True
infotica
[email protected]
?ISDATE(#30-FEB-2012#)
?NOW
7/26/2013 11:11:06 AM
?TIME
11:11:22 AM
?hour(time)
11
?minute(time)
12
?second(time)
19
'
?StrReverse("acbdfdf1234")
infotica
[email protected]
?val("4321fdfdbca")
4321
?StrReverse(val(StrReverse("acbdfdf1234")))
1234
Round
'ROUND(NUMBER,[NUMBER OF DIGITS AFTER DECIMAL])
?ROUND(234.567)
235
?ROUND(234.567,2)
234.57
Use Relative References:
Macros are recorded with reference to the initial selected cell.
This option can be switched on/off while recording.
Working with Personal Macro Workbook
Where is the path?
*Varies based on versions
The best way to find the path of Personal Macro Workbook :
Ctrl+G
infotica
[email protected]
?Application.startuppath
Note : PMW is stored under XLSTART folder
PMW is used to store the commonly used macros
PMW is a hidden workbook
PMW gets loaded into excel on each session
PMW can be viewed in Project Explorer
PMW contains one sheet by default, and sheets can be added
To view PMW from Excel :
View -> Unhide -> Personal -> OK
Note : PMW, just like any workbook, can store an unlimited no. of
macros
Note : PMW can be deleted
Note : Workbooks placed in XLSTART folder are loaded into excel on
each session
infotica
[email protected]
*Changing/Removing SHORTCUT/DESCRIPTION
Alt+F8
Click OPTIONS
Change/Remove
Click OK
Option Explicit
A module level statement that makes variable declaration
mandatory in all programs in the current module
Write two programs with wrong/undeclared variable names and run
Option Explicit
Sub pro3()
Dim a As Integer, b As Integer
a = 10: b = 20
'Variables can be declared anywhere in the program
Dim c As Integer
c=a+b
MsgBox c
End Sub
Sub pro4()
Dim x As Integer
infotica
[email protected]
y = 10 'error
End Sub
'set option explicit as default :
'Tools -options - Editor - Require variable declaration
Types of Errors:
1. Compile Errors
Compiler : An internal feature
that converts source-code(your program)
into object-code(binary language)
Ex : Syntatctical errors
2. Runtime Errors
Syntactically correct:
Resource missing
object name etc.
3. Logical Errors
Programming approach/logic
infotica
[email protected]
localswindow &
debug.print are used to debug the
programs
Lucy(a dog)
Summary(a sheet)
Descriptions
Sex : Female
Age : 6 months
Height,weight,
Rows
Columns
Name
Tab-color
Actions
Bark,jump,wag,hunt,smell
Move,copy,delete
Classes : workbook, worksheet, range etc
infotica
[email protected]
infotica
[email protected]
10
infotica
[email protected]
?Activesheet.name
Sheet1
?activecell.Address
$A$1
'syntax : range().Address([rowAbsolute],[ColumnAbsolute])
?activecell.Address(false,false)
A1
?activecell.Address(,false)
A$1
11
infotica
[email protected]
?activecell.Address(false)
$A1
'xl...
: excel constants
?activeprinter
PDF-XChange 3.0 on Ne01:
Select
'select
range("a10").Select
range("a1:B5").Select
range("a1:B5,f5:f10").Select
range("a:A").Select
range("a:c").Select
range("a:c,f:f").Select
range("a:c,f:f,g5").Select
12
infotica
[email protected]
range("1:1").Select
range("1:3").Select
range("1:3,7:7").Select
range("a:c,f:f,2:10,g5").Select
Activate
range("a1:B5").Select
'activate method changes activecell in the current selection
'OR it can be used like SELECT
range("b3").Activate
range("e2").Activate
range("e2:e5").Activate
Value
'value
'to set /get the value of a range
'It is optional
range("A1").Value="Sample Text"
range("b1:b5")="excel vba"
range("b1:b5")=100.567
range("b1:b5")=#12-may-2014#
13
infotica
[email protected]
range("A1")=""
range("B:B").Columns.AutoFit
Autofit
Font
'colors in excel 2007
'1 to 56 default colors
'custom colors
'16 Millions
range("A1:b5")="Sample Text"
range("A1:b2").Font.Bold=true
range("A1:b2").Font.Italic=true
range("A1:b2").Font.Underline=true
Sub fontEx()
'*with is used to avoid repetitions in object reference
'Code runs faster
'*WITH ... must be ended with END WITH
With Range("A1:b2").Font
.Bold = True
.Italic = True
.Underline = True
.Name = "Impact"
End With
14
infotica
[email protected]
End Sub
Color/ColorIndex
'DEFAULT COLORS : COLORINDEX
'CUSTOM COLORS : COLOR
RANGE("a1:b2").Font.ColorIndex=4
?RANGE("a1:b2").Font.ColorIndex
4
RANGE("a1:b1").Font.Color=14567899
?RANGE("a1").Font.ColorIndex
39
'rgb() 'min 0 max 255
range("F1").Interior.Color=rgb(199,112,18)
?range("F1").Interior.Color
1208519
Cut/Paste
Sub cutPasteEx1()
'within the same sheet
Range("A1:b2").Cut Range("h5")
End Sub
Sub cutPasteEx2()
'b'ween 2 sheets
15
infotica
[email protected]
Sheets("sheet1").Range("h5:i6").Cut _
Sheets("sheet3").Range("a1")
End Sub
Sub cutPasteEx3()
'b'ween 2 sheets in 2 wbks
Workbooks("book6").Sheets("sheet3").Range("a1:b2").Cut _
Workbooks("book7").Sheets("sheet1").Range("a1")
End Sub
Copy/Paste
Option Explicit
Sub copyPasteEx1()
'one time
'range.Copy [destination]
Range("A1:a4").Copy Range("f1")
End Sub
Sub copyPasteEx2()
'multiple times
'range.Copy [destination]
Range("A1:a4").Copy
Range("f1").PasteSpecial
Range("g1").PasteSpecial
16
infotica
[email protected]
Application.CutCopyMode = False
'to clear the data from os clipboard
PasteSpcial
17
infotica
[email protected]
Branching/Decision Making
IIF
IF complete syntax
IF single line
IF multiline
IF multiline with Else
If multiline with ElseIF Else
Nested IF