Option Explicit
Sub wverweis()
Dim auswahl, rng1, rng2 As Range
Set auswahl = Rows(11).Find("tofind", LookIn:=xlValues, lookat:=xlWhole)
If Not auswahl Is Nothing Then
Range(Cells(11, 8), Cells(11, auswahl.Column - 1)).Select
Set rng2 = Selection
Else
MsgBox "nix gefunden"
'im Falle dass "nix gefunden" musst du die Sub verlassen (oder so)
'sonst knallt es in den Folgezeilen
Exit Sub
End If
Set rng1 = Range("F15")
Range("K40").Select
ActiveCell.Formula = "=HLOOKUP(" & rng1.Address & Chr(44) & rng2.Address & ",1,TRUE)"
Range("K40").Select
End Sub
Sub Betterwverweis()
Dim auswahl As Range 'nur so wird jede Variable auch ein Range-Object
Dim rng1 As Range
Dim rng2 As Range
Dim strFormula As String
strFormula = "=HLOOKUP(F15,XX,1,TRUE)"
Set auswahl = Rows(11).Find("tofind", LookIn:=xlValues, lookat:=xlWhole)
If Not auswahl Is Nothing Then
Set rng2 = Range(Cells(11, 8), Cells(11, auswahl.Column - 1))
Range("K40").Formula = Replace(strFormula, "XX", rng2.Address(0, 0))
Else
MsgBox "nix gefunden"
End If
End Sub
Sub Alternativ()
'was passiert mit WVERWEIS ????
'Wenn WVERWEIS das Suchkriterium nicht finden kann und "Bereich_Verweis" WAHR ist,
'wird der größte Wert verwendet, der kleiner als "Suchkriterium" ist.
Dim auswahl As Range, rng1 As Range
Set auswahl = Rows(11).Find("tofind", LookIn:=xlValues, lookat:=xlWhole)
If auswahl Is Nothing Then
Range("K40").ClearContents
Exit Sub
End If
Range("K40").Value = auswahl.Offset(, -1).Value
Set rng1 = Range(Cells(11, 8), Cells(11, auswahl.Column - 1)).Find(Range("F15").Value, LookIn:=xlValues, lookat:=xlWhole)
If Not rng1 Is Nothing Then Range("K40").Value = rng1.Value
End Sub
|