Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
On Error Resume Next
If Target.Count > 1 Then Exit Sub
Cells.FormatConditions.Delete
With Target.EntireColumn.FormatConditions '欄變色
.Delete
.Add xlExpression, , "TRUE"
.Item(1).Interior.ColorIndex = Int(28)
End With
With Target.EntireRow.FormatConditions '列變色
.Delete
.Add xlExpression, , "TRUE"
.Item(1).Interior.ColorIndex = Int(35)
End With
With Target.FormatConditions '單元格變色
.Delete
.Add xlExpression, , "TRUE"
.Item(1).Interior.ColorIndex = Int(4)
End With
End Sub
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With Target
Dim xR As Range, xA As Range
If .Columns.Count = Columns.CountLarge Then Exit Sub
If .Count > 1 Or .Column = 1 Then Exit Sub
Set xR = .Cells: Set xA = Range([B1], Cells(1, Columns.Count)).EntireColumn
With Intersect(ActiveSheet.UsedRange, xA)
.Interior.ColorIndex = xlNone
Intersect(xR.EntireRow, .Cells).Interior.ColorIndex = 6
Intersect(xR.EntireColumn, .Cells).Interior.ColorIndex = 6
End With
.Interior.ColorIndex = 4
End With
End Sub作者: cowww 時間: 2023-5-4 08:53
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With Target
Dim xR As Range, xA As Range, xB As Range
If .Columns.Count = Columns.CountLarge Then Exit Sub
If .Count > 1 Or .Column = 1 Or .Row = 1 Then Exit Sub
Set xR = .Cells: Set xA = Range([B1], Cells(1, Columns.Count)).EntireColumn Set xB = Range([A2], Cells(Rows.Count, 1)).EntireRow
With Intersect(ActiveSheet.UsedRange, xA, xB)
.Interior.ColorIndex = xlNone
Intersect(xR.EntireRow, .Cells).Interior.ColorIndex = 6
Intersect(xR.EntireColumn, .Cells).Interior.ColorIndex = 6
End With
.Interior.ColorIndex = 4
End With
End Sub作者: cowww 時間: 2023-5-4 10:15
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With Target
Dim xR As Range, xA As Range, xB As Range
If .Columns.Count = Columns.CountLarge Then Exit Sub
If .Count > 1 Or .Column = 1 Or .Row < 5 Then Exit Sub
Set xR = .Cells: Set xA = Range([B1], Cells(1, Columns.Count)).EntireColumn
Set xB = Range([A5], Cells(Rows.Count, 1)).EntireRow
With Intersect(ActiveSheet.UsedRange, xA, xB)
.Interior.ColorIndex = xlNone
Intersect(xR.EntireRow, .Cells).Interior.ColorIndex = 6
Intersect(xR.EntireColumn, .Cells).Interior.ColorIndex = 6
End With
.Interior.ColorIndex = 4
End With
End Sub作者: cowww 時間: 2023-5-4 10:49
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With Target
'↑以下是關於觸發的程序
Dim xR As Range, xA As Range, xB As Range
'↑宣告(xR,xA,xB)是 儲存格變數
If .Columns.Count = Columns.CountLarge Then Exit Sub
'↑如果觸發欄數是 最大欄數!就結束程式執行
If .Count > 1 Or .Column = 1 Or .Row < 5 Then Exit Sub
'↑如果觸發格數大於1 或 觸發欄位數是1(A欄) 或觸發列號小於 5!就結束程式執行
Set xR = .Cells: Set xA = Range([B1], Cells(1, Columns.Count)).EntireColumn
'↑令xR這儲存格變數是 觸發的儲存格
'↑令xA這儲存格變數是 ([B1]到 第1列最後欄範圍存格)所在欄的全部儲存格
Set xB = Range([A5], Cells(Rows.Count, 1)).EntireRow
'↑令xB這儲存格變數是 ([A5]到 A欄最後列範圍存格)所在列的全部儲存格
With Intersect(ActiveSheet.UsedRange, xA, xB)
'↑以下是關於三個儲存格區域交集後的儲存格區域 程序
'1.本表有使用的儲存格 :2.xA變數 :3.xB變數
.Interior.ColorIndex = xlNone
'↑令此區域儲存格底色是 無色
Intersect(xR.EntireRow, .Cells).Interior.ColorIndex = 6
'↑令觸發格所在此區域儲存格列底色是 黃色
Intersect(xR.EntireColumn, .Cells).Interior.ColorIndex = 6
'↑令觸發格所在此區域儲存格欄底色是 黃色
End With
.Interior.ColorIndex = 4
'↑令觸發格底色是 綠色
End With
End Sub作者: cowww 時間: 2023-5-4 12:47
補充//為了防止選取區不在使用範圍內的錯誤, 修改如下
Set X = Intersect(Selection.EntireRow, ActiveSheet.UsedRange)
If X Is Nothing Then Exit Sub
Set Y = Intersect(Selection.EntireColumn, ActiveSheet.UsedRange)
If Y Is Nothing Then Exit Sub作者: Andy2483 時間: 2023-5-4 13:52
Option Explicit
Sub 命名選取的圖片_名稱()
Selection.ShapeRange.Name = "矩形_橫"
End Sub
Sub 選取的圖片_名稱()
MsgBox Selection.ShapeRange.Name
End Sub作者: shuo1125 時間: 2023-5-4 13:57
我把語法改成這樣
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With Target
Dim xR As Range, xA As Range, xB As Range
If .Columns.Count = Columns.CountLarge Then Exit Sub
If .Count > 1 Or .Column = 1 Or .Row < 5 Then Exit Sub
Set xR = .Cells: Set xA = Range([B1], Cells(1, Columns.Count)).EntireColumn
Set xB = Range([A5], Cells(Rows.Count, 1)).EntireRow
With Intersect(ActiveSheet.UsedRange, xA, xB)
.Interior.ColorIndex = xlNone
If Range("F2") = "N" Then
Intersect(xR.EntireRow, .Cells).Interior.ColorIndex = 0
Intersect(xR.EntireColumn, .Cells).Interior.ColorIndex = 0
.Interior.ColorIndex = 0
Else
Intersect(xR.EntireRow, .Cells).Interior.ColorIndex = 6
Intersect(xR.EntireColumn, .Cells).Interior.ColorIndex = 6
End If
End With
.Interior.ColorIndex = 4
End With
End Sub