Solved

Multiple VBS Macros running at once

Posted on 2014-02-10
2
282 Views
Last Modified: 2014-02-10
I have an excel sheet that uses a VBS macro to update a sheet when a change is made on another sheet. This works fine, now I need to add an almost identical macro to the sheet that updates when different cells are changed. I actually need to add MANY of these MACROS. The problem I am having is that when I create and 2nd "Module" in the VBS editor and "RUN" the new macro it works fine, but when in the actual sheet itself, only the original macro seems to run. How can I get all my other Macros to kickoff when the first one does. Below is the VBS of the original and functioning Macro "Module1" and the bottom is the 2nd Module2 that does not work unless manually run.

Module1
Sub UpdateMonthlyTotals()

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Dim ColumnNumber As Integer

Const StartYear = 2013
Const StartMonth = 9

Const StartRow = 15
Const StartColumn = 2
Const SourceRow = 142
Const SourceStartColumn = 3
Const NumberOfItems = 10

Dim CurrentYear As Integer
Dim CurrentMonth As Integer
Dim ItemNumber As Integer

Dim SourceCell As String
Dim TargetCell As String
Dim SourceValue As String


Dim SourceFormat As Variant
CurrentYear = Year(Now())
CurrentMonth = Month(Now())
ColumnNumber = (CurrentYear - StartYear) * 12 + (CurrentMonth - StartMonth) + StartColumn
For ItemNumber = 0 To NumberOfItems - 1
  SourceCell = Chr(ItemNumber + 64 + SourceStartColumn) & Format(SourceRow, "0")
  TargetCell = Chr(ColumnNumber + 64) & Format(StartRow + ItemNumber * 12 + 1)
  Sheets("Scorecard").Select
  SourceValue = Range(SourceCell).Value
  SourceFormat = Range(SourceCell).NumberFormat
 
  Sheets("Plan vs Actual").Select
  Range(TargetCell).Value = SourceValue
  Range(TargetCell).NumberFormat = SourceFormat
Next ItemNumber
Sheets("Scorecard").Select

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub

Module2
Sub UpdateERMMonthlyTotals()

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Dim ColumnNumber As Integer

Const StartYear = 2013
Const StartMonth = 9

Const StartRow = 41
Const StartColumn = 2
Const SourceRow = 6
Const SourceStartColumn = 5
Const NumberOfItems = 1

Dim CurrentYear As Integer
Dim CurrentMonth As Integer
Dim ItemNumber As Integer

Dim SourceCell As String
Dim TargetCell As String
Dim SourceValue As String


Dim SourceFormat As Variant
CurrentYear = Year(Now())
CurrentMonth = Month(Now())
ColumnNumber = (CurrentYear - StartYear) * 12 + (CurrentMonth - StartMonth) + StartColumn
For ItemNumber = 0 To NumberOfItems - 1
  SourceCell = Chr(ItemNumber + 64 + SourceStartColumn) & Format(SourceRow, "0")
  TargetCell = Chr(ColumnNumber + 64) & Format(StartRow + ItemNumber * 1 + 1)
  Sheets("Scorecard").Select
  SourceValue = Range(SourceCell).Value
  SourceFormat = Range(SourceCell).NumberFormat
 
  Sheets("Plan vs Actual").Select
  Range(TargetCell).Value = SourceValue
  Range(TargetCell).NumberFormat = SourceFormat
Next ItemNumber
Sheets("Scorecard").Select

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True


End Sub
0
Comment
Question by:Mcottuli
2 Comments
 
LVL 22

Accepted Solution

by:
rspahitz earned 500 total points
ID: 39847753
>update a sheet when a change is made on another sheet.

If I understand this correctly, you want a macro to run "automatically" when a change event occurs.
In your new macro, it is apparently not linked to any events so it only runs "on demand".

To fix this, you'll need to identify an appropriate event (like the Change event of a sheet) and tell it to call your new macro.

I would suggest opening the code area for the sheet that currently works to initiate the working macro and look for something like this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   UpdateMonthlyTotals
End Sub

Open in new window


You'll want to do something like that in the other sheet where you want things to happen or (if it's the same sheet) add the new subroutine inside the same "event sub" above.
0
 
LVL 1

Author Closing Comment

by:Mcottuli
ID: 39847847
Completely missed the obvious! Thanks!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

830 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