Can someone review my loop?

Linda21

New Member
Joined
Aug 2, 2010
Messages
38
This code works EXCEPT when the the first row of data is what I am trying to match. Thank you!!


Sub fLocateClientInSourceData()

SourceDataRow = 5 'row to begin search on SourceData tab for client name match
DetailDataRow = 10
DetailClient = Detail.Cells(1, 1).Value 'location of client name in Detail tab
'MsgBox (DetailClient & " DetailClient")
SourceClient = SourceData.Cells(SourceDataRow, 1) 'location of cells to search in SourceData
'msgBox (SourceClient & " SourceClient")
SourceManagerName = SourceData.Cells(SourceDataRow, 2) 'location of cells to search for 'Total'
DetailManagerName = Detail.Cells(DetailDataRow, 1) 'location of cells in Detail to place Source Manager name

Do Until DetailClient = SourceClient ' loop thru until Detail client name = SourceData client name
SourceDataRow = SourceDataRow + 1 'add row until match is found
SourceClient = SourceData.Cells(SourceDataRow, 1)

If DetailClient = SourceClient Then
Do While DetailClient = SourceClient 'match occurred during Do Until step above

DetailManagerName = Detail.Cells(DetailDataRow, 1)
SourceManagerName = SourceData.Cells(SourceDataRow, 2)

DetailManagerName = SourceManagerName

If SourceManagerName <> "zzTotal" Then 'update data for all managers
Detail.Cells(DetailDataRow, 1) = DetailManagerName
Detail.Cells(DetailDataRow + 1, 2) = SourceData.Cells(SourceDataRow, 3) / 1000 'update Detail tab Shares column
ElseIf SourceManagerName = "zzTotal" Then 'update data for total row
Detail.Cells(8, 2) = SourceData.Cells(SourceDataRow, 3) / 1000 'update Detail tab Shares column
End If

DetailDataRow = DetailDataRow + 3 'move to next row in Detail tab for pasting data
SourceDataRow = SourceDataRow + 1 'move to next row in SourceData tabfor retrieving data
SourceClient = SourceData.Cells(SourceDataRow, 1)

Loop
End If

If IsEmpty(SourceData.Cells(SourceDataRow, 1)) Then Exit Do

Loop


End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Move this:
Code:
        SourceDataRow = SourceDataRow + 1    'add row until match is found
... to the end of the loop. You're incrementing it before testing the first row.
 
Upvote 0

Forum statistics

Threads
1,224,543
Messages
6,179,429
Members
452,914
Latest member
echoix

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top