简体   繁体   中英

Data clearing when using Target to update range

I have the following macro

Dim rng As Range
Set rng = Target.Parent.Range("D31")
If Target.Address <> rng.Address Then Exit Sub
Select Case rng.Value
        Case "N/A": Result = "N/A"
        Case "NA": Result = "N/A"
        Case "": Result = ""
End Select
Range("E31, G31, H31, I31, J31, K31") = Result
End Sub

When I initially type a value in D31 that is not NA and then type a value in E31, I see the data in E31. But if I update D31 (ie original value red, new vale read and blue), the value in E31 is erased. Is there a way to stop this from happening?

  1. I put Range("E31, G31, H31, I31, J31, K31") = Result inside a condition
    If Target.Value = "" Or Result <> "" .
  2. Also, removed rng variable which seems not needed.
If Target <> Range("D31") Then Exit Sub
Select Case Target.Value
        Case "N/A": Result = "N/A"
        Case "NA": Result = "N/A"
        Case "": Result = ""
End Select
If Target.Value = "" Or Result <> "" Then
  Range("E31, G31, H31, I31, J31, K31") = Result
End If

Now values E31, G31, H31, I31, J31, K31 only change when you input "N/A", "NA" or "" into D31 .

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