Excel VBA-以前运行的代码在新工作表上不起作用

Excel VBA-以前运行的代码在新工作表上不起作用,excel,vba,excel-formula,Excel,Vba,Excel Formula,这个有点罗嗦,所以请容忍我 在这个新的Excel程序中,在单独的表格中有几个主表。我将重点介绍两位相关大师: Die Master ---------------------------------------- | Die No | Desc | Preven. Stroke | PIC | ======================================== | D00001 | Test | 1000000 | Me | --------------------

这个有点罗嗦,所以请容忍我

在这个新的Excel程序中,在单独的表格中有几个主表。我将重点介绍两位相关大师:

Die Master
----------------------------------------
| Die No | Desc | Preven. Stroke | PIC |
========================================
| D00001 | Test |    1000000     | Me  |
----------------------------------------

Model Master
----------------------
| Model | Model Name | 
======================
| M0001 |   Model 1  |
----------------------
然后在新的工作表中,
项目条目
在表中有
模具编号
型号

Project Registration
------------------------------------------------------------------
| Project No | Machine | Die No | Die Desc | Model | Model Name  |
==================================================================
|   P00001   |   A00   | D00001 |   Test   | M0001 |   Model 1   |
------------------------------------------------------------------ 
Die Desc
Model Name
分别使用
Die No
Model
中的值自动填写。因为我不希望公式被篡改,所以我将把它放在VBA代码中:

Private Sub worksheet_change(ByVal target As Range)

'Autofill Die Description after Die No is filled
If Not Intersect(target, Me.ListObjects("D.Entry").ListColumns("Die No").DataBodyRange) Is Nothing Then

 With target.Offset(0, 1) 'Die Desc
    .FormulaR1C1 = "=IFerror(INDEX(B.Die,MATCH(rc[-1],B.Die[Die No],0),2),"""")"
    .Value = .Value
 End With

End If

'Autofill Model Name after Model is filled
If Not Intersect(target, Me.ListObjects("D.Entry").ListColumns("Model").DataBodyRange) Is Nothing Then

 With target.Offset(0, 1)
    .FormulaR1C1 = "=IFerror(INDEX(C.Model,MATCH(rc[-1],C.Model[Model],0),2),"""")"
    .Value = .Value
 End With
End If
代码有效,我可以继续下一页,
数据输入
,我计划使用与“项目输入”中相同的代码,这次将其用作参考表

Data Entry
-----------------------------------------------------------------
| Project No | Machine | Die No | Die Desc | Model | Model Name |
=================================================================
|   P00001   |         |        |          |       |            |
-----------------------------------------------------------------
Machine
列开始,我使用了与前面类似的代码,如下所示:

Private Sub worksheet_change1(ByVal target1 As Range)

'If Project No is not empty then do the following
If Not Intersect(target1, Me.ListObjects("F.Main").ListColumns("Project No").DataBodyRange) Is Nothing Then

 With target1.Offset(0, 1) 
    .FormulaR1C1 = "=IFerror(INDEX(D.Entry,MATCH(rc[-1],D.Entry[Machine],0),2),"""")"
    .Value = .Value
 End With
End If

但是,在测试时,机器列不会显示任何内容。即使更改
rc
值也不会改变任何内容。一个类似的代码怎么能在一张工作表上工作,却突然无法在下一张工作表上工作?

我猜您的
工作表\u change1
是您创建的?我很确定这不是保留的,这意味着它可能永远不会运行你的宏。您需要将代码移动到相应的工作表
数据输入
,其名称与
工作表\u Change

如果您仍然有问题,下面的代码可能会帮助您调试它。仔细检查您的代码,您会发现:

Private Sub worksheet_change(ByVal target As Range)

MsgBox "Your macro is running where it's supposed to."
Stop


'If Project No is not empty then do the following
    If Not Intersect(target1, Me.ListObjects("F.Main").ListColumns("Project No").DataBodyRange) Is Nothing Then

    With target1.Offset(0, 1)
    .FormulaR1C1 = "=IFerror(INDEX(D.Entry,MATCH(rc[-1],D.Entry[Machine],0),2),"""")"
    .Value = .Value
    End With

    Else
        MsgBox "Couldn't find what you were looking for"

    End If
End Sub

工作表\u change1
没有具有该名称的事件。您只能有一个
工作表。在工作表中更改
事件,必须将其放置在工作表代码区域中。此外,它被写为
私有子工作表\u Change(ByVal目标为Excel.Range)
您不能更改它。