Solved

VBA Excel 2010 - First blank cell in a column

Posted on 2011-09-21
14
757 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +2
14 Comments
 
LVL 85

Expert Comment

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

Open in new window

0
 
LVL 33

Expert Comment

by:Rob Henson
ID: 36573169
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
ID: 36573187
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 24

Accepted Solution

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

Open in new window

0
 
LVL 1

Author Comment

by:csehz
ID: 36573217
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
ID: 36573226
Stephen thanks your code gaves result 789 so not 4980, and this 789 is my target so seems working
0
 
LVL 33

Expert Comment

by:Rob Henson
ID: 36573272
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
 
LVL 33

Expert Comment

by:Rob Henson
ID: 36573304
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
ID: 36573361
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
ID: 36573411
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
ID: 36573456
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
ID: 36573493
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
ID: 36573510
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
ID: 36573523
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

732 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