Hi
I'm making a script that first searches for a person in a sheet and then copies some information about the person to another sheet.
Some of the people are listed, on the sheet that is searched, three times, which is why I search for information about the persons three times. If the person is listed more than once I want to copy all of the information from different cells to only one cell on the destination sheet (the "Forhandlingsenhed U+H sorteret" sheet).
Now, the first search works fine and the information is copied. But it seems that the script does the two other searches and then "forgets" to copy the information found in the second and third search. Why is that?
I don't understand why the string "reason" only contains information from the first search when it is stored.
My code is as follows:
[vba]
Sub indsaet_begrundelser_og_moduler()
'
' indsaet_begrundelser_og_moduler Makro
'
Dim search1 As range
Dim search2 As range
Dim search3 As range
Dim int_person As Integer
Dim name As String
Dim reason As String
'Turns off screen updating so that the Macro runs faster
Application.ScreenUpdating = False
'Ensures that the statusbar is viewable
Application.DisplayStatusBar = True
Application.StatusBar = _
"Begrundelserne kopieres... For at få alle begrundelserne med søges der efter begrundelser for hver person tre gange..."
int_person = 2
Do
Sheets("Forhandlingsenhed U+H sorteret").Select
name = range("A" & int_person) & " " & range("B" & int_person)
Sheets("Begrundelser").Select
'First search
Set search1 = Cells.Find(What:=name, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If search1 Is Nothing Then 'Or Not Err.Number = 0
reason = "Ingen begrundelse"
GoTo NextPerson:
Else
reason = _
Sheets("Begrundelser").range("G" & search1.Row).Value
MsgBox "Reason one = " & reason
End If
'Second search
Set search2 = Cells.FindNext(After:=ActiveCell)
If search2.Address = search1.Address Then
GoTo NextPerson:
Else
'Sheets("Forhandlingsenhed U+H sorteret").range("Z" & int_person).Value
reason = "Begrundelse et: " & _
Sheets("Begrundelser").range("G" & search1.Row).Value & vbNewLine & "Begrundelse to: " & _
Sheets("Begrundelser").range("G" & search2.Row).Value
MsgBox "Reason two = " & reason
End If
'Third search
Set search3 = Cells.FindNext(After:=ActiveCell)
If search3.Address = search1.Address Or search3.Address = search2.Address Then
GoTo NextPerson:
Else
'Sheets("Forhandlingsenhed U+H sorteret").range("Z" & int_person).Value
reason = _
"Begrundelse et: " & Sheets("Begrundelser").range("G" & search1.Row).Value & vbNewLine & _
"Begrundelse to: " & Sheets("Begrundelser").range("G" & search2.Row).Value & vbNewLine & _
"Begrundelse tre: " & Sheets("Begrundelser").range("G" & search3.Row).Value
MsgBox "Reason three = " & reason
End If
NextPerson:
Sheets("Forhandlingsenhed U+H sorteret").range("Z" & int_person).Value = reason
int_person = int_person + 1
Loop Until int_person = Sheets("Forhandlingsenhed U+H sorteret").UsedRange.Rows.Count + 1
'Gives Excel control of the statusbar again
Application.StatusBar = False
'Turns screen updating back on
Application.ScreenUpdating = True
Sheets("Forhandlingsenhed U+H sorteret").Select
End Sub
[/vba]
The script does not show the messageboxes with reason two or three, but it shows the messagebox containing reason one.
Now, I hope you can figure it out because I can't see what I'm doing wrong.