VBA_事件01

'選區發生變化就調用某個方法--事件
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Call biaoshi
End Sub
 

'with的簡單使用
Sub withyuju()
With Sheet2
    .Range("A1") = 5
    .Range("A2") = 6
    .Range("A3") = 7    
End With
End Sub

 

'鼠標點擊某行標識顏色
Sub biaoshi()
Dim colors As Long
colors = Application.RandBetween(1, 99999)
Cells.Interior.Pattern = xlNone '清除所有表的填充色
Selection.EntireRow.Interior.color = colors '標識顏色
End Sub

'單元格值發生變化-事件
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False '關閉事件響應
Call shaixuanshijian
Application.EnableEvents = True  '打開事件響應
End Sub

Sub shaixuanshijian()
Dim irow As Integer
irow = Range("a56433").End(xlUp).Row
Range("L1:q" & irow).ClearContents
Range("A1:F" & irow).AutoFilter Field:=4, Criteria1:=Range("i2")
Range("A1:F" & irow).Copy Range("k1")
Range("A1:F" & irow).AutoFilter
End Sub

'點擊工作表-事件
Private Sub Worksheet_Activate()
Call gengxin
End Sub

Sub gengxin()
ActiveWorkbook.RefreshAll '更新數據
End Sub
 

'工作薄保存前-事件
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Call fuzhibaocunbook
End Sub

'複製表
Sub fuzhibaocunbook()
ThisWorkbook.SaveCopyAs "D:\VBA\備份" & Format(Now(), "yyyymmddhhmmss") & ".xlsx"  '設置當期日期格式
End Sub

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章