I have added a VBA code to my excel, what it does is automatically writes the rest of the number (For eg: When I write 2
and hit enter it will take the number above it and fill it to 990112
[see img])
This works fine when every number is typed, but When I use Auto Increment (CTRL + Drag)
it throws an error
This is my VBA Code
Private Sub Worksheet_Change(ByVal Target As Range)
Dim oldText As String, aboveText As St开发者_运维百科ring, newText As String
If Target.Column = 3 Then
oldText = Target.Text
aboveText = Target.Cells(0, 1).Text
If aboveText <> "DESIGN" Or Target.Text <> "" Then
If Len(aboveText) = 6 And Len(oldText) < 6 And Len(oldText) >= 1 Then
Application.EnableEvents = False
newText = Left(aboveText, 6 - Len(oldText)) + oldText
Target.Value = newText
Application.EnableEvents = True
End If
End If
End If
End Sub
Change
If Target.Column = 3 Then
to
If Target.Column = 3 And Target.Cells.Count = 1 Then
You're trying to get the Text property of a multi-cell range ("Target" in that situation is C6:C7). The new line will ensure that you are only changing one cell.
精彩评论