De presentatie wordt gedownload. Even geduld aub

De presentatie wordt gedownload. Even geduld aub

AAHA (voor intern gebruik)

Verwante presentaties


Presentatie over: "AAHA (voor intern gebruik)"— Transcript van de presentatie:

1 AAHA (voor intern gebruik)
Slide 1 Titel slide Slide 2 een bericht plaatsen waarde plaatsen in een cel m.b.v range (" celadres").value waarde plaatsen in een cel en een formule opstellen (vb. vermenigvuldigen) uitwissen van een waarde in een cel toevoegen van een commentaar in een cel Slide 3 Variabelen numerieke variabelen: integer en double string: tekst Boolean Slide 4 Vervolg variabelen datum variabelen gebruik van function: Datevalue, Dateadd, Now, Timevalue Slide 5 Text manipulatie samenvoeging van tekst gebruik van de functies: Left, Right, Instr, Mid Slide 6 maken van een teller If then Slide 7 If then else waarde plaatsen in een cel m.b.v Range (" celadres").value (herhaling) waarde plaatsen in een cel m.b.v Cells (rij,kolom).value slide 8 Combinatie van: Loop: For….next, teller, If en msgbox Slide 9 opdracht 1 Slide 10 oplossing Opdracht 1 Slide 11 functies: Rnd ( met bepalen van een Range), Int Slide 12 opdracht 2 willekeurige waarden neerzetten en status bepalen slide 13 oplossing opdracht 2 slide 14 swap en kleuren van cellen en ranges slide 15 opdracht 3 slide 16 oplossing opdracht 3 slide 17 opdracht 4 VBA for Excel AAHA (voor intern gebruik)

2 Private Sub CommandButton1_Click() ‘ een bericht plaatsen MsgBox “ hello" ' cel A1 krijgt de waarde "hello" Range("A1").Value = "Hello" 'message met bericht “entered value is” de waarde in cel A1 gevolgd door een nieuwe regel "This is fun" MsgBox "entered value is " & Range("A1").Value & vbNewLine & "This is fun" ' cel B1 gelijk stellen aan 100 en de waarde van B1 vermenigvuldigen in cel C1 Range("B1").Value = Range("C1").Formula = Range("B1") * ' uitwissen van waarde in cel A1 Range("A1").ClearContents ' toevoegen van een commentaar aan een cel Range("A2").AddComment "dit is mijn commentaar" ' maar als een commentaar er reeds staat dan ter voorkoming van error de volgende keer 'uitwissen commentaar Range("A2").ClearComments End Sub

3 Private Sub CommandButton2_Click() 'Integer variable stores whole numbers Dim x As Integer ' declares variable with name x of type integer x = 'initialize the variable: assigning a beginning value to a variable Range("A7").Value = x ' place the value assigned to the variable x into cell A '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 = MsgBox "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
Private Sub CommandButton3_Click() 'to get the year of a date. Dim exampleDate As Date 'First, we declare a date using the Dim statement exampleDate = DateValue("Jun 19, 2010") 'To initialize a date, we use the DateValue function MsgBox Year(exampleDate) 'To add a number of days to a date, use the DateAdd function Dim firstDate As Date, secondDate As Date firstDate = DateValue("Jun 19, 2010") secondDate = DateAdd("d", 3, firstDate) '"d" to "m" to add a number of months to a date MsgBox firstDate & vbNewLine & secondDate 'msgbox firstDate en secondDate op de volgende regel 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 A MsgBox Date & " is present date" 'To get the current date MsgBox Now 'To get the current date and time, use the Now function MsgBox 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 'For example, noon (halfway through the day) is represented as MsgBox TimeValue(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 manipulatie Private Sub CommandButton4_Click() 'gebruik van & operator om strings (tekst) samen te voegen Dim text1 As String, text2 As String text1 = "Constance" text2 = "Lee" MsgBox text1 & text 'de vier begin letters van de voornaam extraheren MsgBox Left(text1, 4) ' de laatste 2 letters van een naam extraheren MsgBox Right(text1, 2) 'om positie van een substring in een string te vinden. Resultaat: string “tan” te vinden op positie 5 in Constance MsgBox InStr(text1, "tan") ' om een substring te extraheren, beginnend op positie 5 van text1 met een lengte van 3. resultaat:tan MsgBox Mid(text1, 5, 3) End Sub

6 Teller Private Sub CommandButton5_Click() Dim x As Integer 'declareren van variabele x van het type integer x = Range("A11").Value 'initieren van een variabele x = x '= betekent: wordt. Aan de huidige waarde van x wordt 1 toegevoegd. Range("A11").Value = x End Sub IF then Private 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 = grade End Sub Cells Instead of the Range object Private 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 'using Cells is particularly useful when we want to loop through ranges (wordt later behandeld) Cells(4, 2).Value = 100 End Sub

8 Loop (For Next) ‘Als waarde van rij 1 van kolom 8 t/m 12 (dus kolom h t/m k)= 0 dan tellen, m.a.w. countif(H1:K1)= 0 Private Sub CommandButton9_Click() Dim total As Integer, i As Integer ‘ipv van i kan evengoed kolom staan als variabele naam total = For i = 8 To 12 ' dus kolom h t/m k Cells(1, i).Select 'Code om te checken wat er gebeurt in de loop. Slechts voor illustratie MsgBox "i = " & i 'Code om te checken wat er gebeurt in de loop. Slechts voor illustratie If Cells(1, i).Value = 0 Then total = total ‘ als waarde in kolom 8 (begin kolom) = 0 dan toevoegen aan total Next i 'Wanneer Excel VBA de volgende i bereikt, springt het terug naar de For statement ‘en vermeerdert i met 1. Kolom 8 wordt dan 9 etc. MsgBox total & " cellen op het bereik met een waarde gelijk aan 0" 'na het verlaten van de For next Loop wordt dit verder uitgevoerd. End Sub

9 Opdracht 1 Plaats op het bereik P1:P5 waarden die boven en onder 32 liggen. Maak een knop die op het bereik O1:O5 de waarden high en low neerzet naast iedere waarde in kolom P. Waarden boven 32 zijn high en waarden onder 32 low.

10 Opdracht 1 en oplossing

11 Random (plaatsen van een willekeurige waarde in een cel) Private Sub Random_Click() Dim Rand As Integer Rand = Int(( ) * Rnd + 20) ' Int verwijdert decimalen. Rnd funktie geeft een waarde >= 0 en < 'willekeurige waarde=Int ((hoogste waarde - laagste waarde + 1) * Rnd + laagste waarde) ' dus Int( )*Rnd + 20) geeft een waarde vanaf 20 tot en met Cells(1, 2).Value = Rand ' de waarde wordt neergezet in cel B1 End Sub

12 Opdracht 2 Plaats met behulp van knop willekeurige waarden op het bereik C1:C5 waarden vanaf 10 tot 20. Maak een andere knop die op het bereik D1:D5 de waarden high en low neerzet naast iedere waarde in kolom C. De waarden 15 en hoger zijn high en waarden onder 15 low.

13 Opdracht 2 en oplossing

14 Swap Private Sub CommandButton1_Click() 'This example teaches you how to swap two values in Excel VBA 'to make button: Developer's tab, insert, activeX control Dim temp As Double 'declare a variable called temp of type Double temp = 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 A '(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 B1 End Sub kleuren Private Sub CommandButton4_Click() Range("A10").Interior.ColorIndex = 26 ' kleurt cel A10 paars. Colorindex gaat van 0 t/m Range("A10").Value = 'plaats de waarde 20 in cel A Cells(10, 1).Font.Size = 24 ' gebruik lettergrootte 24 in cel A Cells(10, 1).Font.ColorIndex = 4 ' plaats de waarde in een kleur (groen) Range(“A11:A12").Interior.Color = RGB(200, 160, 35) ' range A11:A12 wordt gekleurd. RBG combinatie. End Sub

15 Opdracht 3 Cel C2 heeft een willekeurige waarde tussen 0 en 10
Opdracht 3 Cel C2 heeft een willekeurige waarde tussen 0 en 10. De waarde in C3 is de helft van C2. De huidigewaarde van A2 wordt vastgehouden in een variabele“temp”. Vervolgens verandert de waarde in A2 in C B2 krijgt de oorspronkelijke waarde van A2 die opgeslagen is in temp. Op het bereik C2: C6 hebben we verschillende kleuren in de individuele cellen.

16 oplossing opdracht 3

17 creatief bezig zijn Private Sub CommandButton3_Click() Dim i As Long For i = 0 To Cells(i + 1, 8).Interior.ColorIndex = i 'kleurt cel in kolom 8 beginnende bij rij Cells(i + 1, 9).Value = "Color " & i 'plaats de tekst color en nummer van i Cells(i + 1, 10).Value = "prima" 'plaats de tekst prima in kolom Cells(i + 1, 10).Font.ColorIndex = i ' kleurt de tekst in kolom MsgBox "let op de volgende kleur " & i ' mag weggelaten worden. Checkt de kleur stap voor stap Next i End Sub

18 Opdracht 4: Impress us. Maak m. b. v
Opdracht 4: Impress us. Maak m.b.v. hetgeen je tot nu toe hebt geleerd een eigen (creatief) ontwerp. Stuur de oplossing van de 4 opdrachten naar


Download ppt "AAHA (voor intern gebruik)"

Verwante presentaties


Ads door Google