如何在Variable中存储不同的整列?

时间:2017-07-18 10:16:14

标签: vba excel-vba compiler-errors excel

这是我到目前为止所拥有的:

    Set cell = Cells.Find(What:="ABC", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

    Set cell2 = Cells.Find(What:="DEF", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
    Set cell3 = Cells.Find(What:="GHI", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)


i = Union(cell.EntireColumn, cell2.EntireColumn, cell3.EntireColumn).Value

Do Until i = ""
Selection.TextToColumns Destination:=Selection.Cells(1, 1), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=True, _
Semicolon:=False, _
Comma:=False, _
Space:=False, _
Other:=False, _
FieldInfo:=Array(1, 1), _
TrailingMinusNumbers:=True
Loop

这里我想要做的是,我想存储列,然后我想进行格式化,直到设置列完成。 但我在下面的行上收到错误,因为运行时错误'13':类型不匹配。

i = Union(cell.EntireColumn, cell2.EntireColumn, cell3.EntireColumn).Value

请帮帮我。

2 个答案:

答案 0 :(得分:0)

正如内森已经说明当前行返回一系列值。

i = Union(cell.EntireColumn, cell2.EntireColumn, cell3.EntireColumn).Value

你试图通过创建 3 - 维数组来对抗它(我猜对了吗?)。

xx = Array(cell.EntireColumn, cell2.EntireColumn, cell3.EntireColumn)

有3个单独的列没有定义 3 维数组。你联合了3列,它产生了一个表,这意味着它仍然是一个 2 - 维数组(任何构造为x = range(****)。value返回 2 -dimensional array,即使您指定的范围包含一列)。 你要做的是循环遍历这个新的 2 - 维数组并检查它是否为空。

P.S。您可能希望一次格式化带有计算行数的范围联合(在每列中查找最后一个非空单元格),而不是循环遍历 2 - 维数组中的每个单元格。

答案 1 :(得分:0)

最后我能用下面的代码解决问题......

    Set cell = Cells.Find(What:="ABC", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

    Set cell2 = Cells.Find(What:="DEF", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
    Set cell3 = Cells.Find(What:="GHI", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

   x = Array(cell.Column, cell2.Column, cell3.Column)


   For Each item In x
      Cells(1, item).Select
      Selection.EntireColumn.Select

        Selection.EntireColumn.Select
        Selection.TextToColumns Destination:=Selection.Cells(1, 1), _
        DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, _
        ConsecutiveDelimiter:=False, _
        Tab:=True, _
        Semicolon:=False, _
        Comma:=False, _
        Space:=False, _
        Other:=False, _
        FieldInfo:=Array(1, 1), _
        TrailingMinusNumbers:=True
   Selection.NumberFormat = 0
   Next

实际上问题是当我定义列数组时,我发现cell, cell2, cell3值是错误的,它是捕获文本而不是列号.... 问题出在下面 前

x = Array(cell.EntireColumn, cell2.EntireColumn, cell3.EntireColumn)

之后

x = Array(cell.Column, cell2.Column, cell3.Column)

感谢您的所有努力。