Tricky Blanks

StefanRSA

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

Im trying to go through a list of names, find the blank one, then add a name into it. I tried using a FOR loop but it keeps adding for every blank all the way down the list, not just the first one. :mad:

Any advice on how to exit the FOR loop when the IF statement is TRUE? Or perhaps a different method to achieve the same end?

For x = 10 To 209
If Cells(x, 2) = "" Then
Cells(x, 2).Value = Employee
End If
Next

Many thanks in advance,

Stefan
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
after

cells(x,2).value = Employee

Exit Sub (or Exit For if you want to do actions outside the loop)

alternative you can find the first blank and adjust that... horses for courses
 
Upvote 0
to Exit a Loop use Exit For

For x = 10 To 209
If Cells(x, 2) = "" Then
Cells(x, 2).Value = Employee
Exit For
End If
Next

But, the same thing can be done without a loop...

Try this, it simulates pressing CTRL + DOWN to find the last cell before the first blank, then goes down 1 cell (to the first blank)...

Cells(10,2).End(xldown).Offset(1,0).Value = Employee

Hope that helps...
 
Upvote 0
If you only want the first one try this:

Code:
For x = 10 To 209
If Cells(x, 2) = "" Then
Cells(x, 2).Value = Employee
Exit For
End If
Next
You can also use exit sub to exit the procedure. Hope that helps.
 
Upvote 0
By Using (9,2), FaceTheGood made me think about why use 9, when the OP started with row 10...

My 1 line solution
Cells(10,2).End(xldown).Offset(1,0).Value = Employee
will give odd results if the first blank happens to be in row 10 or 11...

FaceTheGood tried to accomodate for that by starting at row 9, But it would have the same problem if the first blank row happened to be 10.

so that got me thinking...
This will work better...

Cells(10, 2).Resize(200).SpecialCells(xlCellTypeBlanks)(1).Value = Employee

However, it's not flawless either, it will fail if there are NO blanks in the range...

So need a few lines, but still not a loop...

Dim MyRange As Range
On Error Resume Next
Set MyRange = Cells(10, 2).Resize(200).SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If Not MyRange Is Nothing Then MyRange(1).Value = Employee
 
Last edited:
Upvote 0
Just a thought:
Code:
columns(2).find(what:="", after:=cells(9,2), searchdirection:=xlnext).value = Employee
but: assumes the column is not full! :)
 
Upvote 0

Forum statistics

Threads
1,213,506
Messages
6,114,025
Members
448,543
Latest member
MartinLarkin

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