1 / 18

VBA for Excel

VBA for Excel. AAHA ( voor intern gebruik ).

kasi
Télécharger la présentation

VBA for Excel

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. VBA for Excel AAHA (voor intern gebruik)

  2. Private Sub CommandButton1_Click()‘ eenberichtplaatsenMsgBox “ hello"' cel A1 krijgt de waarde "hello" Range("A1").Value = "Hello" 'message met bericht“entered value is” de waarde in cel A1 gevolgd door eennieuweregel"This is fun"MsgBox "entered value is " & Range("A1").Value & vbNewLine & "This is fun"' cel B1 gelijkstellenaan 100 en de waarde van B1 vermenigvuldigen in cel C1 Range("B1").Value = 100 Range("C1").Formula = Range("B1") * 2 ' uitwissen van waarde in cel A1 Range("A1").ClearContents' toevoegen van eencommentaaraaneencel Range("A2").AddComment "dit is mijncommentaar"' maaralseencommentaarer reeds staatdantervoorkoming van error de volgendekeer 'uitwissencommentaar Range("A2").ClearCommentsEnd Sub

  3. Private Sub CommandButton2_Click()'Integer variable stores whole numbers Dim x As Integer ' declares variable with name x of type integer x = 9 'initialize the variable: assigning a beginning value to a variable Range("A7").Value = x ' place the value assigned to the variable x into cell A7 'Double variable store decimal numbers'double more accurate than integer but need more space. As a result code will run slower 'errors easier to find when use variables of right type Dim y As Double y = 5.5MsgBox "value is " & y ' String variables are used to store text Dim b As String b = "bible" Range("A8").Value = b' Boolean variables are used to store value true or false Dim continue As Boolean continue = True If continue = True Then MsgBox "Boolean variables are cool "End Sub

  4. Private Sub CommandButton3_Click()'to get the year of a date. Dim exampleDate As Date 'First, we declare a date using the Dim statementexampleDate = DateValue("Jun 19, 2010") 'To initialize a date, we use the DateValue functionMsgBox Year(exampleDate)'To add a number of days to a date, use the DateAdd function Dim firstDate As Date, secondDate As DatefirstDate = DateValue("Jun 19, 2010")secondDate = DateAdd("d", 3, firstDate) '"d" to "m" to add a number of months to a dateMsgBoxfirstDate & vbNewLine & secondDate'msgboxfirstDate en secondDate op de volgenderegel Range("A9").Value = firstDate'place the value assigned to the variable firstDate in cel A9 Range("A10").Value = secondDate 'place the value assigned to the variable secondDate in cel A10MsgBox Date & " is present date" 'To get the current dateMsgBox Now 'To get the current date and time, use the Now functionMsgBox Hour(Now) 'gets the hour of the current time'TimeValue 'The TimeValue function converts a string to a time serial number. The time's serial number is a number between 0 and 1. 'For example, noon (halfway through the day) is represented as 0.5.MsgBoxTimeValue(Now) Dim y As Double y = TimeValue("12:10:01")MsgBox y & " this is time as a numbers between 0 and 1"End Sub

  5. Text manipulatiePrivate Sub CommandButton4_Click()'gebruik van & operator om strings (tekst) samentevoegen Dim text1 As String, text2 As String text1 = "Constance" text2 = "Lee"MsgBox text1 & text2'de vier begin letters van de voornaamextraherenMsgBox Left(text1, 4) ' de laatste 2 letters van eennaamextraherenMsgBox Right(text1, 2)'ompositie van een substring in een string tevinden. Resultaat: string “tan” tevinden op positie 5 in ConstanceMsgBoxInStr(text1, "tan") ' omeen substring teextraheren, beginnend op positie 5 van text1 met eenlengte van 3. resultaat:tanMsgBox Mid(text1, 5, 3)End Sub

  6. TellerPrivate Sub CommandButton5_Click() Dim x As Integer 'declareren van variabele x van het type integer x = Range("A11").Value 'initieren van eenvariabele x = x + 1 '= betekent: wordt. Aan de huidigewaarde van x wordt 1 toegevoegd. Range("A11").Value = xEnd Sub IF thenPrivate Sub CommandButton6_Click() Dim score As Integer, grade As String score = Range("A11").Value 'Only if there is one code line after Then and no Else statement, 'it is allowed to place a code line directly after Then and to omit End If. If score >= 5 Then grade = "passed" Range("B11").Value = grade 'opm: Instead of multiple If Then statements, you can use Select Case ( wordt later behandeld)End Sub

  7. IF Then Else Private Sub CommandButton7_Click() 'nu een if met Else Statement Dim score As Integer, grade As String score = Range("A11").Value If score >= 6 Then 'start a new line after the words Then and Else and end with End If ' grade = "voldoende" Else grade = "onvoldoende" End If Range("B12").Value = gradeEnd Sub Cells Instead of the Range objectPrivate Sub CommandButton8_Click()'Instead of the Range object, you can also use Cells 'For example, Cells(4,2).value is the same as Range("B4").value. rownumber 4 and columnnumber 2 'using Cells is particularly useful when we want to loop through ranges (wordt later behandeld) Cells(4, 2).Value = 100End Sub

  8. Loop (For.........Next) ‘Alswaarde van rij 1 van kolom 8 t/m 12 (duskolom h t/m k)= 0 dantellen, m.a.w. countif(H1:K1)= 0Private Sub CommandButton9_Click() Dim total As Integer, i As Integer ‘ipv van ikanevengoedkolomstaanalsvariabelenaam total = 0 For i = 8 To 12 ' duskolom h t/m k Cells(1, i).Select 'Code omtecheckenwatergebeurt in de loop. SlechtsvoorillustratieMsgBox "i = " & i'Code omtecheckenwatergebeurt in de loop. Slechtsvoorillustratie. If Cells(1, i).Value = 0 Then total = total + 1 ‘ alswaarde in kolom 8 (begin kolom) = 0 dantoevoegenaan total Next i'Wanneer Excel VBA de volgendeibereikt, springt het terugnaar de For statement ‘en vermeerderti met 1. Kolom 8 wordtdan 9 etc. MsgBox total & " cellen op het bereik met een waarde gelijk aan 0" 'na het verlaten van de For next Loop wordtditverderuitgevoerd.End Sub

  9. Opdracht 1Plaats op het bereik P1:P5 waarden die boven en onder 32 liggen. Maakeenknop die op het bereik O1:O5 de waarden high en low neerzetnaastiederewaarde in kolom P. Waardenboven 32 zijn high en waardenonder 32 low.

  10. Opdracht 1 en oplossing

  11. Random (plaatsen van eenwillekeurigewaarde in eencel) Private Sub Random_Click() Dim Rand As Integer Rand = Int((45-20+1) * Rnd + 20) ' Intverwijdertdecimalen. Rndfunktiegeefteenwaarde >= 0 en <1 'willekeurigewaarde=Int ((hoogstewaarde - laagstewaarde + 1) * Rnd + laagstewaarde) ' dusInt(45-20+1)*Rnd + 20) geefteenwaardevanaf 20 tot en met 45 Cells(1, 2).Value = Rand ' de waardewordtneergezet in cel B1End Sub

  12. Opdracht2Plaats met behulp van knop willekeurigewaarden op het bereik C1:C5 waardenvanaf 10 tot 20. Maakeenandereknopdie op het bereik D1:D5 de waarden high en low neerzetnaastiederewaarde in kolom C. De waarden 15 en hogerzijn high en waardenonder 15 low.

  13. Opdracht 2 en oplossing

  14. SwapPrivate Sub CommandButton1_Click()'This example teaches you how to swap two values in Excel VBA 'to make button: Developer's tab, insert, activeXcontrolDim temp As Double 'declare a variable called temp of type Doubletemp = Range("A1").Value 'initialize the variable temp with the value of cell A1. Range("A1").Value = Range("B1").Value 'Now we can safely write the value of cell B1 to cell A1 '(we have stored the value of cell A1 to temp so we will not lose it).Range("B1").Value = temp 'Finally, we write the value of cell A1 (written to temp) to cell B1End Sub kleurenPrivate Sub CommandButton4_Click() Range("A10").Interior.ColorIndex = 26 ' kleurtcel A10 paars. Colorindexgaat van 0 t/m 56 Range("A10").Value = 20 'plaats de waarde 20 in cel A10 Cells(10, 1).Font.Size = 24 ' gebruiklettergrootte 24 in cel A10 Cells(10, 1).Font.ColorIndex = 4 ' plaats de waarde in eenkleur (groen) Range(“A11:A12").Interior.Color = RGB(200, 160, 35) ' range A11:A12 wordtgekleurd. RBG combinatie. End Sub

  15. Opdracht 3Cel C2 heefteenwillekeurigewaardetussen 0 en 10. De waarde in C3 is de helft van C2. De huidigewaarde van A2 wordtvastgehouden in eenvariabele“temp”. Vervolgensverandert de waarde in A2 in C2 + 1. B2 krijgt de oorspronkelijkewaarde van A2 die opgeslagen is in temp. Op het bereik C2: C6 hebben we verschillendekleuren in de individuelecellen.

  16. oplossingopdracht 3

  17. creatiefbezigzijnPrivate Sub CommandButton3_Click() Dim i As Long For i = 0 To 56 Cells(i + 1, 8).Interior.ColorIndex = i'kleurtcel in kolom 8 beginnendebijrij 1 Cells(i + 1, 9).Value = "Color " & i'plaats de tekstcolor en nummer van i Cells(i + 1, 10).Value = "prima" 'plaats de tekst prima in kolom 10 Cells(i + 1, 10).Font.ColorIndex = i' kleurt de tekst in kolom 10MsgBox "let op de volgendekleur " & i' magweggelatenworden. Checkt de kleurstapvoorstap Next iEnd Sub

  18. Opdracht 4: Impress us.Maakm.b.v. hetgeen je tot nu toe hebtgeleerdeeneigen(creatief) ontwerp.Stuur de oplossing van de 4 opdrachtennaaricaaha2010@gmail.com

More Related