Applying Macro to multiple rows, respectively

TuckerStevens

New Member
Joined
Jul 23, 2015
Messages
2
Hello, I'm new to macros - any help appreciated - I'm sure its quite simple.

My code:

Sub NameClear2()

If Range("M8") = 0 Then
Range("B8") = 0

Else
Range("B8") = Range("B8")

End If

End Sub

I want this same function to apply to M9:M200, keeping B9 with M9 and so on respectively until B200 with M200. I think I just need a loop, but don't know how to write it?

Thanks!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Code:
Sub NameClear2()
Dim i As Integer
i = 8
For i = 8 to 200
If Range("M"&i) = 0 Then
Range("B"&i)= 0
Else
Range("B"&i) = Range("B"&i)
End If
End Sub
 
Upvote 0
This gives me a compile error: For without next. What do you think? I copy pasted that directly...

Oops, you're right. I missed it.

Code:
Sub NameClear2()
Dim i As Integer
i = 8
For i = 8 to 200
If Range("M"&i) = 0 Then
Range("B"&i)= 0
Else
Range("B"&i) = Range("B"&i)
End If
[B][/B][COLOR="#FF0000"]Next[/COLOR]
End Sub

Add the "Next" after the "End If" if you haven't already.
 
Upvote 0
The title of your sub is: Sub NameClear2()

When you say: If Range("M8") = 0
Do you mean zero or do you mean empty?
 
Upvote 0

Forum statistics

Threads
1,223,970
Messages
6,175,707
Members
452,667
Latest member
vanessavalentino83

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top