VBA_01 Flashcards

(28 cards)

1
Q

Alle Zellen aus A in graue Linien

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Was kann ich mit UBound machen?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Wie kann ich Text austauschen?

A

Sub ZeichenketteErsetzen2()

‘Variable definieren

Dim Text As String

‘Wert zuweisen

Text = “Ich liebe Pizza”

‘Zeichen ersetzen

MsgBox Replace(Text,” “,”-“)

End Sub

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Wie kann ich einen ausgewählten Bereich grau einfärben?

A

Dim rng As Range
Set rng = Selection

rng.Interior.Color = RGB(100, 100, 100)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to fasten the VBA macro?

A

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.

  1. Declare the variables and avoid using ‘Variant’ Data Type.
  2. Turn Off Screen Updating
  3. Turn Off Automatic Calculations
  4. Disable Events
  5. Use With Statement
  6. Use vbNullString instead of “”.
  7. Release memory objects at the end of the procedure
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Die Zahlen in C1 bis E12 sollen alle durch 3 geteilt werden.

A

Dim C As Range

For Each C In Range(“C1:E12”)

C = C / 3
Next C

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Wie erzeuge ich eine Message Box?

A

MsgBox (“kjdkfk”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Was kann ich mit Join machen?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to assign values to an array?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Letzte Reihe

A

Dim lastRow As Integer
lastRow = Cells(Rows.Count, 1).End(xlUp).Row

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Wie kann ich herkömmliche und Ganzzahldivision machen?

A

‘Herkömmliche Division

Range(“C1”).Value = 15 / 6

‘Ganzzahldivision

Range(“C2”).Value = 15 \ 6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Mention the difference between the Subroutines and Functions in VBA?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the code to find a last used Row in a column or last used Column of a row?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Kopiere von C4:C6 in Tabelle 2

A

Range(“C4:C6”).Copy Sheets(“Tabelle2”).Range(“C4”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Wie kann ich eine Inputbox erstellen?

A

Dim eingabe As String

‘InputBox mit eigenem Dialogfeld

eingabe = inputbox(“Bitte geben Sie etwas ein”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Wie kann ich ein bei einem Array die Größe neu anpassen?

A

‘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)

17
Q

Wie kann ich nach einem Begriff suchen?

A

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

18
Q

Wenn in dem Bereich C1 bis E12 eine 1 steht, soll die RGB-Farbe 200, 200, 200 eingefügt werden

A

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

19
Q

Die Adresse eines speziellen Strings (varLookFor) etc. finden

A

strAddress = Cells.Find(What:=varLookFor, LookAt:=xlWhole).Address

20
Q

Was kann ich mit Split machen?

A

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

21
Q

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

A

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

22
Q

Was ist Option Base?

A

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

23
Q

Was macht LBound?

A

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

24
Q

Explain how can you pass arguments to VBA functions?

A

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

25
Explain what is COM (Component Object Model) objects in VBA?
COM objects are usually .dll files, and are compiled executable programs.
26
Die Kanten mit einer With-Anweisung verändern
**'Alle Einstellungen mit einer With-Anweisung** Sub Beispiel2() **With** Range("B3:F10").Borders .LineStyle = xlDash .Color = vbGreen .Weight = xlMedium **End With** End Sub
27
Explain what is the meaning of “Option Explicit”? Where it should be used?
“Option Explicit” makes the declaration of variables mandatory. Line explicit function makes the compiler to determine all the variables that are not declared by the dim statement. This command diminishes the problem of type errors significantly. It is used in VBA because it deals with information rich application in which type errors are common. Before starting any, sub-procedures it can be used inside a module.
28
How to run macros automatically while opening Workbook in Excel VBA?
Einfügen in "DieseArbeitsmappe" ## Footnote Private Sub Workbook\_Open() MsgBox "Workbook has Opened Successfully.”, vbInformation End Sub