Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Test a string and if it matches copy to another range, else insert

Status
Not open for further replies.

knifey

Technical User
Nov 14, 2006
180
GB
Hi,
Can anyone help a complete beginner? My loop below should search a named range for the last 5 charicters in each cell and if they match the last 5 charicters in each cell of the target range, copy the offset value 1 cell to the right from source to target. If they do not match then the source should be inserted at point x in the target range.
I keep getting a block if without end if error at the first if, can anyone advise?
Thanks,
Roy

This is my code:

Sub TestAndCopy()

Dim sourceRange As Range
Dim targetRange As Range
Dim x As Integer
Dim c As Integer
Dim numofRows As Integer
Set sourceRange = [Control_Print_Data_Status_List] 'source workbook
Set targetRange = [Target_Control_Print_Data_Status_List] 'created workbook
numofRows = sourceRange.Rows.Count
x = 51
For c = 1 To numofRows
If sourceRange(c).Right(Value, 5) = targetRange(x).Right(Value, 5) Then Do
sourceRange(c).Offset(0, 1).Value = targetRange(x).Offset(0, 1).Value
x = x + 1
Else
sourceRange(c).Value = targetRange(x).Value
sourceRange(c).Offset(0, 1).Value = targetRange(x).Offset(0, 1).Value
End If
Next c
End Sub
 
remove the "Do"

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top