如何避免在我的特定vba代码中使用.Select,.Activate,ActiveSheet,ActiveCell?

时间:2016-10-13 11:41:30

标签: excel vba excel-vba

我有这个代码显然使用Select,.Activate,...我明白这不是一个好的做法,此外应用程序正在进行,所以这可能是因为使用Select ...
我对VBA很新,并且非常感谢有关如何将此代码更改为不使用Select.Activate,ActiveSheet,ActiveCell以及其他考虑因素以获得更高效的帮助。

  Sub FormatText()

    Sheets("A4").Cells(1 + PageRowOffset(Page) + BoxRowOffset(Box) - 2, BoxColOffset(Box)).Activate

    With ActiveCell.Font
        .Name = "Calibri"
        .Size = 11
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
        .Bold = False
    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
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
        .Bold = False
    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
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
        .Bold = False
    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)).Select
    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 Selection
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlTop
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
End Sub

**How do you attack something like this?**
Sheets("report").Activate
       If fcnHasImage(Cells(15 + i, 24)) Then
            ActiveSheet.Cells(15 + i, 24).CopyPicture
Else
            ActiveSheet.Cells(15 + i, 2).CopyPicture         
     End If
       Sheets("A4").Select  '< - How should I this be changed?
       Cells(1 + PageRowOffset(Page) + BoxRowOffset(Box) + 7, BoxColOffset(Box) + 1).Select '< - This I guess is by changing it to Range?/Henrik
       ActiveSheet.Paste
       Application.CutCopyMode = False
       ShowProgress 'Run macro
       ActiveSheet.Cells(1, 25).Value = 15 + i + 
  End If...

3 个答案:

答案 0 :(得分:2)

以下是您的代码的简化版本:

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

答案 1 :(得分:1)

选择和激活基本上只是用于录制宏的方法。要从那里减少宏,您可以执行以下操作:

  • 使用任何位置ActiveCell,只需将其替换为Range被调用的.Activate引用。 (在您的情况下,第一个With ActiveCell.Font将成为With Sheets("A4").Cells(1 + PageRowOffset(Page) + BoxRowOffset(Box) - 2, BoxColOffset(Box)).Font
  • 使用任何位置Selection,只需将其替换为Range被调用的.Select引用。 (在您的情况下,With Selection将成为With Range(Cells(1 + PageRowOffset(Page) + BoxRowOffset(Box) + 1, 1 + BoxColOffset(Box) + 1), Cells(1 + PageRowOffset(Page) + BoxRowOffset(Box) + 2, 1 + BoxColOffset(Box) + 1))

顺便说一句,当您更正上一个With Selection块时,您也可以将.NumberFormat调整移动到With块中。

一些额外的建议是养成建立Worksheet对象的习惯,你可以存储你工作的特定工作表。所以我会做Dim currentSheet As Worksheet之类的东西,然后在这块之前的某个地方您已发布的代码(如果适用)Set currentSheet = Sheets("A4")。您必须将所有Range(...)Cells(...)来电更新为currentSheet.Range(...),但这样做的好处是您的RangeCells来电将总是参考表格(“A4”) - 如果您稍后对此宏进行修改,它们不会意外地切换上下文。一般来说,这也是你避免依赖ActiveSheet的方式。

答案 2 :(得分:1)

每当你必须水平滚动以阅读你的代码时;考虑重构。

如果您的Range引用包含两个共享变量的Cell引用,那么使用Range Resize可能会更好。

这两个例子都指的是同一个范围。使用Range Resize,我们可以删除共享变量。

  

范围(细胞(a + b,c),细胞(a + b + 10,c + 10))

     

细胞(a + b,c).Resize(10 + 1,10 + 1)

注意:您必须在Columns and Rows参数中添加一个。

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