?
Solved

VBA Excel 2010 - First blank cell in a column

Posted on 2011-09-21
14
Medium Priority
?
765 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 24

Accepted Solution

by:
StephenJR earned 2000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

765 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