[EXCEL-VBA] Range-Objekt

Gepostet am: Jun 15, 2011 6:33:38 PM

Beispiel-Arbeitsmappe siehe Anhang

Excel-Funktionen

mit VBA

Umgang mit dem Range-Object unter VBA

Public Function AnzahlSpalten(myRange As range) As Integer     AnzahlSpalten = myRange.Columns.Count End FunctionPublic Function AnzahlZeilen(myRange As range) As Integer     AnzahlZeilen = myRange.Rows.Count End FunctionPublic Function AnzahlZellen(myRange As range) As Integer     AnzahlZellen = myRange.Cells.Count End FunctionPublic Function Wert_aus_Range(myRange As range, Spalte As Integer, Zeile As Integer) As Variant     Wert_aus_Range = myRange.Cells(Zeile, Spalte).Value End Function

Ermitteln von relativen Positionen innerhalb des Bereiches

Public Function RowRelative(SuchBereich As range, mZelle As Variant) As VariantDim mAddress As StringIf TypeName(mZelle) = "String" Then     mAddress = range(mZelle).Address ElseIf TypeName(mZelle) = "Range" Then     mAddress = mZelle.Address Else     RowRelative = "#Fehler"End IfDim Zeile As Integer Zeile = SuchBereich.Cells(1, 1).Row Dim myCell As range For Each myCell In SuchBereich     If myCell.Address = mAddress Then         RowRelative = myCell.Row - Zeile + 1         Exit Function     End IfNext RowRelative = 0End FunctionPublic Function ColumnRelative(SuchBereich As range, mZelle As Variant) As VariantDim mAddress As StringIf TypeName(mZelle) = "String" Then     mAddress = range(mZelle).Address ElseIf TypeName(mZelle) = "Range" Then     mAddress = mZelle.Address Else     ColumnRelative = "#Fehler"End IfDim Spalte As Integer Spalte = SuchBereich.Cells(1, 1).Column Dim myCell As range For Each myCell In SuchBereich     If myCell.Address = mAddress Then         ColumnRelative = myCell.Column - Spalte + 1         Exit Function     End IfNext ColumnRelative = 0End Function