PDA

View Full Version : How to skip the cell?



alivenwell
10-01-2007, 07:43 AM
Hi, I am a newbie to Vba, and was hoping if anyone can help me on my loop that i made.

Right. My loop, starting with the first country it finds in this customer file, it searches for a country in a second file and copies a range of data and pastes it into the customer file. However, there are some countries that are not on the second file and I would like the macro to skip to the next country instead.

Here's my loop:


With sht1
colnum = 12
mainwb = ActiveWorkbook.Name
otherwb = Workbooks("Country.xls").Name
lastrow = LastRowWithData()
For x = 7 To lastrow
If Range("D" & x).Value <> "" Then
country = Trim(Range("D" & x).Value)
colnum = 12
For y = 1 To 16

Workbooks(otherwb).Activate
Cells.Find(country).Select
Range("C" & ActiveCell.Row, "I" & ActiveCell.Row).Copy

sht1.Activate
Cells(x, colnum).PasteSpecial xlPasteValues
Application.CutCopyMode = False
colnum = colnum + 7
Next
End If


Hope someone can help. Thanks

Bob Phillips
10-01-2007, 08:24 AM
This isn't tested, and won't work, because I cannot see why you had a loop on y, and never used the y indx, so I ditched it.



With sht1
colnum = 12
mainwb = ActiveWorkbook.Name
otherwb = Workbooks("Country.xls").Name
lastrow = LastRowWithData()
For x = 7 To lastrow
If Range("D" & x).Value <> "" Then
country = Trim(Range("D" & x).Value)
colnum = 12

With Workbooks(otherwb).Worksheets(1)
Set mpCell = .Cells.Find(country)
If Not mpCell Is Nothing Then
Range("C" & mpCell.Row, "I" & mpCell.Row).Copy
.Cells(9, colnum).PasteSpecial xlPasteValues
End If
End With
End If