VBA to populate cell A2 with cell A2 from previously select WKST.

KP_SoCal

Board Regular
Joined
Nov 17, 2009
Messages
116
I have an excel workbook with tabs: Sheet1, Sheet2, Sheet3, Sheet 4, Sheet 5, Sheet 6, and Sheet7.

For Sheets 1 thru 5, I need cell A2 on a newly activated sheet to populate with the value on the most recent worksheet. Sheet1 holds the default value of “Widget1” in A2. So if I click on any Sheet between 2 and 5 (Sheets 6 and 7 should be excluded in criteria), cell A2 would populate with “Widget1”.

However, if I decide to populate cell A2 on Sheet3 with “Widget3”, then if I click back to Sheet1 it would populate with “Widget3” in call A2. Does this make sense?

I’ll probably need something like this code below, but I’m just not sure how to piece it together to make it work in the way I described above. Any advice is greatly appreciated. :biggrin:

Code:
Dim wsSheet As Worksheet
Application.ScreenUpdating = False
For Each wsSheet In ThisWorkbook.Worksheets
    If wsSheet.CodeName =        Then
        
    Else
        
    End If
Next wsSheet
Application.ScreenUpdating = True
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Your problem is like a dog chasing its tail ...
When editing a cell A2 sheets 2 to 5 and you active sheet 1 find modify, but when you switch back to 2 to 5 sheets of A2 of the modified sheet 1 is extended to other sheets. Anyway:
In workBook:
Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
On Error Resume Next
For i = 2 To 5
       Worksheets(i).Range("A2") = Worksheets(1).Range("A2")
   Next
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
On Error Resume Next
If Intersect(Target, Range("A2")) Is Nothing Then Exit Sub
If Target <> 0 Then
   For i = 2 To 5
       Worksheets(1).Range("A2") = Target
   Next
End If
End Sub
Bye
 
Upvote 0
Code:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    If Sh.Name <> "Sheet1" Then
        Worksheets("Sheet1").Range("A2").Value = Sh.Range("A2").Value
    End If
End Sub
 
Upvote 0
Mike, I like it! This will be cleaner and I like that it uses the deactivation event since I'm already running other procedures for the activation effect. Thanks you so much!!! :biggrin:

KP
 
Upvote 0

Forum statistics

Threads
1,214,589
Messages
6,120,415
Members
448,960
Latest member
AKSMITH

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