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
 
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Range("A2").Value = "" Then
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 If
End Sub
 
Upvote 1
Solution

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
@A Thayuman: I removed the solution mark from your own post since the provided code in Post #12 was already the correct implementation except it works on every single worksheet_change event - which you already realized that it is not the efficient way of solving the problem.

With your latest follow-up question, @Skyybot already provided a much better approach which is checking the A2 cell and only executing if the cell is empty, so Post #21 is the answer to the question as I can see as I would mark it as the solution to help future readers.
 
Upvote 1
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Range("A2").Value = "" Then
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 If
End Sub
Thanks Skybolt works perfectly
 
Upvote 0
@A Thayuman: I removed the solution mark from your own post since the provided code in Post #12 was already the correct implementation except it works on every single worksheet_change event - which you already realized that it is not the efficient way of solving the problem.

With your latest follow-up question, @Skyybot already provided a much better approach which is checking the A2 cell and only executing if the cell is empty, so Post #21 is the answer to the question as I can see as I would mark it as the solution to help future readers.
Thanks I have a better understanding of why the if, then and end if is required
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,613
Members
449,090
Latest member
vivek chauhan

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