在Excel中,如果儲存格的內容等於某個值,則如何自動變更它

在Excel中,如果儲存格的內容等於某個值,則如何自動變更它

我目前正在處理與一組值相對應的一列資料(數字),我想知道如何自動將資料/數字替換為正確的對應值? (下面提供了我的意思的圖片)感謝您的幫助!

起始數據

在此輸入影像描述

最後結果

在此輸入影像描述

答案1

在工作表變更事件中,您可以執行類似的操作。它假設您要輸入的列是 A,並且根據您的範例,您的查找佈局位於 K&J 中。您可以根據需要調整範圍。

Private Sub Worksheet_Change(ByVal Target As Range)
  'should check you are doing entry in column A first
If Target.Column = 1 Then
 Application.EnableEvents = False
 Dim R1 As Range
 Dim R2 As Range
 Dim rngStart As Range
 Dim varFind As Variant
 Dim InRange As Boolean
   Set R1 = Range(Target.Address)
    Set R2 = Range("J:J")
      Set rngStart = Range("J1")

   If R2.Find(What:=Target, After:=rngStart, LookIn:=xlValues, LookAt:=xlWhole _
    , SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) Is Nothing Then

     Application.EnableEvents = True
        Set R1 = Nothing
        Set R2 = Nothing

     Exit Sub
   Else
    varFind = R2.Find(What:=Target, After:=rngStart, LookIn:=xlValues, LookAt:=xlWhole _
    , SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Address

      Set varFind = Range(varFind)
        R1.Value = varFind.Offset(0, -1)
   Application.EnableEvents = True
      Set R1 = Nothing
      Set R2 = Nothing
      Set rngStart = Nothing
      Set varFind = Nothing
   End If
  Else
    Exit Sub
 End If
End Sub

相關內容