【发布时间】:2022-11-07 03:01:44
【问题描述】:
我一直在尝试使用 VBA 格式化不同行中的单元格,一个带有数字,另一个带有日期。我的代码如下。但是第二个事件没有被触发。当我将 1s 和 2nd 事件上下互换时(日期第一和数字第二),日期格式有效,数字无效。请问我可以在这里得到任何帮助吗?
*Private Sub Worksheet_Change(ByVal Target As Range)
'___________ 8 DIGITS FORMAT ____________________
Dim i As Integer
Dim cell As Integer
Application.EnableEvents = False
On Error GoTo Err 'To avoid error when multiple cells are selected
If Not Intersect(Target, Range("U:U")) Is Nothing Or _
Not Intersect(Target, Range("B:B")) And Target.Value <> "" Then
cell = Target.Rows.Count
For i = 1 To cell
'To avoid cells with NO VALUE to be FORMATTED
If Target.Cells(i, 1).Value <> "" Then
Target.Cells(i, 1).NumberFormat = "@"
Target.Cells(i, 1).Value = Application.WorksheetFunction.Text(Target.Cells(i, 1).Value, "00000000")
Else
Resume LetsContinue
End If
Next i
End If
Application.EnableEvents = True
'______________________ Date Format ____________________
Dim x As Integer
Dim dt As Integer
Application.EnableEvents = False
On Error GoTo Err2 'To avoid error when multiple cells are selected
If Not Intersect(Target, Range("E:E")) Is Nothing Or _
Not Intersect(Target, Range("AQ:AQ")) And Target.Value <> "" Then
dt = Target.Rows.Count
For x = 1 To dt
'To avoid cells with NO VALUE to be FORMATTED
If Target.Cells(x, 1).Value <> "" Then
Target.Cells(x, 1).NumberFormat = "dd-Mmm-yyyy"
Target.Cells(x, 1).Value = Application.WorksheetFunction.Text(Target.Cells(x, 1).Value, "dd-Mmm-yyyy")
Else
Resume LetsContinue
End If
Next x
Else
End If
Application.EnableEvents = True
Err:
If Not Intersect(Target, Range("U:U")) Is Nothing Or Not Intersect(Target, Range("B:B")) Is Nothing Then
Resume Next
Else
Resume LetsContinue
End If
Err2:
If Not Intersect(Target, Range("E:E")) Is Nothing Or Not Intersect(Target, Range("AQ:AQ")) Is Nothing Then
Resume Next
Else
Resume LetsContinue
End If
LetsContinue:
Application.ScreenUpdating = True
Application.EnableEvents = True
Exit Sub
Whoa:
Resume LetsContinue
End Sub*
【问题讨论】: