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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
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,214,430
Messages
6,119,443
Members
448,898
Latest member
drewmorgan128

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