Clever-Excel-Forum

Normale Version: Schleife in Visual Basic mit Target.Address
Du siehst gerade eine vereinfachte Darstellung unserer Inhalte. Normale Ansicht mit richtiger Formatierung.
Hallo liebe Community,

ich bin eher noch Anfänger in Visual Basic und bin gerade dabei, die ein oder anderen Makros in der Arbeit umzuschreiben und zu vereinfachen.

Ich möchte, wenn man in ein Feld einfach reinklickt, dass in einem anderen Feld die Ziffer um eins erhöht wird. Ohne zurücksetzen oder sonst was.

Was war mein Ansatz bisher?

Code:
Private Sub Worksheet_SelectionChange(ByVal Target as Range)

If Target.Address = "$A$3" Then
Range ("E3") = Range("E3") + 1
End If

End Sub



So...das kann ich jetzt für alle Zellen machen in die ich einfach reinklicken möchte. Ich dachte mir, ich machs mit Schleifen, deutlich einfacher und weniger Code.

Mein Ansatz:



Code:
Sub EintragenZahlen()
Dim intRow As Integer
For intRow = 3 to 10

If Target.Address = Cells(intRow, 1) Then

Cells(intRow, 5) = Cells(intRow, 5) + 1


End If

Next intRow
End Sub

Jedoch funktioniert das einfach nicht, ich bringe es leider nicht zusammen. Ich hoffe es ist verständlich, was ich machen möchte.

Vielen Dank schonmal für eure Hilfe!

Liebe Grüße  Idea
Hi

Als Schleife so. Bei dir fehlt nur einmal .Address
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim intRow As Integer

For intRow = 3 To 10
  If Target.Address = Cells(intRow, 1).Address Then Cells(intRow, 5) = Cells(intRow, 5) + 1
Next intRow
End Sub

Gruß Elex
(03.12.2019, 10:26)Elex schrieb: [ -> ]Hi

Als Schleife so. Bei dir fehlt nur einmal .Address
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim intRow As Integer

For intRow = 3 To 10
  If Target.Address = Cells(intRow, 1).Address Then Cells(intRow, 5) = Cells(intRow, 5) + 1
Next intRow
End Sub

Gruß Elex


Funktioniert einwandfrei, vielen Dank.

Was ich sehr interessant finde ist, dass wenn ich alles ab Cells(intRow,5) in die nächste Zeile schmeiße, er mir einen Fehler beim Kompilieren gibt...ich habe so lange herumprobiert und vielleicht lags dann auch teilweise an dem. Na toll...

Liebe Grüße
Instant
Du meinst so. Dann muss ein End If dazu.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim intRow As Integer

For intRow = 3 To 10
  If Target.Address = Cells(intRow, 1).Address Then
    Cells(intRow, 5) = Cells(intRow, 5) + 1
  End If
Next intRow
End Sub
(03.12.2019, 10:54)Elex schrieb: [ -> ]Du meinst so. Dann muss ein End If dazu.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim intRow As Integer

For intRow = 3 To 10
  If Target.Address = Cells(intRow, 1).Address Then
    Cells(intRow, 5) = Cells(intRow, 5) + 1
  End If
Next intRow
End Sub

Aaaja, stimmt. Das hab ich schon im Original-Code von mir vergessen, dann ists klar.

Danke dir :))
Hallo,

... und ich bin felsenfest der Meinung, daß das ein klassischer Fall für "Select Case" ist.