[英]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 件事:
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:有几件事可能会有所帮助,因为我还没有看到更新以包括收到哪些错误:
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
);activesheet
instead of me
because it is tied to a sheet could lead to issues;我相信选择activesheet
而不是me
,因为它与工作表相关联可能会导致问题;target.count
as that can oftentimes lead to issues;添加了对target.count
的检查,因为这通常会导致问题;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.