worksheet_SelectionChange for a specific column

jmmac

New Member
Joined
Jul 24, 2014
Messages
30
I have a worksheet that contains invoice numbers in column D. I'd like to copy the invoice number to another worksheet ("Details") when one is selected. I've added the "If IsNumeric" condition to make sure that only cells containing an invoice # can be copied over. The code is in the Sheet1 module, but seems to do nothing. Can anyone help point me in the right direction?
<code>

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      If Target.Column = 4 Then
      On Error Resume Next
            Application.EnableEvents = False
            If IsNumeric(Target.Address) Then
                  ActiveSelection.Copy
                  Sheets("Detail").Range("A5").Select
                  ActiveSelection.Paste
            End If
            Application.EnableEvents = True
      End If
End Sub
</code>
 
I'd prefer both actions (copy target to Detail sheet and open Detail sheet) to occur with one click.

The blank cell being recognized as numeric brings up another problem - I only want cells with an invoice# to activate the Change event. The list of invoices varies daily, and is always D36:D?. I know how to determine the last row of data in a column, should I be defining a range rather than looking at Column 4?
This modification will ensure that selecting a blank cell does nothing. One click on any numeric cell in col 4 will do it - works for me with a single click.
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      If Target.Column = 4 Then
             Set Target = Target.Cells(1, 1)
            If IsNumeric(Target.Value) And Not IsEmpty(Target.Value) Then
                  Sheets("Detail").Range("A5").Value = Target.Value
                Sheets("Detail").Activate
            End If
      End If
End Sub
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
It seems to be working now. Takes about 5 seconds from the time I click until it changes sheets - I didn't realize it took a bit to run such a small macro, maybe I was just too impatient! Thanks for the help!
 
Upvote 0
It seems to be working now. Takes about 5 seconds from the time I click until it changes sheets - I didn't realize it took a bit to run such a small macro, maybe I was just too impatient! Thanks for the help!
If the cell you are copying the ID to in sheet Detail is a precedent cell for formulas on that sheet or elsewhere, you may be seeing a delay while worksheets calculate. In that case, try this revision:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      If Target.Column = 4 Then
             Set Target = Target.Cells(1, 1)
            If IsNumeric(Target.Value) And Not IsEmpty(Target.Value) Then
                Application.Calculation = xlCalculationManual
                  Sheets("Detail").Range("A5").Value = Target.Value
                Sheets("Detail").Activate
                Application.Calculation = xlCalculationAutomatic
            End If
      End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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