我在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
如果有人能指出我正确的方向,那将非常感激。
答案 0 :(得分: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