Solved

Need Excel sheets to share a macro

Posted on 2001-08-07
16
295 Views
Last Modified: 2006-11-17
Many sheets in my workbook have their own copy of the same macro.  To reduce space, I'd like there to be only one copy that they all can share.  The macro begins:

"Option Explicit"
"Private Sub Worksheet_Change(ByVal Target As Range)"

It goes into action if there is input in certain cells.

Can it be modified and put in a common area where it will be accessible to all sheets?

Thanks
0
Comment
Question by:ksander
  • 8
  • 3
  • 2
  • +2
16 Comments
 
LVL 8

Expert Comment

by:starl
ID: 6360433
sure, get rid of the private and put it in a module. you can create/add a module by (in the VB editor) right-click on the project and choose INSERT-MODULE.
course, depending on how your progs written, you may have to make some other changes...
0
 
LVL 13

Expert Comment

by:cri
ID: 6360530
Put them in an add-in (*.xla), especially if you want/need to call them 'manually'. Easiest way I know:
http://www.j-walk.com/ss/excel/tips/tip53.htm
0
 

Author Comment

by:ksander
ID: 6360531
I have already tried putting the code into another module and removing 'Private'. Also tried removing 'Option Explicit'. Didn't work.

The only other way I see that it differs from other macros that are available to all sheets is the "(ByVal Target As Range)" wording. So I removed that too - still didn't work.

I think the answer must be to "make some other changes" but I don't know how.
0
 

Author Comment

by:ksander
ID: 6360584
cri - this macro isn't to be called manually. It works fine now - it just watches certain cells for input. If there is, it automatically performs caclulations and stores the results in other cells.  
0
 

Author Comment

by:ksander
ID: 6360631
Also - The macro has lines like:

"If Target.Column <> 4 Then Exit Sub"

that break if "(ByVal Target As Range)" is taken out.

I believe what's happening is that if the "ByVal" code is there, the macro can't be seen, but, it's required for functions within the macro.

So some other changes have to be made.  I don't have a clue what they would be.
0
 
LVL 8

Expert Comment

by:starl
ID: 6360739
post the code.
btw - how is it not working w/the changes? compilation error? Since it was originally a worksheet script, we may just have to be more explicit in certain areas since it's moving out to the workbook.
also, there may be some variables you have to declare as Const
0
 

Author Comment

by:ksander
ID: 6360774
Also - The macro has lines like:

"If Target.Column <> 4 Then Exit Sub"

that break if "(ByVal Target As Range)" is taken out.

I believe what's happening is that if the "ByVal" code is there, the macro can't be seen, but, it's required for functions within the macro.

So some other changes have to be made.  I don't have a clue what they would be.
0
 

Author Comment

by:ksander
ID: 6360911
If I remove 'Private' and/or 'Option Explicit', it just isn't seen - the worksheet doesn't react and when I go to Tools-Macro it isn't there.

When I remove "ByVal" I can see it in Macros. If I enter something that should cause a reaction nothing happens. If I try to Run from the Macro list I get "Run-time error - object required".  That's the "break" I mentioned.

Here's the original code:
***********************
Option Explicit
--------------
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim r As Long       'Row of changed value
  Dim c As Long       'Currently handled column in loop
  Dim x As Double     'Value entered into column 4
  Dim d As Integer    'Number of days in the month
  Dim leftover As Long  'Leftover to be distributed
     
  If Target.Column <> 4 Then Exit Sub
  If Target.Row >= 13 And Target.Row <= 35 Then GoTo UseIt
  Exit Sub
UseIt:
  If Target.Areas.Count <> 1 Then Exit Sub
  If Target.Cells.Count <> 1 Then Exit Sub
  ' Find row number of changed cell
  r = Target.Row
  ' Find #of days in the month (highest value of row 9)
  d = WorksheetFunction.Max(Range("9:9"))
  ' Read the value in column D, store as variable x.
  On Error Resume Next
  x = CDbl(Cells(r, 4))
  If x = 0 Then
    'Reset number format to default
     Cells(r, 4).NumberFormat = "0_);(0)"
    'Save 0 for all days of the month
     Cells(r, 6).Resize(1, d).Value = 0
  End If
  If x >= 1 Then
    ' Change number format
     Cells(r, 4).NumberFormat = "0_);(0)"
    ' Put x / d, rounded, into all days of the month
    Cells(r, 6).Resize(1, d).Value = CLng(x / d)
    ' Calculate the remaining amount to be distributed
    leftover = x - (CLng(x / d) * d)
    ' Distribute the leftover amount randomly
    Do Until leftover = 0
      'Find a random column
      c = Int(Rnd * d) + 6
      'If the number in this column hasn't been changed
      If Cells(r, c).Value = CLng(x / d) Then
        'Add 1 (or -1) to the value
        Cells(r, c).Value = Cells(r, c).Value +    (leftover / Abs(leftover))
        'Decrease leftover with 1 (or -1)
        leftover = leftover - (leftover / Abs(leftover))
      End If
    Loop
  End If
End Sub
 
 

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Expert Comment

by:starl
ID: 6361068
well, for starters, having a ByVal means that you are going to pass a value in.. the only way of doing that (that I know of) is to call this macro from within another macro.
How were you calling the macro originally?
0
 

Author Comment

by:ksander
ID: 6361103
I don't think the macro is being called by another.  All I have to do to invoke it is to go (by selecting its tab) to one of the sheets that 'housed' the macro.

Then nothing happens until I made an entry into one of the "watched" cells.
0
 
LVL 5

Accepted Solution

by:
jklmn earned 100 total points
ID: 6361128
Hi ksander,

It looks very easy.....hopefully, I am right:-)

In each Sheet:
Private Sub Worksheet_Change(ByVal Target As Range)
MyWorksheet_Change Target
End Sub

In a module:
Public Sub MyWorksheet_Change(ByVal Target As Range)
MsgBox "Yes, I know."
End Sub

You may need some midification though.
0
 
LVL 5

Expert Comment

by:jklmn
ID: 6361142
I mean put the original code to
Public Sub MyWorksheet_Change(ByVal Target As Range)
in a module, and call this sub from
Private Sub Worksheet_Change(ByVal Target As Range)
in each sheets.
0
 

Author Comment

by:ksander
ID: 6361163
Works great and very cool. Thanks.
0
 

Author Comment

by:ksander
ID: 6361215
jklmn - Please watch for another question coming in a few minutes that may require this or similar logic.
0
 
LVL 13

Expert Comment

by:cri
ID: 6361434
I stated "...especially...". This is not restricted to Subs, it also works for Functions. Never mind.
0
 
LVL 17

Expert Comment

by:calacuccia
ID: 6361815
You do not need this MyWorksheet_Change macro, as it already exists in the ThisWorkbook module, where you have this event

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Excel.Range)

End Sub

This will apply to all worksheets.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now