Need edit on this Macro

Shinod

New Member
Joined
Jun 29, 2022
Messages
38
Office Version
  1. 2019
Platform
  1. Windows
Hi Guys,

I want to run a Macro if I change the value in "H3" (Which is working) but along with that, I want to activate the value in "H3" with "A2" if I change the value in "A2"

Could you correct it for me, please?

Thank you in Advance.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$H$3" Then
        Call Hide_Unused_Rows
    End If
        If Target.Address = "$A$2" Then
        Range("H3") = Range("A2").Value
    End If
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Important Tips:
  1. Whenever you are working with Worksheet_Change event, always switch Off events if you are writing data to any cell. This is required so that the code doesn't retrigger the Change event, and go into a possible endless loop
  2. Whenever you are switching off events, use error handling to turn it back on, else if you get an error, the code will not run the next time.
Is this what you are trying (UNTESTED)?

VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Whoa
   
    Application.EnableEvents = False
   
    If Not Intersect(Target, Range("H3")) Is Nothing Then Hide_Unused_Rows
   
    If Not Intersect(Target, Range("A2")) Is Nothing Then Range("H3").Value = Range("A2").Value
   
Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub
 
Upvote 0
@Siddharth Rout , There is an issue:

Issue is I need to run the macro if i change A2 also.

ie i need a way like if i change A2 Run Macro & Activate the value with A
 
Upvote 0
@Siddharth Rout , There is an issue:

Issue is I need to run the macro if i change A2 also.

ie i need a way like if i change A2 Run Macro & Activate the value with A

You mean like this?

VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Whoa
   
    Application.EnableEvents = False
   
    If Not Intersect(Target, Range("H3")) Is Nothing Then Hide_Unused_Rows
   
    If Not Intersect(Target, Range("A2")) Is Nothing Then
        Hide_Unused_Rows
        Range("H3").Value = Range("A2").Value
    End If
    
Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub
 
Upvote 0
Solution
No. The first part is okay. the macro is running if i change value in H3.

Then next if I change I want still need to run the macro AND activate the value in H3 with A2.
 
Upvote 0
No. The first part is okay. the macro is running if i change value in H3.

Then next if I change I want still need to run the macro AND activate the value in H3 with A2.

Not sure if i understand. In the code above...

1. If you change the value of H3, the Hide_Unused_Rows macro is run.
2. If you change the value of A2, the Hide_Unused_Rows macro is run and then H3 gets the value from A2

If this is not what you want then you may have to explain it a bit more...
 
Upvote 0
Not sure if i understand. In the code above...

1. If you change the value of H3, the Hide_Unused_Rows macro is run.
2. If you change the value of A2, the Hide_Unused_Rows macro is run and then H3 gets the value from A2

If this is not what you want then you may have to explain it a bit more...
Yes. This is what i want
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,803
Members
449,048
Latest member
greyangel23

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