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 (916 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 (916 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 (916 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 (916 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 (916 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 (3.2k 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 (916 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 (916 points)
You're welcome. Glad I can offer little help.

Welcome to wellsr Q&A
Ask any questions you have about VBA and Python and our community will help answer them. wellsr Q&A is the standalone question and answer platform for wellsr.com. If you have a question about one of our specific tutorials, please include a link back to the tutorial.

Getting Started
Register
VBA Cheat Sheets (On Sale Now)

Looking for something else? Hire our team directly through ourVBA Help page, instead.

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

...