Modifying a spreadsheet using a VB macro


I have two spreadsheets... when one gets modified in a certain way I want to have a macro run that modifies the second in an appropriate manner. I've already isolated the event I need to act on (the modification of any cell in a particular column), I just can't seem to find any concrete information on accessing and modifying another spreadsheet (this spreadsheet is located on a different LAN share also... the user has access to both, though).

Any help would be great. References on how to do this or something similar are just as good as concrete code samples.


Copy the following in your ThisWorkbook object to watch for specific changes. In this case when you increase a numeric value to another numeric value.

NB: you will have to replace Workbook-SheetChange and Workbook-SheetSelectionChange with an underscore. Ex: Workbook_SheetChange and Workbook_SheetSelectionChange the underscore gets escaped in Markdown code.

Option Explicit
Dim varPreviousValue As Variant ' required for IsThisMyChange() . This should be made more unique since it's in the global space.

Private Sub Workbook-SheetChange(ByVal Sh As Object, ByVal Target As Range)
  ' required for IsThisMyChange()
  IsThisMyChange Sh, Target
End Sub

Private Sub Workbook-SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
  '  This implements and awful way of accessing the previous value via a global.
  '  not pretty but required for IsThisMyChange()
  varPreviousValue = Target.Cells(1, 1).Value ' NB: This is used so that if a Merged set of cells if referenced only the first cell is used
End Sub

Private Sub IsThisMyChange(Sh As Object, Target As Range)
  Dim isMyChange As Boolean
  Dim dblValue As Double
  Dim dblPreviousValue As Double

  isMyChange = False

  ' Simple catch all. If either number cant be expressed as doubles, then exit.
  On Error GoTo ErrorHandler
  dblValue = CDbl(Target.Value)
  dblPreviousValue = CDbl(varPreviousValue)
  On Error GoTo 0 ' This turns off "On Error" statements in VBA.

  If dblValue > dblPreviousValue Then
     isMyChange = True
  End If

  If isMyChange Then
    MsgBox ("You've increased the value of " & Target.Address)
  End If

  ' end of normal execution
  Exit Sub

  ' Do nothing much.
  Exit Sub

End Sub

If you are wishing to change another workbook based on this, i'd think about checking to see if the workbook is already open first... or even better design a solution that can batch up all your changes and do them at once. Continuously changing another spreadsheet based on you listening to this one could be painful.

By : Mark Nold

After playing with this for a while, I found the Michael's pseudo-code was the closest, but here's how I did it:

Dim xl As Excel.Application
Set xl = CreateObject("Excel.Application")
xl.Workbooks.Open "\\owghome1\bennejm$\testing.xls"

Then, manipulate the sheet... maybe like this:

xl.Cells(x, y).Value = "Some text"

When you're done, use these lines to finish up:


If changes were made, the user will be prompted to save the file before it's closed. There might be a way to save automatically, but this way is actually better so I'm leaving it like it is.

Thanks for all the help!

You can open a spreadsheet in a single line:

Workbooks.Open FileName:="\\the\share\file.xls"

and refer to it as the active workbook:

Range("A1").value = "New value"

This video can help you solving your question :)
By: admin