Help with vba to activate on input of cell

A Thayuman

New Member
Joined
Mar 6, 2019
Messages
28
Hi I hope that I can be helped with using activate on input cell

'Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Not Intersect(Traget, Range("A2:D2")) Is Nothing Then

Sheets("Sheet1").Select
Range("A2:D2").Select
Selection.UnMerge
Range("A2").Select
Selection.Copy
Sheets("Sheet2").Select
Range("A1").Activate
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Range("A1").Select

ActiveSheet.Paste

Sheets("Sheet1").Select
Range("A2:D2").Select
Selection.Merge

End If

End Sub

The current macro as see below works fine without adding this feature however when I use the above and when it is in module 1
Sub test()
'Copy Invoice Number to Data Sheet

Sheets("Sheet1").Select
Range("A2:D2").Select
Selection.UnMerge
Range("A2").Select
Selection.Copy
Sheets("Sheet2").Select
Range("A1").Activate
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Range("A1").Select

ActiveSheet.Paste

Sheets("Sheet1").Select
Range("A2:D2").Select
Selection.Merge
End Sub



Mr.xlsm
ABCDEFGH
1Customer IDCustomer Name
2126Jack Garret
Sheet1


Mr.xlsm
AB
1Customer IDCustomer Name
2123Jack Garret
3123
4123
Sheet2
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Try this code in Sheet1 and see if it works for you.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$2" Then
Sheets("Sheet1").Select
Range("A2:D2").Select
Selection.UnMerge
Range("A2").Select
Selection.Copy
Sheets("Sheet2").Select
Range("A1").Activate
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("A2:D2").Select
Selection.Merge
End If

End Sub
 
Upvote 0
The code now takes me to Sheet2, please see image below, thanks again
 

Attachments

  • Active Sheet VBA debug1.png
    Active Sheet VBA debug1.png
    191.2 KB · Views: 5
Upvote 0
Hi Skybolt thanks for your help - I have managed to stumble into this code after trying for the whole day, it works
Is there a way to make it better


Private Sub Worksheet_Change(ByVal Target As Range)
Sheets("Sheet1").Select
Range("A2:D2").Select
Selection.UnMerge
Range("A2").Select
Selection.Copy
Sheets("Sheet2").Select
Sheets("Sheet2").Range("A1").Activate

Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveSheet.Paste

Sheets("Sheet1").Select
Range("A2:D2").Select
Selection.Merge
End Sub
 
Upvote 0
There is a drawback in the code - if cell "A2" is not filled then the there is a debug message, the cursor goes down to the last row in the column
 
Upvote 0
There is a drawback in the code - if cell "A2" is not filled then the there is a debug message, the cursor goes down to the last row in the column
We have made so many changes. :) Which code are you using?
 
Upvote 0
We have made so many changes. :) Which code are you using?

Hi Skybolt the code that works is shown below - however we need to have A2 filled with information, if it is blank the code goes right down to the the bottom of the sheet in coloum "A". Thanks as always for your offer to help


Private Sub Worksheet_Change(ByVal Target As Range)
Sheets("Sheet1").Select
Range("A2:D2").Select
Selection.UnMerge
Range("A2").Select
Selection.Copy
Sheets("Sheet2").Select
Sheets("Sheet2").Range("A1").Activate
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("A2:D2").Select
Selection.Merge
End Sub
 
Upvote 0
Hi Skybolt the code that works is shown below - however we need to have A2 filled with information, if it is blank the code goes right down to the the bottom of the sheet in coloum "A". Thanks as always for your offer to help.
It started working when the code to Activate the Sheet and range was added
Sheets("Sheet2").Select
Sheets("Sheet2").Range("A1").Activate

The code that works is:

Private Sub Worksheet_Change(ByVal Target As Range)
Sheets("Sheet1").Select
Range("A2:D2").Select
Selection.UnMerge
Range("A2").Select
Selection.Copy
Sheets("Sheet2").Select
Sheets("Sheet2").Range("A1").Activate
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("A2:D2").Select
Selection.Merge
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,977
Members
449,200
Latest member
Jamil ahmed

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