How to avoid using .Select, .Activate, ActiveSheet,ActiveCell in my specific vba code?

后端 未结 3 1941
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 10:49

I have this code that obviously use Select, .Activate,...and I understand it\'s not a good practice in addition the application is craching now and then so thats probably be

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-20 11:10

    The below is a shortened-up version of your code:

    Sub FormatText()
    
        With Sheets("A4").Cells(1 + PageRowOffset(Page) + BoxRowOffset(Box) - 2, BoxColOffset(Box)).Font
            .Name = "Calibri"
            .Size = 11
            .Underline = xlUnderlineStyleNone
            .ThemeColor = xlThemeColorLight1
            .ThemeFont = xlThemeFontMinor
        End With
    
    
    
        With Range(Cells(PageRowOffset(Page) + BoxRowOffset(Box), 1 + BoxColOffset(Box)), Cells(PageRowOffset(Page) + BoxRowOffset(Box) + 3, 1 + BoxColOffset(Box) + 1)).Font
            .Name = "Calibri"
            .Size = 8
            .Underline = xlUnderlineStyleNone
            .ThemeFont = xlThemeFontMinor
        End With
    
        With Range(Cells(PageRowOffset(Page) + BoxRowOffset(Box) + 4, 1 + BoxColOffset(Box)), Cells(PageRowOffset(Page) + BoxRowOffset(Box) + 7, 1 + BoxColOffset(Box) + 1)).Font
            .Name = "Calibri"
            .Size = 7
            .Underline = xlUnderlineStyleNone
            .ThemeFont = xlThemeFontMinor
        End With
    
        Range(Cells(1 + PageRowOffset(Page) + BoxRowOffset(Box) + 1, 1 + BoxColOffset(Box) + 1), Cells(1 + PageRowOffset(Page) + BoxRowOffset(Box) + 2, 1 + BoxColOffset(Box) + 1)).NumberFormat = "#,##0.00"
        With Range(Cells(1 + PageRowOffset(Page) + BoxRowOffset(Box) + 1, 1 + BoxColOffset(Box) + 1), Cells(1 + PageRowOffset(Page) + BoxRowOffset(Box) + 2, 1 + BoxColOffset(Box) + 1))
            .HorizontalAlignment = xlGeneral
            .VerticalAlignment = xlTop
            .ReadingOrder = xlContext
        End With
    End Sub
    

提交回复
热议问题