2011-04-15 42 views
1

我已经添加了一个VBA代码给我的Excel,它所做的是自动写入其余的号码(例如:当我写2并点击输入它将它上面的数字,并填写990112 [查看图])Excel VBA代码错误,当使用数量增量(CTRL拖动)

当每个号码键入这工作得很好,但是当我使用自动增量(CTRL + Drag)它抛出一个错误

这是我的VBA代码

Private Sub Worksheet_Change(ByVal Target As Range) 
Dim oldText As String, aboveText As String, 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 

Excel File

回答

1

变化

If Target.Column = 3 Then 

If Target.Column = 3 And Target.Cells.Count = 1 Then 

你正在试图获得多小区范围的Text属性(在这种情况下 “目标” 是C6:C7) 。新生产线将确保您只更换一个电池。