使用.LineStyle和.Weight-VBA-格式化错误

时间:2017-05-29 13:24:40

标签: excel vba excel-vba

我正在运行此代码来修复我的工作簿中的fomatting,它似乎在某些情况下正常工作,但在其他一些情况下它停止了" 1004错误" .Linestyle或.Weight.Anyone中的msg对这里可能出现的问题有任何建议吗?非常感谢!

'适合列的宽度&修复格式

Columns("A:D").AutoFit`

With Sheets("Sheet1")`
    .Cells.Font.Size = 8`
End With`

Set rng1 = Columns("A:F")`
Set rng2 = Rows("1:10")`

With rng1.Borders`
    .LineStyle = xlContinuous`
    .Weight = xlHairline`
End With`

With rng2.Borders`
    .LineStyle = xlNone`
End With`

1 个答案:

答案 0 :(得分:0)

将范围设置为 With Sheets("Sheet1")... End With

With Sheets("Sheet1")
    .Columns("A:D").AutoFit
    .Cells.Font.Size = 8
    Set rng1 = .Columns("A:F")
    Set rng2 = .Rows("1:10")
End With

With rng1.Borders
    .LineStyle = xlContinuous
    .Weight = xlHairline
End With`

With rng2.Borders
    .LineStyle = xlNone
End With

当您退出With ... End With时,您丢失了父工作表关联。