Ranges and Variables

StefanRSA

New Member
Joined
May 26, 2008
Messages
24
Hi Guys,

Must be getting annoyed with all my N00b questions...


Im trying to use a bit of code to clear a set range of cells. However, Im not sure of how the syntax should be. I have removed where the row references were and replaced them with a variable - y.

I have tried the following but no success:

For y = 10 To 209
If Cells(y, 2) = Employee Then
Range("B(y):AG(y)").ClearContents
Exit For
End If
Next

Any ideas on the syntax?

Many thanks in advance for your consideration.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi Stefan

That would be:

Code:
For y = 10 To 209
If Cells(y, 2) = Employee Then
Range("B" & y & ":AG" &  y).ClearContents
Exit For
End If
Next
 
Upvote 0
Code:
For y = 10 To 209
If Cells(y, 2) = Employee Then
Range(Cells(y,"B"),Cells(y,"AG")).ClearContents
Exit For
End If
Next
 
Upvote 0
:biggrin: Thanks again, sorry I have not studied VB6, am trying to teach myself as I go along.

I appreciate your help very much.
 
Upvote 0
or:
Code:
cells(y,2).resize(,32).ClearContents
 
Upvote 0
Having read further about what it was you were actually attempting to do, I think the following also achieves what you want but much more efficiently:

Code:
Dim r As Range
'...
 
With Range("B10:B209")
  Set r = .Find(What:=Employee,After:=.Cells(.Rows.Count,1),SearchDirection:=xlNext)
  If Not r Is Nothing Then Range(r,"AG"&r.Row).ClearContents
End With
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,664
Members
448,976
Latest member
sweeberry

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