13
Range (“A1:A10).Value=”Visual Basic “ Range (“C11”).Value=Range (“A11”).Value Range (“B11”).Value Range ("A1") = 100 Range (“A1”).Value= “VBA” Dim VariableName as String * n Where n defines the number of characters the string can hold. For example, Dim

Vba Document

  • Upload
    jyoti

  • View
    223

  • Download
    3

Embed Size (px)

DESCRIPTION

complete VBA notes for the excel programming

Citation preview

Range (A1:A10).Value=Visual Basic Range (C11).Value=Range (A11).Value Range (B11).ValueRange ("A1") = 100Range (A1).Value= VBADim VariableName as Sring ! n"#ere n $e%ines #e number &% c#aracers #e sring can #&l$. '&r e(am)le* Dim+&urName as Sring ! 10 mean +&urName can #&l$ n& m&re #an 10 C#aracers.,#e use &% -)i&n .()lici is & #el) us & rac/ err&rs in #e usage &% 0ariable names 1i#in a )r&gram c&$e. '&r e(am)le* i% 1e c&mmi a +)&* VB. 1ill )&) u) an err&r message Variable n& $e%ine$. 2n$ee$* -)i&n .()lici %&rces #e )r&grammer & $eclare all #e 0ariables using #e Dim /e+1&r$. 2 is a g&&$ )racice & use -)i&n .()lici because i 1ill )re0en us %r&m using inc&rrec 0ariable names $ue & +)ingerr&rs* es)eciall+ 1#en #e )r&gram ges larger. "i# #e usage &% -)i&n .()lici* i 1ill sa0e us ime in $ebugging &ur )r&grams.firstNumber=100secondNumber=firstNumber-99userName="John Lyan"userpass.Text = passwordLabe1.!isibe = True"ommand1.!isibe = faseThirdNumber = !a#usernum1.Text$tota = firstNumber % secondNumber%ThirdNumber&ption 'xpicit(ri)ate *ub "ommand+utton1,"ic- #$.im number1/ number0/ number1 as *in2e.im tota/ a)era2e as .oubenumber1="es #1/ 1$.!auenumber1="es #0/ 1$.!auenumber1= "es #1/ 1$.!aueTota=number1%number0%number13)era2e=Tota41"es #5/ 1$ =Tota"es #6/ 1$ =3)era2e'nd *ub*ub msss2box#$.im yourms2 3s *trin2"es#1/ 0$ = #"your first )ba pro2ram"$yourms2 = "es#1/ 0$ms2box yourms2'nd *ubAn InputBox( ) is a function that displays an input box where the user can enter avalue or a message in the form of text. The format ismyMessage=InputBox(Prompt,Title,default, text/ x-position/ y-position$ Private Sub CommandButton1Cli!"(#$im x, y %s Integerx = Int(&nd ' 1(# ) 1y = x Mod *If +ot y = ( T,enMsgBox - x is an odd number-.lseMsgBox - x is an even number-.nd If.nd Sub/or next loop syntax is0Private Sub CommandButton1Cli!"(#$im i %s Integer/or i = 1 To 1(Cells(i, 1#12alue = i+ext.nd Sub/or loop program0*ub foroop#$.im i 3s 7nte2er8or i = 1 To 100 *tep 5"es#i/ 11$.!aue = iNext'nd *ubInputbox functionalityPrivate Sub CommandButton1Cli!"(#$im userMsg %s StringuserMsg = InputBox(-3,at is your message4-, -Message .ntry /orm-, -.nteryour messge ,ere-, 5((, 6((#Cells(1,1#12alue=userMsg.nd Sub$o 7,ile programPrivate Sub CommandButton1Cli!"(#$im !ounter %s Integer$o!ounter = !ounter ) 1Cells(!ounter, 1# = !ounter8oop 3,ile !ounter 9 1(.nd SubIn this example, the program will keep on adding 1 to the preceding counter value aslong as the counter value is less than 10. It displays 1 in cells (1,1), 2 in cells(2,1)..until 10 in cells (10,1).Private Sub CommandButton1Cli!"(#$im !ounter %s Integer$o :ntil !ounter = 1(!ounter = !ounter ) 1Cells(!ounter, 1# = 11 ; !ounter8oop.nd Suba) $o 3,ile !onditionBlo!" of one or more 2B statements8oopb) $oBlo!" of one or more 2B statements8oop 3,ile !onditionc) $o :ntil !onditionBlo!" of one or more 2B statements8oopd) $oBlo!" of one or more 2B statements8oop :ntil !onditionAlthough Excel VBA is not a truly object oriented programming language, it does deal with objects. VBA object is something like a tool or a thing that has certain functions andproperties, and can contain data.Cell is also an Excel VBA object, but it is also the property of the range object. So anobject can also be a property, it depends on the hierarchy of the objects. Range hashigher hierarchy than cells, and interior has lower hierarchy than Cells, and color haslower hierarchy than Interior, so you can write&ange(-%10%=our&ange@#1/ont1Bold=True&ange (>=our&ange@#1/ont1Itali!=True&ange (>=our&ange@#1/ont1:nderline=TrueThe FontStyle property can actually be used to replace all the properties above toachieve the same formatting efects. The syntax is as follows:&ange (>=our&ange@#1/ont1/ontStyle=@Bold Itali! :nderline@Private Sub CommandButton*Cli!"(#&ange(-%F0B1(-#1/ont1ColorIndex = F.nd SubTo sum up t7o !olumns values t,roug, vba t,e !ode is0Private Sub CommandButton1Cli!"(#&ange(-%10B