Solved

Multiple VBS Macros running at once

Posted on 2014-02-10
2
276 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

706 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