VBA_01 Flashcards
(28 cards)
Alle Zellen aus A in graue Linien
Range(“A1:A” & Cells(Rows.Count, “A”).End(xlUp).Row).Borders.LineStyle = xlContinuous
Range(“A1:A” & Cells(Rows.Count, “A”).End(xlUp).Row).Borders.ColorIndex = 48
Was kann ich mit UBound machen?
UBound(SourceArray(), [Dimension As Long = 1]) As Long
Gibt den größten erlaubten Indexwert für die Dimension eines Arrays zurück.
Function Obergrenze()
Dim Zeichen(65 To 90)
Obergrenze = UBound(Zeichen) ‘ Gibt 90 zurück
End Function
Wie kann ich Text austauschen?
Sub ZeichenketteErsetzen2()
‘Variable definieren
Dim Text As String
‘Wert zuweisen
Text = “Ich liebe Pizza”
‘Zeichen ersetzen
MsgBox Replace(Text,” “,”-“)
End Sub
Wie kann ich einen ausgewählten Bereich grau einfärben?
Dim rng As Range
Set rng = Selection
rng.Interior.Color = RGB(100, 100, 100)
How to fasten the VBA macro?
We have several best practices to follow while coding VBA. This is also one of the most frequently asked Excel VBA Interview Questions and Answers. This helps interviewer to understand your real time experience in VBA.
We can fasten the execution of macros or VBA Procedures by following the below tips.
- Declare the variables and avoid using ‘Variant’ Data Type.
- Turn Off Screen Updating
- Turn Off Automatic Calculations
- Disable Events
- Use With Statement
- Use vbNullString instead of “”.
- Release memory objects at the end of the procedure
Die Zahlen in C1 bis E12 sollen alle durch 3 geteilt werden.
Dim C As Range
For Each C In Range(“C1:E12”)
C = C / 3
Next C
Wie erzeuge ich eine Message Box?
MsgBox (“kjdkfk”)
Was kann ich mit Join machen?
Join(SourceArray(), [Delimiter As String = “ “]) As String
Fügt einen eindimensionalen Array SourceArray zu einem String zusammen. Mit Delimiter kann eine Zeichenfolge angegeben werden, die zwischen den einzelnen Datenfeldern eingefügt werden soll.
Function Satz()
Dim Wort(2) As String
Wort(0) = “Das”
Wort(1) = “VBA”
Wort(2) = “Tutorial”
Satz = Join(Wort)
End Function
How to assign values to an array?
We can assign values to an array in the following way.
‘Declare an array variable
Dim aValue (2) As Integer
aValue(0)=”first”
aValue(1)= “Secon
Letzte Reihe
Dim lastRow As Integer
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Wie kann ich herkömmliche und Ganzzahldivision machen?
‘Herkömmliche Division
Range(“C1”).Value = 15 / 6
‘Ganzzahldivision
Range(“C2”).Value = 15 \ 6
Mention the difference between the Subroutines and Functions in VBA?
The difference between the Subroutines and Functions is that
- Subroutines never return a value, but functions does return values
- Subroutines could change the values of the actual arguments while a function could not change the value of actual arguments
What is the code to find a last used Row in a column or last used Column of a row?
To find the last row in a column, the command used is End(xlUp) and to find last column in a row, the command used is End(xlToLeft).
Für letzte Spalte besser ist: Range(“A1”).SpecialCells(xlCellTypeLastCell).Column
Kopiere von C4:C6 in Tabelle 2
Range(“C4:C6”).Copy Sheets(“Tabelle2”).Range(“C4”)
Wie kann ich eine Inputbox erstellen?
Dim eingabe As String
‘InputBox mit eigenem Dialogfeld
eingabe = inputbox(“Bitte geben Sie etwas ein”)
Wie kann ich ein bei einem Array die Größe neu anpassen?
‘Die Variablen definieren
Dim staedte()
Dim size As Integer
Dim i As Integer
‘Anzahl der Einträge in Spalte 1 der Variable size zuweisen
size = WorksheetFunction.CountA(Worksheets(1).Columns(1))
‘Die Größe des Arrays anpassen
ReDim staedte(size)
‘Dem Array die Werte aus Spalte 1 nach und nach zuweisen (Möglichkeit 2)
For i = 0 To size - 1
staedte(i) = Cells(i+1,1).Value
Next i
‘Größe des Arrays ausgeben lassen
MsgBox UBound(staedte)
Wie kann ich nach einem Begriff suchen?
Dim finden As Range
‘Nach dem Begriff “Schmidt” suchen
Set finden = Range(“A2:A9”).Find(what:=”Schmidt”)
‘Ausgeben lassen wie der gefundene Begriff heißt
MsgBox “Der gefundene Begriff lautet: “ & finden
‘Ausgeben lassen in welcher Zelle der Begriff steht
MsgBox “Der Begriff befindet sich in Zelle: “ & finden.Address
‘Die Zelle bearbeiten, in der sich der gefundene Begriff befindet
Cells(finden.Row,finden.Column).Interior.ColorIndex = 6
Wenn in dem Bereich C1 bis E12 eine 1 steht, soll die RGB-Farbe 200, 200, 200 eingefügt werden
Dim C As Range
For Each C In Range(“C1:E12”)
If C = 1 Then
C.Interior.Color = RGB(200, 200, 200)
End If
Next C
Die Adresse eines speziellen Strings (varLookFor) etc. finden
strAddress = Cells.Find(What:=varLookFor, LookAt:=xlWhole).Address
Was kann ich mit Split machen?
Split(Expression As String, [Delimiter As String = “ “], [Limit As Long = -1], [Compare As VbCompareMethod = vbBinaryCompare]) As Array()
Zerlegt einen String zu einem eindimensionalen Array. Mit Delimiter kann eine Zeichenkette angegeben werden, die als Trennzeichen verwendet wird. Ob dabei zwischen Groß/Kleinschreibung unterschieden wird, hängt von Compare ab, wo ein Wert aus der Aufzählung VbCompareMethod möglich ist. Mit Limit kann angegeben werden, dass nach einer maximalen Zahl von Treffern des Trennzeichens nicht weiter zerlegt werden soll, der letzte String des zurückgegebenen Arrays enthält dann möglicherweise noch weitere Treffer von Delimiter.
Sub zerlege()
Dim Wort() As String
Dim i As Integer
Wort = Split(“Das VBA Tutorial”)
For i = 0 To 2
Debug.Print Wort(i)
Next
End Sub
FizzBuzz in VBA
The FizzBuzz problem:
* Write a program that prints each number from 1 to 100 on a new line.
* If the number is number multiple of 3 print Fizz instead of the number.
* If the number is number multiple of 5, print Buzz instead of the number.
* If the number is number multiple 3 and 5, print FizzBuzz instead of the numbe
Sub fizbuzz()
For i = 1 To 101
If i Mod 5 = 0 And i Mod 3 = 0 Then
Debug.Print (i & “ Fiz Buzz!”)
ElseIf i Mod 3 = 0 Then
Debug.Print (i & “ Fizz”)
ElseIf i Mod 5 = 0 Then
Debug.Print (i & “ Buzz”)
End If
Next i
End Sub
Was ist Option Base?
Option Base is used to declare the default lower bound of array elements. It is declared at module level and is valid only for the current module.
By default (and thus if no Option Base is specified), the Base is 0. Which means that the first element of any array declared in the module has an index of 0.
If Option Base 1 is specified, the first array element has the index 1
Was macht LBound?
LBound
LBound(SourceArray(), [Dimension As Long = 1]) As Long
Gibt den kleinsten erlaubten Indexwert für die Dimension eines Arrays zurück.
Function Untergrenze()
Dim Zeichen(65 To 90)
Untergrenze = LBound(Zeichen) ‘ Gibt 65 zurück
End Function
Explain how can you pass arguments to VBA functions?
ByVal: When argument is passed by Value, then it means that only value is passed to the procedure, and any changes that are made to the argument inside the procedure will be lost when the procedure is exited
ByRef: When the argument is passed by reference than the actual address of the argument is passed to the procedure. Any changes that are made to the argument inside the procedure will be recalled when the procedure is exited