Types of Private Sub

smilelover

New Member
Joined
Jul 24, 2011
Messages
32
I want to use my code personally on that workbook,
and I hope that it runs on the start up of the workbook and ANY changes on the workbook.

Currently, I've tried
Private Sub Workbook_Open()
Private Sub Workbook_Activate()

They both runs on start up of workbook and sync when I made any changes on EXISTING data, not when I add new data, for example, my code refers to column A on Sheet1 and column A on Sheet4, if I have 20 data on Sheet1!A and I made any changes on them, they will appear on Sheet4!A as well, but if I add an 21st data on Sheet1!A, they won't appear.

My code is :
Code:
Private Sub Workbook_Open()
    Dim rowA As Long
    For rowA = 1 To WorksheetFunction.CountA(Range("Sheet1!A:A"))
        Range("=Sheet4!A" & rowA * 8 - 5).Formula = ("=Sheet1!A" & rowA)
        Range("=Sheet4!J" & rowA * 8 - 5).Formula = ("=Sheet1!B" & rowA)
        Range("=Sheet4!K" & rowA * 8 - 5).Formula = ("=Sheet1!C" & rowA)
        Range("=Sheet4!L" & rowA * 8 - 5).Formula = ("=Sheet1!D" & rowA)
    Next rowA
    
End Sub

Private Sub Workbook_Activate()
    Dim rowB As Long
    For rowB = 1 To WorksheetFunction.CountA(Range("Sheet1!A:A"))
        Range("=Sheet4!B" & rowB * 8 - 5).Formula = ("=Sheet2!A1")
        Range("=Sheet4!C" & rowB * 8 - 4).Formula = ("=Sheet2!B2")
        Range("=Sheet4!D" & rowB * 8 - 3).Formula = ("=Sheet2!C3")
        Range("=Sheet4!E" & rowB * 8 - 2).Formula = ("=Sheet2!D4")
        Range("=Sheet4!F" & rowB * 8 - 1).Formula = ("=Sheet2!E5")
        Range("=Sheet4!G" & rowB * 8 - 0).Formula = ("=Sheet2!F6")
        Range("=Sheet4!H" & rowB * 8 + 1).Formula = ("=Sheet2!G7")
        Range("=Sheet4!I" & rowB * 8 + 2).Formula = ("=Sheet2!H8")
    Next rowB

Please tell me what kind of Private sub should I use :confused:
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
You need the Worksheet_change event. It provides you one argument - Target is the cells being changed. You need to put the code into the code module for sheet1 - right click the sheet tab and choose view code.
 
Upvote 0
Hmm I keep getting an error:

Compile Error:
Procedure declaration does not match description of event or procedure having the same name.

Whats going on?
 
Upvote 0
You have declared it wrongly. Select Worksheet from the dropdown at the top left of the main code window and then select Change from the right hand dropdown list. That will create the declaration for you in the correct syntax:
Code:
private sub worksheet_change(byval target as excel.range)
 
Upvote 0

Forum statistics

Threads
1,224,538
Messages
6,179,412
Members
452,912
Latest member
alicemil

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