How to search in a column by value =< 200 (run command)?

Asked

Viewed 2,071 times

1

I intend to search row by row within the column P:P a number less than or equal to 200 and execute a command. If more than 200 execute another. The values are defined as 0.0.

If .Value =< 200 Then

ActiveCell.FormulaR1C1 = "=RC[-5]*1.69*(1+30%)"
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
Range("U2").AutoFill Destination:=Range("U2:U" & Lastrow)

ElseIf .Value > 200 Then

ActiveCell.FormulaR1C1 = "=RC[-5]*1.69*(1+40%)"
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
Range("U2").AutoFill Destination:=Range("U2:U" & Lastrow)

End If

REPLY:

Sub teste()
Dim rng As Range
Set rng = Range("P1:P300") 'Se colocar P:P vai até à última linha

For Each Row In rng.Rows

If Row.Value < "200" Then

Range("U2").Select
ActiveCell.FormulaR1C1 = "=RC[-5]*1.69*(1+30%)"
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
Range("U2").AutoFill Destination:=Range("U2:U" & Lastrow)
Selection.NumberFormat = "0"

ElseIf Row.Value >= "200" Then

Range("U2").Select
ActiveCell.FormulaR1C1 = "=RC[-5]*1.69*(1+40%)"
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
Range("U2").AutoFill Destination:=Range("U2:U" & Lastrow)
Selection.NumberFormat = "0"

End for

End If

Next Row

End Sub
  • What is the doubt?

  • I’m not sure how to assign . value to the @bfavaretto column

  • How do I search row by row within column and compare values?

1 answer

0


To make a loop in a column can be done as follows:

Dim rng as Range

Set rng = Selection ' ou Range("A1:A30") ou Range("P:P") no seu caso...

For Each Row In rng.Rows

 ' Seu código aqui...
 ' If row.value >= 200 Then 
 ' [...]

Next Row

Care with loop very large, depending on the performance. I think it would be better to know which line has data, so that you do not need to run the For Each by all Excel lines.

Another option would be to use as follows within the For Each, to check if there is any value in the cell:

For Each Row In rng.Rows

 If Row.value = "" then

   Exit Sub ' ou _Function_ se for uma função

 End If

Rebound that if you have any empty value between the values the function stops at this point, you should look for another way to test the cell not to use unnecessary resource.

I hope I’ve helped!

  • Thank you for your comment @Evert!

  • I can’t run the code, Excel freezes!

  • How do I assign the reading to the last line on set rng @Evert?

  • To assign the whole column use the example 'range ("P:P")'. Vc is placing the code between 'Sub NAME()' and 'End Sub'?

  • So it worked? If yes mark as valid answer. About your code, I saw that you updated your question... in your code it always takes the same cell "U2" to put your formula... if that’s what you really want, I think you found your answer. If it is not and still has some open point, I suggest posting new question and leave this for future reference. Hug and good luck!

  • Thank you @Evert!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.