Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Multiple VBS Macros running at once

Posted on 2014-02-10
2
Medium Priority
?
296 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 22

Accepted Solution

by:
rspahitz earned 2000 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

722 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