【发布时间】:2015-05-01 04:25:47
【问题描述】:
我正在尝试构建一个脚本来提取列的前 6 个字符(用户定义),然后插入一个新列并粘贴这些结果,或者只是将这些结果粘贴到已经存在的列上(用户的选择),但我不断收到对象定义错误(我在代码中用星号标记了该行)。
谁能告诉我我做错了什么?这是我的代码
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 个布尔值编写代码。
标签: vba excel excel-2013