文本到列vba

时间:2014-09-09 15:32:16

标签: excel vba excel-vba

如果使用某个标头,我试图拆分列。到目前为止,当我运行我的代码时,它找到列,向右添加新列,更改标题,然后将文本错误抛出到列部分。我使用了记录宏,然后稍微改了一下。

Sub CUSIPSymbol()
Dim acell As Range
Dim CCUSIP As Integer

Set acell = Sheets("Fixed Income").Range("A1:Z4").Find(What:="CUSIP/ Symbol", LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
If Not acell Is Nothing Then
CCUSIP = acell.Column
Cells(1, CCUSIP + 1).EntireColumn.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
Cells(2, CCUSIP).Value = "CUSIP"
Cells(2, CCUSIP + 1).Value = "Symbol"
Columns(CCUSIP).TextToColumns Destination:=Range(Cells(1, CCUSIP)), DataType:=xlFixedWidth, _
    FieldInfo:=Array(Array(0, 1), Array(9, 1)), TrailingMinusNumbers:=True
End If

End Sub

获得以下错误

Method 'Range' of object'_Global failed

语法有问题吗?

1 个答案:

答案 0 :(得分:0)

Sub CUSIPSymbol()
Dim acell As Range
Dim CCUSIP As Integer

Set acell = Sheets("Fixed Income").Range("A1:Z4").Find(What:="CUSIP/ Symbol", LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
If Not acell Is Nothing Then
CCUSIP = acell.Column
Cells(1, CCUSIP + 1).EntireColumn.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
Cells(2, CCUSIP).Value = "CUSIP"
Cells(2, CCUSIP + 1).Value = "Symbol"
Columns(CCUSIP).TextToColumns Destination:=Range(Cells(1, CCUSIP), Cells(100, CCUSIP)), DataType:=xlFixedWidth, _
    FieldInfo:=Array(Array(0, 1), Array(9, 1)), TrailingMinusNumbers:=True
End If

End Sub

根据Ann L.的建议,我在范围参考中添加了第二个细胞。我添加了一个远低于我期望遇到的东西,所以它会灵活。