2017-07-26 54 views
1

我有這段代碼,它工作得很好。 唯一的問題是,我按下輸入後,例如在單元格「A2」中,而不是像往常一樣向下移動到單元格「A3」 - 它移動到單元格「E3」,所以它使得用戶難以類型。如何在使用Worksheet_Change時避免excel中單元格的移動事件

有什麼建議嗎?

Private Sub Worksheet_change(ByVal Target As Range) 

Application.EnableEvents = False 

Range("A2:M2").Interior.ColorIndex = 19 
Dim LASTROW As Long 
TheLastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row 

Dim intx As Variant 
For i = 2 To TheLastRow 
    If Range("a" & i) = Range("a" & i + 1) Then 
     Range("A" & i + 1 & ":n" & i + 1).Interior.Color = Range("a" & i).Interior.Color 
     intx = intx + 0 
    Else 
     Range("A" & i + 1 & ":n" & i + 1).Interior.ColorIndex = 46 - intx 
    intx = intx + 1 
    End If 
Next i 

For i = 2 To TheLastRow 
    Range("e" & i).Select 
    ActiveCell.FormulaR1C1 = _ 
      "=IF(ISBLANK(RC[-1]),"""",HYPERLINK(""PCDOCS://PCDOCS_JLM/""&RC[-1]&""/R"",""link""))" 
Next i 
Application.EnableEvents = True 

End Sub  

回答

1

您應該避免使用選擇或啓用VBA,所以:

Private Sub Worksheet_change(ByVal Target As Range) 

Application.EnableEvents = False 

Range("A2:M2").Interior.ColorIndex = 19 
Dim LASTROW As Long 
TheLastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row 

Dim intx As Variant 
For i = 2 To TheLastRow 
    If Range("a" & i) = Range("a" & i + 1) Then 
     Range("A" & i + 1 & ":n" & i + 1).Interior.Color = Range("a" & i).Interior.Color 
     intx = intx + 0 
    Else 
     Range("A" & i + 1 & ":n" & i + 1).Interior.ColorIndex = 46 - intx 
    intx = intx + 1 
    End If 
Next i 

For i = 2 To TheLastRow 
    Range("e" & i).FormulaR1C1 = _ 
      "=IF(ISBLANK(RC[-1]),"""",HYPERLINK(""PCDOCS://PCDOCS_JLM/""&RC[-1]&""/R"",""link""))" 
Next i 
Application.EnableEvents = True 

End Sub  
+0

的偉大工程,謝謝! – ADIIDA

1

我做了一些修改你的代碼,當我按下{enter}細胞「A2」它執行的代碼和「跳躍」到細胞「A3」。

代碼

Option Explicit 

Private Sub Worksheet_change(ByVal Target As Range) 

Dim C As Range 
Dim intx As Long 

Application.EnableEvents = False 

Range("A2:M2").Interior.ColorIndex = 19 

' loop through all cells with data in column "A" 
For Each C In Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row) 
    If C.Value = C.Offset(1, 0).Value Then 
     C.Offset(1, 0).Resize(1, 14).Interior.Color = C.Interior.Color 
    Else 
     C.Offset(1, 0).Resize(1, 14).Interior.Color = 46 - intx 
     intx = intx + 1 
    End If 
Next C 

' loop through all cells with data in column "E" 
For Each C In Range("E2:E" & Cells(Rows.Count, "E").End(xlUp).Row) 
    C.FormulaR1C1 = "=IF(ISBLANK(RC[-1]),"""",HYPERLINK(""PCDOCS://PCDOCS_JLM/""&RC[-1]&""/R"",""link""))" 
Next C 

Application.EnableEvents = True 

End Sub 
0

可以讀出從小區是Trigert事件的地址和保存他們。 完成代碼後,您可以選擇單元格,下面的1行。 希望這有助於。

私人小組Worksheet_change(BYVAL目標作爲範圍)

Application.EnableEvents = False 


    Dim rngAddress As String 
    rngAddress = Target.Address 

    Range("A2:M2").Interior.ColorIndex = 19 
    Dim LASTROW As Long 
    TheLastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row 

    Dim intx As Variant 
    For i = 2 To TheLastRow 
     If Range("a" & i) = Range("a" & i + 1) Then 
      Range("A" & i + 1 & ":n" & i + 1).Interior.Color = Range("a" & i).Interior.Color 
      intx = intx + 0 
     Else 
      Range("A" & i + 1 & ":n" & i + 1).Interior.ColorIndex = 46 - intx 
     intx = intx + 1 
     End If 
    Next i 

    For i = 2 To TheLastRow 
     Range("e" & i).Select 
     ActiveCell.FormulaR1C1 = _ 
       "=IF(ISBLANK(RC[-1]),"""",HYPERLINK(""PCDOCS://PCDOCS_JLM/""&RC[-1]&""/R"",""link""))" 
    Next i 

Range(rngAddress).offset(1,0).select 

    Application.EnableEvents = True 

    End Sub 
相關問題