Selection Change VBA

ashani

Active Member
Joined
Mar 14, 2020
Messages
345
Office Version
  1. 365
Platform
  1. Windows
Hi
I'm using the below formula for selection change in VBA - it works fine but everytime I click on Column A & B than it comes up with error message - Runtime error 1004. Highlights this line : Dim cel As Range: Set cel = Target.Offset(, -2)

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.CountLarge > 1 Then Exit Sub
    Dim cel As Range: Set cel = Target.Offset(, -2)
    If Not Intersect(Target, Range("U7:U37")) Is Nothing Then
        If cel = "" Then
            cel.Select
            MsgBox "test"
        End If
    End If

Application.CutCopyMode = False

End Sub

Please can someone guide me.

Thank you
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
If you click on a cell in Col "A", then this statement will be invalid as you can't access 2 columns to the left of "A"
VBA Code:
Set cel = Target.Offset(, -2)
Try using
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.CountLarge > 1 Then Exit Sub
    If Not Intersect(Target, Columns("A:B")) Is Nothing Then Exit Sub
    Dim cel As Range: Set cel = Target.Offset(, -2)
    If Not Intersect(Target, Range("U7:U37")) Is Nothing Then
        If cel = "" Then
            cel.Select
            MsgBox "test"
        End If
    End If

Application.CutCopyMode = False

End Sub
 
Upvote 0
absolute legend - thank you so much for your help.

Can I ask you another question regarding VBA - I'm using the below coding and it's working fine but I want that everytime I click on the button than it goes to the first blank cell in Column A. Please can you guide me on that.

VBA Code:
Sub Tabshape()
With Sheet7

.Shapes("sheet1On").Visible = msoCTrue
.Shapes("sheet1Off").Visible = msoFalse

Sheet7.Activate

Thank you
 
Upvote 0
What is in the rest of the code ?
Where did you want the cursor to go to ?
 
Upvote 0
Rest of the code is
End with
End sub

I want cursor to go the sheet 1 column A in first blank cell. Thanks
 
Upvote 0
Try
VBA Code:
Sub Tabshape()
Dim lr As Long
With Sheet7
.Shapes("sheet1On").Visible = msoCTrue
.Shapes("sheet1Off").Visible = msoFalse
End With
lr = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
Sheets("Sheet1").Activate
Range("A" & lr).Select
End Sub
 
Upvote 0
Hi Michael,

thank you for the reply.

Unfortunately, it's not working for me, as its select the last filled cell of A55 instead of first blank cell of A7. The cells have data validation list there but nothing select and it's blank.

Thank you.
 
Upvote 0
Thanks you Peter

What's your thoughts on my Post 6 query ?
 
Upvote 0
What's your thoughts on my Post 6 query ?
Post 3? ;)

Untested, but try
VBA Code:
Dim FirstBlank As Range

On Error Resume Next
Set FirstBlank = Sheets("Sheet1").Columns("A").SpecialCells(xlBlanks).Cells(1)
On Error GoTo 0
If FirstBlank Is Nothing Then Set FirstBlank = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1)
Application.Goto Reference:=FirstBlank, Scroll:=True
 
Upvote 0

Forum statistics

Threads
1,215,154
Messages
6,123,327
Members
449,098
Latest member
thnirmitha

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