vba侦听Execl单元格修改(Worksheet_Change)
Private Sub Worksheet_Change(ByVal Target As Range) Dim str As String str = Cells(1, Target.Column).Value If (str = "订单编号") Then Application.EnableEvents = False Cells(Target.Row, Target.Column).Value = WorksheetFunction.Proper(Cells(Target.Row, Target.Column).Value) Application.EnableEvents = True End If End Sub
注:
1)先用 Application.EnableEvents 关闭页面侦听事件,防止无限循环侦听
2)需要调用execl自带函数,使用WorksheetFunction.Proper(),Proper函数:将英文首字母变成大写
Private Sub Worksheet_Change(ByVal Target As Range) Dim str As String str = Cells(1, Target.Column).Value If (str = "订单编号") Then Application.EnableEvents = False Cells(Target.Row, Target.Column).Value = WorksheetFunction.Proper(Cells(Target.Row, Target.Column).Value) Application.EnableEvents = True End If End Sub