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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
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
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
Range("B" & y & ":AG" & y).ClearContents
 

xld

Banned
Joined
Feb 8, 2003
Messages
5,378
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
 

StefanRSA

New Member
Joined
May 26, 2008
Messages
24

ADVERTISEMENT

:biggrin: Thanks again, sorry I have not studied VB6, am trying to teach myself as I go along.

I appreciate your help very much.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,729
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
or:
Code:
cells(y,2).resize(,32).ClearContents
 

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
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
 

Watch MrExcel Video

Forum statistics

Threads
1,130,048
Messages
5,639,769
Members
417,111
Latest member
buyukbang

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
Top