Visual Basic for Applications (VBA)
Die Programmiersprache VBA ist eine Entwicklung von Microsoft und wird daher hauptsächlich im Zusammenhang mit Microsoft Software eingesetzt. VBA ist jedoch auch in vielen Programmen großer Softwarehersteller als Scriptsprache zur Makroprogrammierung verfügbar. Als Beispiel hierfür sind im Bereich Grafik-Software die Firma Corel oder im PLM-Bereich die Firmen Autodesk und Dassault Systemes zu nennen.
Die folgenden Code-Schnipsel können im Rahmen von MS Excel Macros eingebunden werden.
1.) Zeilen-Zählfunktion
| Function: |
count_rows |
| Typ: |
Function |
| Parameter: |
sWS_Name (String) Name des Tabellenblattes, in dem gezählt werden soll. |
|
iColumn (Integer) Spalte in der gezählt wird. |
|
iFirst_Row (Integer) Erste Zeile, in der mit dem zählen begonnen wird. |
|
iEmpty_Rows (Integer) Wie viele Leerzeilen dürfen in einer Spalte sein, ohne dass die Zählschleife abgebrochen wird. |
| Rückgabewert: |
Anzahl Zeilen in Tabellenblatt |
Function count_rows(sWS_Name As String, _
iColumn As Integer, _
iFirst_Row As Integer, _
iEmpty_Rows As Integer) As Integer
Dim oWS As Worksheet
Set oWS = Worksheets(sWS_Name)
Dim iX As Integer
Dim iY As Integer
Dim bEmpty As Boolean
iX = iFirst_Row
bEmpty = True
iEmpty_Rows = iEmpty_Rows + 1
Do While oWS.Cells(iX, iColumn).Value <> Empty Or bEmpty = True
If oWS.Cells(iX, iColumn).Value <> Empty Then
iX = iX + 1
Else
iY = iX
Do While (iX + iEmpty_Rows) > iY
If oWS.Cells(iY, iColumn).Value <> Empty Then
iY = iY + 1
iX = iY
bEmpty = True
iY = iX + iEmpty_Rows
Else
iY = iY + 1
bEmpty = False
End If
Loop
End If
Loop
count_rows = iX - 1
End Function
2.) String-Zählfunktion
| Function: |
Count_InStr |
| Typ: |
Function |
| Parameter: |
sInput (String) Der zu durchsuchende Text |
|
sSpliter (String) Der zu suchende Text |
| Rückgabewert: |
Wie oft kommt sSpliter in sInput vor. |
Function Count_InStr(sInput As String, _
sSpliter As String) As Integer
Count_InStr = 0
Do While InStr(sInput, sSpliter) > 0
sInput = Left(sInput, (Len(sInput) - InStr(sInput, sSpliter)))
Count_InStr = Count_InStr + 1
Loop
End Function
oder noch viel einfacher ...
Function Count_InStr2(sInput As String, _
sSpliter As String) As Integer
Dim aInput As Variant
aInput = Split(sInput, sSpliter)
Count_InStr2 = UBound(aInput)
End Function
|