返回列表 上一主題 發帖

儲存各取得焦點後某一欄位變色

回復  coafort

新增程式碼到原有程式碼裡需要整理一下,請把整個工作表模組裡的程式碼貼上來
Andy2483 發表於 2024-2-2 14:17


Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
With Target
'↑以下是關於儲存格編輯觸發的程序
     If .Count > 1 Or .Item(1) = "" Then Exit Sub
     '↑如果觸發格數量大於 1 或觸發格的下方格是空白!就結束程式執行
     If Not Intersect([AR212:AR219,AX212:AX219,AD222,AF222], .Cells) Is Nothing Then
     '↑如果觸發格是在[A2:A2]儲存格裡??
        .Cells(1, 2) = Val(.Cells(1, 2)) + Val(.Value)
       '↑令觸發格的右邊那1格的值 + (觸發格值以Val 函數轉化回傳的數值)
        
        '觸發格本身是.Cells(1,1), 右側1格是.Cells(1,2), 左側1格是.Cells(1,0),
        '上方1格是.Cells(0,1), 下方1格是.Cells(2,1)
        
        .ClearContents
        '↑清除觸發格的內容
        '此清除的程序再次的觸發了,但是 .Item(1) = "" 所以結束程式執行
     End If
End With
End Sub

Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim a As Range, b As Range, c As Range
With Target
   Set b = [AG212:AG219,AF212:AF219]
   Set c = [AR212:AR219,AX212:AZ219]
   For Each a In b
      If a.Interior.ColorIndex = 6 Then a.Interior.ColorIndex = xlNone
   Next
   Set c = Intersect(.Cells, c)
   If Not c Is Nothing Then Intersect(c.EntireRow, b).Interior.ColorIndex = 6
End With
End Sub

謝謝大大
:D

TOP

回復  coafort

Option Explicit '←這是偵測所使用的變數有沒有做宣告,要放最上面
Dim Brr '←這是同模 ...
Andy2483 發表於 2024-2-2 15:52


報告大大,離開後黃色底不會消失
謝謝大大

TOP

回復  coafort

離開[AR212:AR219,AX212:AZ219] 這些範圍才會恢復原底色
Andy2483 發表於 2024-2-2 16:05


底色跑去AJ那邊去了
謝謝大大

TOP

回復 27# Andy2483

我說錯了
應該是說
黃底會累計不會消失
除非離開游標儲存格
謝謝大大

TOP

利用格式條件變色//保留原有格式

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ...
准提部林 發表於 2024-2-3 10:35


非常謝謝准提大大
非常謝謝安迪大大
下次改進:'(

TOP

利用格式條件變色//保留原有格式

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ...
准提部林 發表於 2024-2-3 10:35


其實是小的把欄位搞錯了
真的是非常抱歉
實際上應該是
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xA As Range, xB As Range, X As Range, R&
Set xA = [AR212:AR219,AX212:AX219]
Set xB = [AG212:AG219]
With Target
     If xB.FormatConditions.Count Then xB.FormatConditions.Delete
     If .Count > 1 Then Exit Sub
     Set X = Intersect(.Cells, xA)
     If X Is Nothing Then Exit Sub
     R = X.Row - xA.Item(1).Row + 1
     With xB.Item(R).FormatConditions
          .Add Type:=xlCellValue, Operator:=xlNotEqual, Formula1:="=""^_^"""
          .Item(1).Interior.ColorIndex = 6
     End With
End With
End Sub


   請問大大
如果AF212:AF219也要一起變色
該如何加入呢?
我改成以下這樣只有AF有作用但是AG沒作用
謝謝大大

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xA As Range, xB As Range, X As Range, R&
Set xA = [AR212:AR219,AX212:AX219]
Set xB = [AF212:AF219,AG212:AG219]
With Target
     If xB.FormatConditions.Count Then xB.FormatConditions.Delete
     If .Count > 1 Then Exit Sub
     Set X = Intersect(.Cells, xA)
     If X Is Nothing Then Exit Sub
     R = X.Row - xA.Item(1).Row + 1
     With xB.Item(R).FormatConditions
          .Add Type:=xlCellValue, Operator:=xlNotEqual, Formula1:="=""^_^"""
          .Item(1).Interior.ColorIndex = 6
     End With
End With
End Sub

TOP

回復  coafort

Set xB = [AF212:AG219]

With xB.Rows(R).FormatConditions
准提部林 發表於 2024-2-4 15:01


非常謝謝准大大
可以了:D

TOP

        靜思自在 : 慈悲沒有敵人,智慧不起煩惱。
返回列表 上一主題