Ensure user selects next available Cell in Column A to avoid blank rows

CarolineGMartin90

New Member
Joined
Aug 7, 2017
Messages
13
Hi there!

Hoping someone can help me out! I have written up the below piece of code, which will check if the column directly above the selected Cell in Column A is empty or not, and if it is empty the macro will find the next available cell and make it the ActiveCell - thereby guiding the user to where they must input their data.

I will have that piece of code in a module which I will call, using a Private_Worksheet_SelectionChange, only when the user has selected a cell within Column A.

Both pieces of code run perfectly as Subs when I F8 through them - my problem comes when I want it to run automatically using the Private_Worksheet_SelectionChange.

My question is - does a Private_SelectionChange not recognise merely a "selection", and only a Change of value within a cell? Is there a different declaration I should be using?

Or does anyone have any other suggestions for me?

All help greatly appreciated!!

Caroline

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Not Intersect(ActiveCell, Range("A:A")) Is Nothing Then​

Call SelectionColumnA​
End If​
End Sub​



'to be stored in a module
Sub SelectionColumnA()
If Selection.Offset(-1, 0) = "" Then 'see if cell above is empty
Selection.End(xlUp).Select​
ActiveCell.Offset(1, 0).Select 'select the next available cell in Column A I want user to enter data into

End If​

End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi,
see if this does what you want

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)


    If Not Intersect(Target, Range("A:A")) Is Nothing Then
        Call SelectionColumnA(Me)
    End If


End Sub

Code:
'to be stored in a module
Sub SelectionColumnA(ByVal sh As Object)
    On Error GoTo exitsub
    Application.EnableEvents = False
    sh.Cells(sh.Cells(sh.Rows.Count, "A").End(xlUp).Row + 1, 1).Select
exitsub:
    Application.EnableEvents = True
End Sub

Dave
 
Upvote 0
Thank you so much Dave!! Works an absolute treat!! Exactly as I wanted!

Just out of curiosity so I learn, why must it be "Call SelectionColumnA(Me)" instead of just "Call SelectionColumnA" - previously when I have called modules I have not used them. Is there a difference?

Really appreciate it!

Caroline
 
Last edited:
Upvote 0
Thank you so much Dave!! Works an absolute treat!! Exactly as I wanted!

Just out of curiosity so I learn, why must it be "Call SelectionColumnA(Me)" instead of just "Call SelectionColumnA" - previously when I have called modules I have not used them. Is there a difference?

Really appreciate it!

Caroline

Hi, glad suggestion helped.

As the sheet is active, there should be no real need to pass the worksheet object (Me keyword) to the procedure in standard module - its just I am old school & like to qualify code. Passing worksheet object just ensures the code is working with the intended worksheet. This is something to think about in future where you have a procedure that needs to work with more than one worksheet.

Dave
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top