Private Sub Worksheet_Change(ByVal Target As Range)
If Len(Target) = 8 Then
Exit Sub
End If
If Range("AF1:AF1000").Value = "Q" Then
Exit Sub
End If
On Error Resume Next
Application.EnableEvents = False
If Target.Column = 30 Then
lTarget = Len(Target)
Target.NumberFormat = "@"
For i = 1 To 8 - lTarget
Target.Value = "0" & Target.Value
Next
End If
Application.EnableEvents = True
End Sub
我在尝试将信息放入某个单元格时基本上是这样的,如果你把任何少于8位数字放入其中,它会在它前面加上'0直到它是8,但是现在我想要如果某个单元格中有文本“Q”,那么它就不会适应它,当我这样做时它不能正常工作,几乎没什么帮助?
编辑:为了让整个自动对焦更容易......它的整个自动对焦列不仅仅是1到1000,所以我如何改变它到目前为止...可能需要重写整个事情..lol :(答案 0 :(得分:0)
如果您的意思是它应检查Q的同一行列 ,请尝试以下操作:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rCell As Range
If Not Intersect(Target, Me.Columns(30)) Is Nothing Then
On Error Resume Next
Application.EnableEvents = False
For Each rCell In Intersect(Target, Me.Columns(30)).Cells
Select Case Len(rCell.Value)
Case 0, 8
' ignore if 8 characters or blank
Case Else
If Strings.UCase$(Cells(rCell.Row, "AF").Value) <> "Q" Then
rCell.NumberFormat = "@"
rCell.Value = VBA.Right$("0000000" & rCell.Value, 8)
End If
End Select
Next rCell
End If
Application.EnableEvents = True
End Sub