简体   繁体   English

根据另一个单元格更改更新表中的值

[英]Update values in a table based on another cell change

I'm building an Excel file that imports data from another Excel file and populates a table.我正在构建一个 Excel 文件,该文件从另一个 Excel 文件导入数据并填充一个表。 I have this functionality working but I'm now trying to have cells update based on entries being changed in another column.我有这个功能,但我现在正试图根据另一列中更改的条目来更新单元格。 Specifically, it needs to do 3 things:具体来说,它需要做 3 件事:

  1. when a user selects "remove" from a drop down list in one column (12), it will automatically change the value in a cell 2 positions to the left (column 10) to "No".当用户从一列 (12) 的下拉列表中选择“删除”时,它会自动将左侧 2 位单元格中的值(第 10 列)更改为“否”。
  2. When the cell in column 10 is set to "No" it automatically changes the cell directly to the right (column 11) to 0 (This can be done independent of selecting "remove" in column 12)当第 10 列中的单元格设置为“否”时,它会自动将单元格直接向右(第 11 列)更改为 0(这可以独立于在第 12 列中选择“删除”来完成)
  3. When the cell in column 10 is set to "Yes" it will change the "Remove" selection back to blank and run a script that recalculates the adjacent column 11.当第 10 列中的单元格设置为“是”时,它会将“删除”选项更改回空白并运行重新计算相邻列 11 的脚本。

I got it to work for a minute but then started getting type errors or just not working at all.我让它工作了一分钟,然后开始出现类型错误或根本不工作。 The following script is what I have developed.以下脚本是我开发的。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, ActiveSheet.ListObjects("GraphicTable").ListColumns(12).DataBodyRange) Is Nothing Then
        If Target.Value = "Remove" Then
               Target.Offset(0, -2).Value = "No"
        End If
    End If
    
    If Not Intersect(Target, ActiveSheet.ListObjects("GraphicTable").ListColumns(10).DataBodyRange) Is Nothing Then
            If Target.Value = "No" Then
                Target.Offset(0, 1).Value = 0
            Else
                Target.Offset(0, 2).Value = ""
                Call HoursCalc
            End If
    End If
End Sub

Several things that may help, as I haven't seen update to include which errors are being received:有几件事可能会有所帮助,因为我还没有看到更新以包括收到哪些错误:

  • I use select when I'm combining my change events, rather than separate if statements so a single-pass check occurs (could similarly use else-if );当我组合我的更改事件时,我使用select ,而不是单独的if语句,因此会发生单遍检查(同样可以使用else-if );
  • I believe the choice of activesheet instead of me because it is tied to a sheet could lead to issues;我相信选择activesheet而不是me ,因为它与工作表相关联可能会导致问题;
  • Added a check for target.count as that can oftentimes lead to issues;添加了对target.count的检查,因为这通常会导致问题;
  • You're calling a separate function, and you haven't specified where that is located... i specified application.run which will run the subroutine even if it is private ;您正在调用一个单独的 function,并且您没有指定它的位置...我指定了application.run它将运行子程序,即使它是private的;

Untested:未经测试:

if target.count > 1 then exit sub
with target
    select case true
        case not intersect(target, Me.ListObjects("GraphicTable").ListColumns(10).DataBodyRange) is nothing
            if .value = "Remove" then .offset(,-2).value = "No"
        case not intersect(target, Me.ListObjects("GraphicTable").ListColumns(10).DataBodyRange) is nothing
            if .value = "No" then 
                .offset(,1).value = 0
            else
                .offset(,2).clearcontents
                application.run "module1.HoursCalc"
            end if
    end select
end with

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM