参考无效

时间:2015-07-09 19:25:45

标签: excel vba excel-vba

我在VBA中编写了一个宏,但面临两个问题:

  1. 我不断收到reference is not valid错误。
  2. 合并单元格的水平对齐不起作用。
  3. 这是子:

    Sub test(numCell As Integer)
        Dim rowNum As Integer
        Dim colNum As Integer
        rowNum = ActiveCell.Row
        colNum = ActiveCell.Column
        With Range(Cells(rowNum, colNum), Cells(rowNum, colNum + numCell - 1))
            .Merge (Across)
            .Interior.Color = 200
            .BorderAround LineStyle:=xlContinuous
            .BorderAround Color:=1
            .Borders(xlEdgeBottom).Color = 1
            .Borders(xlEdgeTop).Color = 1
            .Borders(xlEdgeLeft).Color = 1
            .Borders(xlEdgeRight).Color = 1
            .Borders.Weight = xlThick
            .Value = Str(numCell)
            .VerticalAlignment = xlCenterAcrossSelection
            .HorizontalAlignment = xlCenterAcrossSelection
        End With
    End Sub
    

1 个答案:

答案 0 :(得分:0)

这些行修正了两个错误:.Merge.VerticalAlignment = xlVAlignCenter。我想我没有改变其他任何东西,代码也有效。

Sub test(numCell As Integer)
    Dim rowNum As Integer
    Dim colNum As Integer
    rowNum = ActiveCell.Row
    colNum = ActiveCell.Column
    With Range(Cells(rowNum, colNum), Cells(rowNum, colNum + numCell - 1))
        .Merge
        .Interior.Color = 200
        .BorderAround LineStyle:=xlContinuous
        .BorderAround Color:=1
        .Borders(xlEdgeBottom).Color = 1
        .Borders(xlEdgeTop).Color = 1
        .Borders(xlEdgeLeft).Color = 1
        .Borders(xlEdgeRight).Color = 1
        .Borders.Weight = xlThick
        .Value = Str(numCell)
        .VerticalAlignment = xlVAlignCenter
        .HorizontalAlignment = xlCenterAcrossSelection
    End With
End Sub
相关问题