0 votes
in VBA by Beginner (5 points)

https://ask.wellsr.com/?qa=blob&qa_blobid=5638158367059313683Why does this code not work on this worksheet. I have a similar workbook that it works perfectly for.

​
Sub Auto_Open()
Dim ws As Worksheet

    Set ws = ActiveSheet
    For Each cell In ws.Columns(2).cells
        If IsEmpty(cell) = True Then cell.Select: Exit For
    Next cell
 
End Sub


​


 

 

by Expert (798 points)
May be trying to do that using If cell.Value = "" Instead of using IsEmpty
by Beginner (5 points)

Thanks, Yasser, but that didn't work. I'm wondering if the change event code is causing the Auto_Open to not work correctly.
 

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("B6:I45")) Is Nothing Then
Range("A6").Value = Target.Row
End If
End Sub

 

by Expert (798 points)
So in your auto_open macro put a line at the start that disables events using Application.EnableEvents=False and at the end restore it using the True value.
by Beginner (5 points)
Tried but still does not do it. Anything else?

Thanks,

Ken Mc
by Expert (798 points)
Can you upload sample file to have a look?
by Beginner (5 points)

Not sure where to upload. Is it link and the https://ask.wellsr.com/?qa=blob&qa_blobid=9085517926208232791

by Beginner (5 points)
Did you get the file?
by Expert (798 points)
Yes I have the file and when opening the code already selects the cell B2 which is empty. So what is the problem now??
by Beginner (5 points)
I am trying to get the code to find the first blank row. In this case, it would be B46
Thanks

1 Answer

0 votes
by Expert (798 points)
selected by
 
Best answer

Try this code

Sub Auto_Open()
    Dim ws As Worksheet, cell As Range

    Set ws = ActiveSheet
    For Each cell In ws.Columns(2).cells
        If cell.Value = "" And cell.Row > 5 Then cell.Select: Exit For
    Next cell
End Sub

 

by Super Expert (2.8k points)
+1

It might be a good idea to apply the Trim function when searching for empty strings. Sometimes people think cells are empty but they actually contain a space.

If Trim(cell.Value) = "" And cell.Row > 5 Then cell.Select: Exit For

 

by Expert (798 points)
Thanks a lot my tutor for this information.
by Beginner (5 points)
Thanks so much Yasser. That worked perfectly. I really appreciate all your help!
Best,
Ken Mc
by Expert (798 points)
You're welcome. Glad I can offer little help.

Welcome to wellsr Q&A
wellsr Q&A is the VBA and Python programming community that rewards you for learning how to code.

Getting Started
Register
VBA Cheat Sheets (On Sale Now)

Earn free prizes for asking VBA and Python questions and for answering questions asked by others in our community.

Looking for something else? Hire our professional VBA Help, instead.

What makes us different?
Our points system rewards you with a chance for free gifts based on the quality of your questions and answers. All you have to do is post and you could get rewarded, like these members:

ParserMonster $25 Amazon Gift Card
Hightree $10 Amazon Gift Card
Thales1 $10 Amazon Gift Card
runfunke $10 Amazon Gift Card
coolag $10 Amazon Gift Card
Siew Hun $10 Amazon Gift Card

So, why don't you join us? It really is an encouraging way to motivate members in our VBA and Python community.

Register

For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.

...