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

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

DonkeyOte

MrExcel MVP
Joined
Sep 6, 2002
Messages
9,123
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
 

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
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...
 

schielrn

Well-known Member
Joined
Apr 4, 2007
Messages
6,939
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.
 

facethegod

Well-known Member
Joined
Aug 1, 2006
Messages
767

ADVERTISEMENT

Try.......

Code:
Sub tst()

Cells(9, 2).End(xlDown).Offset(1, 0) = "Employee"

End Sub


HTH
 

StefanRSA

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

Thanks for so many responses!

Really helpfull!!

PROBLEM SOLVED :biggrin:

Thank you again...
 

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
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:

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,730
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Just a thought:
Code:
columns(2).find(what:="", after:=cells(9,2), searchdirection:=xlnext).value = Employee
but: assumes the column is not full! :)
 

Watch MrExcel Video

Forum statistics

Threads
1,130,118
Messages
5,640,216
Members
417,131
Latest member
Seanr19871

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