Solved

Need Excel sheets to share a macro

Posted on 2001-08-07
16
290 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 8

Expert Comment

by:starl
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Works great and very cool. Thanks.
0
 

Author Comment

by:ksander
Comment Utility
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
Comment Utility
I stated "...especially...". This is not restricted to Subs, it also works for Functions. Never mind.
0
 
LVL 17

Expert Comment

by:calacuccia
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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 article will show you how to use shortcut menus in the Access run-time environment.
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

771 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

12 Experts available now in Live!

Get 1:1 Help Now