我在嘗試構建一個腳本,該腳本可提取列(用戶定義的)的前6個字符,並插入一個新列並粘貼這些結果,或者只是將這些結果通過已存在的結果列(用戶的選擇),但我不斷收到一個對象定義錯誤(我用星號標記了代碼中的行)。Excel VBA腳本中的對象定義錯誤
誰能告訴我我做錯了什麼?這裏是我的代碼
Sub AAC_Extract()
Dim rng As Range, col As Range, arr
Dim sht As Worksheet, shet As Worksheet
On Error Resume Next
Set rng = Application.InputBox(_
Prompt:="Please select the column that contains the Standard Document Number. " & vbNewLine & _
" (e.g. Column A or Column B)", _
Title:="Select Document Number Range", Type:=8)
On Error GoTo 0
hdr = MsgBox("Does your selection contain a header?", vbYesNo + vbQuestion, "Header Option")
Set dest = Application.InputBox(_
Prompt:="Please select the column that you would the AAC to be placed in. " & vbNewLine & _
" (e.g. Column B or Column C)", _
Title:="Select Destination Range", Type:=8)
If dest Is Nothing Then Exit Sub
Set sht = dest.Worksheet
Set shet = rng.Worksheet
'If dest = rng Then
' MsgBox "Your Destination Range can not be the same as your Reference Range. Please choose a valid Destination Range", vbExclamation
' Exit Sub
'End If
On Error GoTo 0
yn = MsgBox("Do you want to insert a new column here?" & vbNewLine & _
"(Choosing 'No' will replace the current cells in your selected range." & vbNewLine & "All data in this range will be permanently deleted.)", vbYesNo + vbQuestion, "Destination Range Options")
LastRow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
Application.ScreenUpdating = False
If hdr = vbYes And yn = vbYes Then
dest.Select
With Selection
.EntireColumn.Insert
End With
Set col = sht.Range(sht.Cells(2, dest.Column), _
sht.Cells(sht.Rows.Count, dest.Column).End(xlUp))
Set cols = shet.Range(shet.Cells(2, rng.Column), _
shet.Cells(shet.Rows.Count, rng.Column).End(xlUp))
'Columns = cols.Column
'dest.EntireColumn.Insert
'col = dest.Column
'cols = rng.Column
'For i = 1 To LastRow
'Cells(i, col).Value = Left(Cells(i, cols), 6)
'Next i
'For Each c In col.Cells.Offset(0, -1) 'Offset due to the fact that dest moved when a column was inserted
' i = c.Row
' c.Value = Left(cols.Cells(i - 1), 6) 'Honestly, I'm not sure why I have to subtract 1 from i....i should be the same row as c
'Next c
With col
.Value2 = cols.Value2
.TextToColumns Destination:=.Cells, DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(6, 9))
End With
End If
End Sub
我沒有看到任何''哪條線給你的麻煩? – Brad
@布拉德對不起,關於這一點。我編輯腳本以顯示星號。除了我注意到的方式之外,我無法想象如何處理它。我想如果我能解決這部分問題,我可以爲其他3個布爾代碼編寫代碼。 –