Solved

VBA Excel 2010 - First blank cell in a column

Posted on 2011-09-21
14
713 Views
Last Modified: 2012-05-12
Dear Experts,

I would like to know which is the row number of the first blank cell in column A, could you please advise how it should be in VBA?

thanks,
0
Comment
Question by:csehz
  • 6
  • 3
  • 3
  • +2
14 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
Comment Utility
Something like:
range("A:A").Find(what:="", lookat:=xlwhole).Row

Open in new window

0
 
LVL 31

Expert Comment

by:Rob Henson
Comment Utility
Depends what you mean by "first blank cell".

Do you mean the bottom of existing data? This would be ignoring rows between blocks of data/calculations inserted for appearance.

This will take the cursor to the bottom right of Used Range and then down 1 and then cone back to column A.

ActiveCell.SpecialCells(xlLastCell).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.End(xlToLeft).Select

Open in new window


Thanks
Rob H
0
 
LVL 1

Author Comment

by:csehz
Comment Utility
Rprya thanks, trying your solution I got an error message

Compile error: Invalid use of property

And in the code window the .row is marked
0
 
LVL 24

Accepted Solution

by:
StephenJR earned 500 total points
Comment Utility
This perhaps?
MsgBox Columns(1).SpecialCells(xlCellTypeBlanks)(1).Row

Open in new window

0
 
LVL 1

Author Comment

by:csehz
Comment Utility
Rob, yes the final target would be to determine with this the last used row, and I also have a method of this but it is doing the same like your code on the attached picture.

It is a tricky file which I am not sure how works technically and your code also jumped under of the last EoF. But you can see the up of it there are a lot of "blank" cell, I mean probably for the Excel it is not blank but for me yes
Last-row.jpg
0
 
LVL 1

Author Comment

by:csehz
Comment Utility
Stephen thanks your code gaves result 789 so not 4980, and this 789 is my target so seems working
0
 
LVL 31

Expert Comment

by:Rob Henson
Comment Utility
Try this:

ActiveCell.SpecialCells(xlLastCell).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.End(xlToLeft).Select
ActiveCell.Offset(-1, 0).Range("A1").Select
If Activecell.Value = "EOF" Then 
Selection.End(xlUp).Select
Else
ActiveCell.Offset(1, 0).Range("A1").Select
End If

Open in new window


This goes to the bottom as before goes up one and checks the cell value, if it is "EOF" it then carries on up otherwise it goes back down one.

Thanks
Rob H
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 31

Expert Comment

by:Rob Henson
Comment Utility
Alternatively without the hopping about:

ActiveCell.SpecialCells(xlLastCell).Select
R = ActiveCell.Row
Cells(R ,1).Select
If Activecell.Value = "EOF" Then Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select

Thanks
Rob H
0
 
LVL 12

Expert Comment

by:kgerb
Comment Utility
Unless I'm missing something, I'm pretty sure this can be done with a single line.  Maybe I'm not understanding the question fully.

Kyle
Cells(Rows.Count, 1).End(xlUp).Offset(1).Row

Open in new window

0
 
LVL 1

Author Comment

by:csehz
Comment Utility
Rob thanks the efforts, but somehow both your codes jumped under the EOF line. Since then I got to know that those are from an imported text from menu Data/From Text. So maybe the Excel does not consider those EOF cells like value in the cell.

Kgerb, tried also your code but got an error message - Run-time error 438, Object does not support this property or method.

But seems having the solution from StephenJR this works even on this tricky file so will accept it

MsgBox Columns(1).SpecialCells(xlCellTypeBlanks)(1).Row

thanks,
0
 
LVL 12

Expert Comment

by:kgerb
Comment Utility
Ok, whatever, but I think for future reference you should understand how to find the last cell in a given column.  It's easy.  To do it manually, scroll down to the very last row in the worksheet 1048576.  Now hold ctrl down and push the up arrow.  It will take you up to the next cell with a value in it.  The same can be done in VBA with the syntax I provided.  If you would like it to be reported in a message box then use the following.  It will work with any workbook, "tricky" or not:).  good luck, happy coding:).

Kyle
Sub GetLastRowInColA()
MsgBox "Last row = " & Cells(Rows.Count, 1).End(xlUp).Offset(1).Row
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:csehz
Comment Utility
Kyle thanks, but in this "tricky" file your code also brings 4980 :-) Like Rob's two codes, you can see on the attached picture at before comment that this is the first row under the EoF lines.

So far for me only StephenJR's code brought the 789 which is visually the true for a user.

In the practice I used to use such line for the target

ActiveSheet.UsedRange.row - 1 + ActiveSheet.UsedRange.Rows.Count

which worked so far always, except this file because also this brings 4980 :-)
0
 
LVL 12

Expert Comment

by:kgerb
Comment Utility
Ok, sounds good.  I just wanted you to be aware of the method.  Not good for every case but still very useful.  I use it constantly.  Good luck and take care.

Kyle
0
 
LVL 1

Author Comment

by:csehz
Comment Utility
Kyle yes I also never thought that once this old reliable code for last row once will not work on a case, but seems can happen

thanks,
Zsolt
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now