0

我在 Access 中有此代码来打开和填充 Excel 报告。它在第一次运行时运行良好,但如果我再次为不同的组运行它,它会给我运行时错误 91: Object variable or With block variable not set

wb.Worksheets(1).Range("A" & i & ":g" & i).Select
wb.Worksheets(1).Range("g" & i).Activate
With wb.Worksheets(1).Range("g" & i)
    Selection.Font.size = 13
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    Selection.Borders(xlInsideVertical).LineStyle = xlNone
    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
End With

该错误当前在第四行抛出,“Selection.font.size=13。我通常不在 excel 中编程,所以我正在尝试一些可能没有意义的事情:

wb.Worksheets(1).Range("A" & i & ":g" & i).Selection.Font.size = 13
wb.Worksheets(1).Selection.Font.size = 13

如果有人能指出我正确的方向,将不胜感激。

4

1 回答 1

0

就像 Sid 所说,您的WITH陈述与您的陈述不匹配SELECT,如果您不是绝对需要,请不要使用 select。此外,缩短,您的代码可能如下所示:

With wb.Worksheets(1).Range("g" & i)
.Font.size = 13
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
End With
With wb.Worksheets(1).Range("g" & i).Borders
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
End With
于 2013-11-05T18:27:00.577 回答