0

I am trying to use the VB code from this question in Excel 2013 and it is not working. I am not familiar with VB and am most likely missing a simple step.

Private Sub Workbook_TwoWayMatch(ByVal Sh As Object, ByVal Target As Range) 
If UCase(Sh.Name) = "sheet1" Or UCase(Sh.Name) = "sheet2" Then 
    If Not Application.Intersect(Target, Range("A1")) Is Nothing Then 
        Application.EnableEvents = False 
        If UCase(Target.Parent.Name) = "SHEET1" Then 
            Sheets("Sheet2").Range("A1") = Target 
        Else 
            Sheets("Sheet1").Range("A1") = Target 
        End If 
        Application.EnableEvents = True 
    End If 
 End If
End Sub

Using a new workbook with worksheets named Sheet1 and Sheet2 I have tried the following in Visual Basic:

  • pasting the code into the object: ThisWorkbook
  • pasting the code into the objects: Sheet1 (Sheet1) and Sheet2 (Sheet2)
  • pasting the code into all 3 objects
  • ensuring the workbook is saved as macro enabled
  • ensuring the workbook security settings are set to trust access to the VBA object model and that macros are enabled

What do I need to do in order to get this code to run?

In response to Mat's Mug's suggestion:

I put the code in a standard module as you suggested, changed the procedure to Public and modified the range. However, I still cannot get it to copy the values from the range on one sheet to the other. The code looks like this now:

Public Sub Workbook_TwoWayMatch(ByVal Sh As Object, ByVal Target As Range)
If UCase(Sh.Name) = "SHEET1" Or UCase(Sh.Name) = "SHEET2" Then
    If Not Application.Intersect(Target, Range("A1:J23")) Is Nothing Then
        Application.EnableEvents = False
        If UCase(Target.Parent.Name) = "SHEET1" Then
            Sheets("Sheet2").Range("A1:J23") = Target
        Else
            Sheets("Sheet1").Range("A1:J23") = Target
        End If
        Application.EnableEvents = True
    End If
 End If
End Sub
2
  • Where are you telling it to run that sub. It can be run from a cell or on a worksheet_change event or another location. It needs to be called somewhere.
    – wbeard52
    Commented Dec 18, 2015 at 2:40
  • I do not believe I am calling it anywhere right now. The code above is all I have. Based on the question where I found it, I thought it was a simple copy and paste job but I now see that is not the case. Ideally I would like it to run anytime a cell changes.
    – raindelay
    Commented Dec 18, 2015 at 16:12

2 Answers 2

1

ThisWorkbook and SheetX objects are special "document" class modules; macros are easier exposed in standard modules, so instead of pasting it everywhere in there, add a new code module (.bas) and paste it there.

The easiest way to add a new code module is to right-click anywhere in the Project Explorer (Ctrl+R) and select Insert > Module.

The next problem, is that the procedure is Private. "Private" means that any code that must see and call this procedure, must be in the same scope - i.e. in the same module. Make it Public and you'll be able to call it from anywhere.

The next problem, is the parameters. From the implementation it looks like Sh is expected to be a Worksheet object, and Target is a Range object: to run the procedure you will have to supply the parameters.

There are many ways to do this.

One of them is to bring up the immediate pane (Ctrl+G) and call it directly:

Module1.Workbook_TwoWayMatch Sheet42, Sheet42.Range("Z123")

Of course the actual parameters you'll pass will vary depending on what you're trying to achieve, but I hope you get the idea.

0
0

The problem might be that UCase(Sh.Name) = "sheet1" on line 2 will never happen.

Try changing those to "SHEET1" and "SHEET2".

1
  • I tried that but it does hot have any effect.
    – raindelay
    Commented Dec 18, 2015 at 15:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .