0% found this document useful (0 votes)
49 views7 pages

Vba Sta

This document contains examples of using VBA (Visual Basic for Applications) to interact with Excel objects like worksheets, cells, ranges, and workbooks. It demonstrates how to select cells and ranges, enter formulas, format cells, work with workbooks and worksheets, and use variables and data types in VBA.

Uploaded by

umberto d'auria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views7 pages

Vba Sta

This document contains examples of using VBA (Visual Basic for Applications) to interact with Excel objects like worksheets, cells, ranges, and workbooks. It demonstrates how to select cells and ranges, enter formulas, format cells, work with workbooks and worksheets, and use variables and data types in VBA.

Uploaded by

umberto d'auria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Immediate window ctrl g

Worksheets.Add
Range("A1:A4").Select
[A1:A4].Value = 55
Selection.ClearContents
ActiveCell.Select
?Selection.Address
$A$1:$A$4
Print ActiveWorkbook.Name
Book2
?Application.Name
Microsoft Excel
?12/3
4
?3+2:?12*8
5
96
m=1

n=2

sumtu

print sumtu

''working with worksheet cells and ranges

range("A2").Select

[a1].select

range("a6:a10").Select

[a6:a10].select

range("a1,b6,c3").Select

range("a11:d11, c12, d3").Select

cells(5,1).select

range(cells(6,1),cells(6,5)).Select

cells.Select

cells(5,2).select
cells(1,1).offset(1,3).select

cells(15,4).offset(-2,-1).select

activecell.Offset(-1).Select

''using the resize property

range("a5:a10").Select

selection.offset(2,2).select

selection.resize(2,4).select

range("a2").Select

activecell.FormulaR1C1="text"

cells(5,1).select

'select the last cell in any row

activecell.End(xlToRight).Select

'moving copying and deleting cells

cells(5,1).formulaR1c1="ciaone"

range("a5").cut

destination:=range("a1")

cells(5,1).copy

destination:=range("a1:a5")

cells(5,1).Clear

cells(5,1).clearcontents

cells(5,1).clearformats
cells(5,1).clearcomments

'entering data and formatting cells

activesheet.range(cells(4,1),cells(4,3)).value="= 4*25"

activesheet.range("a1:a4").formula="= 4*25"

cells(5,1).formula="amount due"

cells(21,4).value=123

cells(21,4).formula=123

cells(4,2).formula= "=d21*3"

?cells(4,2).value

369

?cells(4,2)

369

?cells(4,2).formula

=D21*3

range("a1").NumberFormat="$0.00"

?range("b4")

369

'working with workbooks and worksheets

workbooks.Add

?workbooks(1).Name

Book1

?workbooks.Count

workbooks(1).Activate

activeworkbook.SaveAs ciaoo..
workbooks(2).Closek;jshdqviuenhfiuve

activeworkbook.close savechanges...

workbooks.clos e

worksheets.Add

?worksheets(1).name

Sheet7

worksheets(3).select

worksheets(array(1,3,4)).select

worksheets("sheet5").select

worksheets("sheet5").move

before:=worksheets("sheet7")

worksheets("sheet7").name= "expenses"

worksheets("sheet5").delete

'The Select and Activate methods can be used interchangeably if only one worksheet is selected.

If you select a group of worksheets, the Activate method allows you to decide which one of the
selected worksheets is active. As you know, only one worksheet can be active at a time.

'excel objects, properties and methods

an OBJECT is a thing u can control with vba. workbook,worksheet,range..

objects are organized in a hierarchy. Excel is an application object. the application object contains
other objects, such as Workbooks or command bar.

a group of like objects is called a collection (workbooks colelction, worksheets collections)

a property let me change the look of the object

objects have method, each action I want the object to perform is called method

the most important method is add method


clearcontents method ecc..

workbooks("clajciaj").Close savechanges:=false

'activecell(object),offset(property)(3,2)(arguments)

'changing the property of an object

'object.property=value

'activecell.font.name="times new roman"

'activecell.font.bold=true

'returning the current value of the object property

'variable object property

'cellvalue=range("a4").value this instruction saves the current value of cell a4 in the variable
named cellvalue

'object method

'range("A2").clearcontents

'vba data types

'boolean true or false

'byte 0 to 255

'integer -32768 to 32767

'long integer

'longlong integer

'currency

'decimal

'date

'using variable

dim dateofbirth as Date

dim age as Integer

dim fullname as String


Sub agecalc()

Dim fullname As String

Dim dateofbirth As Date

Dim age As Integer

fullname = "ciao io "

dateofbirth = #1/3/1990#

age = Year(Now()) - Year(dateofbirth)

Debug.Print fullname & "is" & age & "years old"

dim fullname as String, dateofbirth as Date, ..

Sub numofrows()

Dim numofrow As long

numofrow = Rows.Count

MsgBox "the worksheet has " & numofrow

End Sub

'dim firstname$ means as a string

' integer % long& single! double# currency@ string$

sub calcost()

slsprice=35

slstax=0.085

cells(1,1).formula="cicici"

cells(1,2).formula=slsprice * slstax

cost= slsprice+(slsprice*slstax)

with range("b6")

.formula=Cost
.numberformat="0.00"

endwith

strMsg="skjdbvsdjn"&...

cells(8,1).formula=strMsg

end sub

'if i want use a costant within a single procedure, declare it at the procedure leve, just below the

'name of the procedure, so..const age as integer =24

'if I want use it in all the procedures I use private const diverletter as string = "c:"

I d like to work for ur company firstly because of the fact that It is one of the biggest company in the
real estate industry widley known all over the world. I would like to work for the brand that it is
recognizable and important in the market. This will enable me to have much more possibilities to
grow and to have a multicultural working environment.

You might also like