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

后端 未结 3 1933
被撕碎了的回忆
被撕碎了的回忆 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:06

    Whenever you have to scroll horizontally to read your code; consider refactoring.

    If you have a Range reference that contains two Cell references that share variables it would probably be better to use Range Resize.

    Both of these examples are referring to the same Range. Using Range Resize we are able to remove shared variable.

    Range(Cells(a + b, c), Cells(a + b + 10, c + 10))

    Cells(a + b, c).Resize(10 + 1, 10 + 1)

    Note: You will have to add one to the Columns and Rows parameter.

    Option Explicit
    
    Sub FormatText()
        Dim bc As Long, br As Long, pr As Long
    
        bc = BoxColOffset(Box)
        br = BoxRowOffset(Box)
        pr = PageRowOffset(Page)
    
        With Worksheets("A4")
            With .Cells(1 + pr + br - 2, bc).Font
                .Name = "Calibri"
                .Size = 11
                .Underline = xlUnderlineStyleNone
                .ThemeColor = xlThemeColorLight1
                .TintAndShade = 0
                .ThemeFont = xlThemeFontMinor
                .Bold = False
            End With
        End With
    
        With Worksheets("Sheet1")
            With .Cells(pr + br, 1 + bc).Resize(4, 2).Font
                .Name = "Calibri"
                .Size = 8
                .Strikethrough = False
                .Superscript = False
                .Subscript = False
                .OutlineFont = False
                .Shadow = False
                .Underline = xlUnderlineStyleNone
                .TintAndShade = 0
                .ThemeFont = xlThemeFontMinor
                .Bold = False
            End With
    
            With .Cells(pr + br + 4, 1 + bc).Resize(4, 2).Font
                .Name = "Calibri"
                .Size = 7
                .Strikethrough = False
                .Superscript = False
                .Subscript = False
                .OutlineFont = False
                .Shadow = False
                .Underline = xlUnderlineStyleNone
                .TintAndShade = 0
                .ThemeFont = xlThemeFontMinor
                .Bold = False
            End With
    
            With .Cells(1 + pr + br + 1, 1 + bc + 1).Resize(2)
                .NumberFormat = "#,##0.00"
                .HorizontalAlignment = xlGeneral
                .VerticalAlignment = xlTop
                .WrapText = False
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False
            End With
        End With
    
    
        'Updated to answer:'**How do you attack something like this?**
        With Worksheets("report")
            If fcnHasImage(.Cells(15 + i, 24)) Then
                .Cells(15 + i, 24).CopyPicture
            Else
                .Cells(15 + i, 2).CopyPicture
            End If
    
            Sheets("A4").Cells(1 + pr + br + 7, bc + 1).PasteSpecial
    
            ShowProgress    'Run macro
            .Cells(1, 25).Value = 15 + i
        End With
    
    End Sub
    

提交回复
热议问题