Supportnet Computer
Planet of Tech

Supportnet / Forum / Tabellenkalkulation

Excel VBA Suche / Vergleich





Frage

Hi Ihr! Ich verstehe es nicht. Ich habe in einer Spalte einige Werte stehen, in einer anderen deutlich mehr. Jetzt möchte ich prüfen, ob Werte der kleineren Spalte auch in der großen vorkommen. Dazu habe ich folgenden Code gestrickt: [code] Sub test() For i = 2 To 3238 Cells(i, 6).Select For j = 2 To 327 If Cells(i, 5).Value = Cells(j, 2).Value Then Cells(i, 6) = "HR" End If Next Next End Sub [/code] Das funktioniert aber nicht.... Jemand eine Idee? Danke Euch! Mona

Antwort 1 von nighty

hi mona :-)

was hälst davon :-))

gruss nighty

Option Explicit
Sub vergleich()
Dim w1y, w2y, zaehler0 As Long
Dim suche1 As Range
w1y = Sheets(1).Range("B" & Rows.Count).End(xlUp).Row
w2y = Sheets(1).Range("E" & Rows.Count).End(xlUp).Row
ReDim excel1(w1y) As Variant
excel1() = Range(Cells(2, 2), Cells(w1y, 2))
For zaehler0 = 1 To w1y - 1
Set suche1 = Sheets(1).Range("E1:E" & w2y).Find(excel1(zaehler0, 1), Lookat:=xlWhole)
If Not suche1 Is Nothing Then
Sheets(1).Cells(suche1.Row, 6) = "HR"
End If
Next zaehler0
End Sub

Antwort 2 von nighty

hi mona :-)

das wäre dein korrigierte code,der aber ein wenig zu langsam ist :-))

gruss nighty

Option Explicit
Sub vergleich()
Dim i, j As Long
For i = 2 To 3238
For j = 2 To 327
If Cells(i, 5).Value <> "" And Cells(i, 5).Value = Cells(j, 2).Value Then
Cells(i, 6) = "HR"
End If
Next
Next
End Sub

Antwort 3 von MonasFrage

Danke Euch!!!

Jetzt weiß ich auch, warum es nicht klappt!

Teilweise sind noch Leerzeichen am Ende des Textes in den Zellen.

Gibt es eine Möglichkeit, diese letzten Leerzeichen automatisch zu entfernen?

Danke nochmal!

Antwort 4 von nighty

hi mona :-)

dann so :-)

gruss nighty

Option Explicit
Sub vergleich()
Dim w1y, w2y, zaehler0 As Long
Dim suche1 As Range
w1y = Sheets(1).Range("B" & Rows.Count).End(xlUp).Row
w2y = Sheets(1).Range("E" & Rows.Count).End(xlUp).Row
ReDim excel1(w1y) As Variant
excel1() = Range(Cells(2, 2), Cells(w1y, 2))
For zaehler0 = 1 To w1y - 1
Set suche1 = Sheets(1).Range("E1:E" & w2y).Find(excel1(zaehler0, 1), Lookat:=xlPart)
If Not suche1 Is Nothing Then
Sheets(1).Cells(suche1.Row, 6) = "HR"
End If
Next zaehler0
End Sub

Antwort 5 von nighty

hi mona :-)

leerzeichen muessen nicht unbedingt entfernt werden,die suchart ist ausschlaggebend :-))

gruss nighty

Antwort 6 von nighty

hi mona :-)

zur verdeutlichung

Lookat:=xlWhole

suchbegriff "ABC"
fund "ABC"

Lookat:=xlPart

suchbegriff "ABC"
fund "ABC "

gruss nighty