Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Laaibah Malik   on Feb 28 In MS Office Category.

  
Question Answered By: Jake Williams   on Feb 28

: I used what you wrote:

But, did you understand it.


: This works except, if there is more than one value in d2:d15
: (ie, 4 on two records) it copies it twice.

Well, that's what you told it to do. The loop checks each
value in the range  d2:d15. To stop on duplicate values, you'll
need to find a way to test for duplicates. The first step
would be to clearly define your criteria.

Are two copies really a problem? Each copy  on April should
produce the same result.


: I tried putting "exit sub" after Destination... line
: but then get nothing..

You mean like this?

With Worksheets("DMH")

For Each rngCell In .Range("d2:d15")

If Int(rngCell.Value) = rngCell.Value _
And rngCell.Value < 13 _
And rngCell.Value > 0 Then _
.Range("A2:S13", .Range("A2:S13").End(xlToRight)).Copy _
Destination:=.Range("A" & rngCell.Value * 15)

Exit Sub

Next rngCell

End With

Now the algorithm only checks cell  d2, then exits the
sub. Cells d3:d15 are never checked. There is no logic in there
to check for duplicate values.

I think you were trying to do this, but it would stop after
the first valid value is reached and I am not clear  whether that
is what you really want.

For Each rngCell In .Range("d2:d15")

If Int(rngCell.Value) = rngCell.Value _
And rngCell.Value < 13 _
And rngCell.Value > 0 Then

.Range("A2:S13", .Range("A2:S13").End(xlToRight)).Copy _
Destination:=.Range("A" & rngCell.Value * 15)

Exit Sub

End If

Next rngCell


Your next step should not be programming anything. It should
be to articulate what you want to have happen when a duplicate
value is hit.

Also, I spotted an error in my code. To match your original
algorithm, Destination should be set to this. This still assumes
that originally the destination for May in your code was
mistakenly set to that of April (A63 instead of A78).

Destination:=.Range("A" & rngCell.Value * 15 + 3)

Share: 

 

This Question has 6 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on If statement in a macro ? Or get search suggestion and latest updates.


Tagged: