2011-04-15 109 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) 。新生產線將確保您只更換一個電池。