Excel VBA: SpaltenVariablen in range

Hallo experten,
ich benötige etwas fine Tuning. Ich will meine Spaltenverweise mit variablen Belegn, weil ab und zu sich meine Spalten ändern. Und ich nicht den gesamt Code ändern will.

.Value = Application.WorksheetFunction. \_
 SumIf(.Range("M:M"), .Cells(j, 18), .Range("N:N"))

Das M:M und das N:N
wenn ich dafür eine Lösung habe, dann natürlich auch für

wksEinzelR.Range("O:R").ClearContents

und

 wksEinzelR.Range("M:M").AdvancedFilter \_
 Action:=xlFilterCopy, CopyToRange:=wksEinzelR.Range("r1"), Unique:=True

Muss ich das mit Cells machen, dass ich dann die anzahl der Zeilen benötige?

Danke und Grüße
Winter


strSpalte1 = „O“
strSpalte2 = „R“

wksEinzelR.Range( strSpalte1 & „:“ & strSpalte2 ).ClearContents

[Bei dieser Antwort wurde das Vollzitat nachträglich automatisiert entfernt]

Hallo SAM
Danke für deine Lösung.

strSpalte1 = „O“
strSpalte2 = „R“

wksEinzelR.Range( strSpalte1 & „:“ & strSpalte2
).ClearContents

Geht das auch mit Zahlen? Dann kann ich nur die Start Zeile angeben:

 intSpalte1 = 15
 wksEinzelR.Range( intSpalte1 & ":" & intSpalte1+3).ClearContent

So natürlich nicht!
Aber so in der Art?
Ich kann natürlich mit

 range(Cells (65000,intSpalte1) , cells(1, intSpalte1 +3)) 

arbeiten, aber da kommt mir unelegant vor.

Grüße
Winter

Hallo,
ich habe mir mal 2 kleine Funktionen geschrieben die
eine Zahl in Buchstaben umrechnet:


Function Index2Cell(IntIndex) As String
’ Umrechnung eines Indexwerts in eine Zellenausdruck
’ Beispiel: Index2Cell(30) -> „AD“
Dim intRest As Integer

If IntIndex > 26 Then
intRest = IntIndex Mod 26
If intRest = 0 Then
intRest = 26
Index2Cell = Chr((Int(IntIndex / 26) - 1) + 64) & Chr((intRest) + 64)
Else
Index2Cell = Chr((Int(IntIndex / 26)) + 64) & Chr((intRest) + 64)
End If
Else
Index2Cell = Chr(IntIndex + 64)
End If

End Function


Und andersrum Buchstabe in Zahl

Function Cell2Index(ByVal strCelle As String) As Integer
’ Umrechnung eines Zellenausdruck in einen Indexwerts
’ Beispiel: Cell2Index(„R“) -> 18

strCelle = Trim(UCase(strCelle))

If Len(strCelle) = 1 Then
Cell2Index = Asc(strCelle) - 64
Else
Cell2Index = (Asc(Mid(strCelle, 1, 1)) - 64) * 26 + Asc(Mid(strCelle, 2, 1)) - 64
End If

End Function

Spaltennummer Spaltenname

Geht das auch mit Zahlen? Dann kann ich nur die Start Zeile
angeben:
intSpalte1 = 15
wksEinzelR.Range( intSpalte1 & „:“ &
intSpalte1+3).ClearContent
So natürlich nicht!
Aber so in der Art?

Hallo Winter,

Sub tt()
Dim lngSpalte As Long
lngSpalte = 15
Columns(lngSpalte).Resize(, 3).Select
With Application.WorksheetFunction
 MsgBox .SumIf(Columns(lngSpalte), "Huhu", Columns(lngSpalte + 1))
End With
MsgBox SpaNr("AA")
MsgBox SpaName(27)
End Sub

Zum Umrechnen:

Function SpaNr(ByVal strS As String) As Long
SpaNr = Columns(strS).Column
End Function

Function SpaName(ByVal lngS As Long) As String
SpaName = Replace(Cells(1, lngS).Address(0, 0), 1, „“)
End Function

Gruß
Reinhard