Solved

VBA Excel 2010 - First blank cell in a column

Posted on 2011-09-21
14
749 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
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
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…

828 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