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

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
What exactly are you trying to do? Unmerge cells but keep the data?
 
Upvote 0
What exactly are you trying to do? Unmerge cells but keep the data?
Thanks for responding to my call for help.

I would like to copy from merged cells from Sheet1 to a single cell in Sheet2, using the VBA to activate when I change data in Cell "A2" in Sheet1.

the code below works fine when in a module (However I have to use a short cut key or Run Sub)

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

However when I try to change it to so that I dont have to hit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Not Intersect(Traget, Range("A2:D2")) Is Nothing Then
with the same code
End If

End Sub

If does not work and gets stuck at "Range("A1").Activate", I tried ".Select" same result

Regards Anil
What exactly are you trying to do? Unmerge cells but keep the data?
I would like to copy data from Sheet1 where rows are merged (Say "A2:D2") ,to sheet2 in a single cell Say ("A2")
Also in sheet2, if cell "A2" has information then cell "A3" is filled.
I hope to use to activate the VBA when information in Sheet1 ("A2") is changed
 
Upvote 0
Try this and see if you get the results that you desire. Please make sure that this Macro is placed in Sheet1 of the VBA project.
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target = Worksheets("Sheet1").Range("A2") 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
Try this and see if you get the results that you desire. Please make sure that this Macro is placed in Sheet1 of the VBA project.
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target = Worksheets("Sheet1").Range("A2") 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
Thanks Skybolt for the code however please see image -its stops at the first line of code
Active Sheet VBA debug.png
 
Upvote 0
I dont understand the code as seen below works in the module and not we try to activate it with the "IF Then and End If"?

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

Thanks again Skybolt
 
Upvote 0
My fault. Try this small change the code in Sheet1 in the VBA project.
VBA Code:
Private Sub Worksheet_SelectionChange(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
Hi Skybolt,

I tried adding the new code in Sheet1 - now there code does not do anything, There is no Debug error in the VBA Code either

Thanks for the effort I wonder I you would like to advice me again Regards Anil
 
Upvote 0
I added two Message Boxes to see if the code is running but just not doing what we want. Let me know which Message Box you see.
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$A$2" Then
MsgBox "Found Target",vbInformation,"Sub Test"
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
MsgBox "Didn't find Target but code did run.",vbInformation,"Sub Test"
End Sub
 
Upvote 0
I added two Message Boxes to see if the code is running but just not doing what we want. Let me know which Message Box you see.
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$A$2" Then
MsgBox "Found Target",vbInformation,"Sub Test"
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
MsgBox "Didn't find Target but code did run.",vbInformation,"Sub Test"
End Sub
Thanks again Skybolt please see image attached, Thanks you once again for the effort put in
 

Attachments

  • VBA did'nt find traget but code did run.png
    VBA did'nt find traget but code did run.png
    154.4 KB · Views: 7
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,449
Members
449,083
Latest member
Ava19

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