简体   繁体   中英

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. 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:

  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".
  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)
  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.

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 );
  • I believe the choice of activesheet instead of me because it is tied to a sheet could lead to issues;
  • Added a check for target.count as that can oftentimes lead to issues;
  • 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 ;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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