Conditional running of an element embedded in a macro

G

Guest

Hi all
I would like to find out how can I add a condition to a macro so that certain embedded functional elements will run only when the condiion is met. e.g. There is a loop built into a macro, which applies only when cell Z5 = "57", "77", or "A202". Otherwise, the macro should just skip the loop and continue. The loop itself is extracted below. Could you kindly help and add on the condition? Thanks
J.C

Dim RowNdx As Lon
Dim LastRow As Lon

LastRow = ActiveSheet.Cells(Rows.Count, "Y").End(xlUp).Ro
For RowNdx = 1 To LastRo
If Cells(RowNdx, "Y").Value = "B"
And IsNumeric(Cells(RowNdx, "D").Value) The
Cells(RowNdx, "D").NumberFormat = "$#,##0
Els
Cells(RowNdx, "D").NumberFormat = "#,##0
End I

Next RowNdx
 
F

Frank Kabel

Hi
try
Dim RowNdx As Long
Dim LastRow As Long
Dim test_rng as range
set test_rng = Range("Z5")

if test_rng.value =57 or test_rng.value =77 or test_rng.value ="A202"
then
LastRow = ActiveSheet.Cells(Rows.Count, "Y").End(xlUp).Row
For RowNdx = 1 To LastRow
If Cells(RowNdx, "Y").Value = "B" _
And IsNumeric(Cells(RowNdx, "D").Value) Then
Cells(RowNdx, "D").NumberFormat = "$#,##0"
Else
Cells(RowNdx, "D").NumberFormat = "#,##0"
End If
Next RowNdx
end if
 
F

Frank Kabel

Hi
$%& linebreak. Just put the word 'Then' from next line at the end of
this line
 
G

Guest

It's fixed and running smoothly
Thanks again for your help, Frank
J.C

----- Frank Kabel wrote: ----

H
$%& linebreak. Just put the word 'Then' from next line at the end o
this lin

-
Regard
Frank Kabe
Frankfurt, German

J.C. wrote
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top