使用一个宏来运行多个宏

时间:2018-04-20 10:22:53

标签: excel vba excel-vba select multiple-columns

我有以下宏来搜索三个不同的列标题,选择这些标题下的数据并将数据从货币格式更改为数字格式。数据存储在我的工作簿的工作表2中,当我在此工作表中运行它时,宏工作正常。

Mutation

我想将这个宏与另外两个结合起来,这样我就可以进入表1,点击"运行"按钮我已插入,我的所有宏将一起运行以更新我的数据。
但是,我收到错误

  

运行时错误1004 - 选择范围类失败的方法

Sub ProjectFundingFormat()
'
Dim WS As Worksheet
Dim lastCol As Long, lastRow As Long, srcRow As Range
Dim found1 As Range, found2 As Range

Set WS = Workbooks("Workbook1.xlsm").Worksheets("Sheet2") 'Needs to be open

    With WS
    lastCol = .Cells(1, Columns.count).End(xlToLeft).Column
    Set srcRow = .Range("A1", .Cells(1, lastCol))
    Set found1 = srcRow.Find(What:="2018FundingLabor", LookAt:=xlWhole, MatchCase:=False)

    If Not found1 Is Nothing Then
            lastRow = .Cells(Rows.count, found1.Column).End(xlUp).Row
            .Range(.Cells(2, found1.Column), .Cells(lastRow, found1.Column)).Select
            Selection.NumberFormat = "0.00"
        End If
End With


With WS
    lastCol = .Cells(1, Columns.count).End(xlToLeft).Column
    Set srcRow = .Range("A1", .Cells(1, lastCol))
    Set found1 = srcRow.Find(What:="2018FundingNonlabor", LookAt:=xlWhole, MatchCase:=False)

    If Not found1 Is Nothing Then
            lastRow = .Cells(Rows.count, found1.Column).End(xlUp).Row
            .Range(.Cells(2, found1.Column), .Cells(lastRow, found1.Column)).Select
            Selection.NumberFormat = "0.00"
        End If
End With

    With WS
    lastCol = .Cells(1, Columns.count).End(xlToLeft).Column
    Set srcRow = .Range("A1", .Cells(1, lastCol))
    Set found1 = srcRow.Find(What:="2018 Total Funding", LookAt:=xlWhole, MatchCase:=False)

    If Not found1 Is Nothing Then
            lastRow = .Cells(Rows.count, found1.Column).End(xlUp).Row
            .Range(.Cells(2, found1.Column), .Cells(lastRow, found1.Column)).Select
            Selection.NumberFormat = "0.00"
        End If
End With
End Sub

有谁知道我的代码可能有什么问题?我感到困惑,因为它本身可以正常工作,但与其他宏结合使用时不会运行。
我使用以下宏来组合我的两个现有宏:

.Range(.Cells(2, found1.Column), .Cells(lastRow, found1.Column)).Select

1 个答案:

答案 0 :(得分:1)

你应该完全避免使用.Select.Activate!这会大大减慢您的代码速度并导致许多问题。这是一个非常糟糕的做法。如果您有兴趣编写稳定且质量上乘的代码,我建议您阅读并遵循:How to avoid using Select in Excel VBA

您的代码可以缩减为以下内容:

Option Explicit

Public Sub ProjectFundingFormat()
    Dim Ws As Worksheet
    Set Ws = Workbooks("Workbook1.xlsm").Worksheets("Sheet2") 'Needs to be open

    With Ws
        Dim srcRow As Range
        Set srcRow = .Range("A1", .Cells(1, .Columns.Count).End(xlToLeft))

        Dim lastRow As Long
        Dim found1 As Range

        Set found1 = srcRow.Find(What:="2018FundingLabor", LookAt:=xlWhole, MatchCase:=False)
        If Not found1 Is Nothing Then
            lastRow = .Cells(.Rows.Count, found1.Column).End(xlUp).Row
            .Range(.Cells(2, found1.Column), .Cells(lastRow, found1.Column)).NumberFormat = "0.00"
        End If

        Set found1 = srcRow.Find(What:="2018FundingNonlabor", LookAt:=xlWhole, MatchCase:=False)
        If Not found1 Is Nothing Then
            lastRow = .Cells(.Rows.Count, found1.Column).End(xlUp).Row
            .Range(.Cells(2, found1.Column), .Cells(lastRow, found1.Column)).NumberFormat = "0.00"
        End If

        Set found1 = srcRow.Find(What:="2018 Total Funding", LookAt:=xlWhole, MatchCase:=False)
        If Not found1 Is Nothing Then
            lastRow = .Cells(.Rows.Count, found1.Column).End(xlUp).Row
            .Range(.Cells(2, found1.Column), .Cells(lastRow, found1.Column)).NumberFormat = "0.00"
        End If
    End With
End Sub
  1. 丢弃不必要的重复With语句。
  2. 无需确定最后一列编号,您可以将第1行direclty的最后一个单元格与.Cells(1, .Columns.Count).End(xlToLeft)一起使用。

  3. 抛弃所有.Select.Activate

  4. 使其更清洁,更稳定。

    或者你甚至使用一个额外的程序来避免重复代码,这是一个很好的做法,不重复代码,更好的程序:

    Option Explicit
    
    Public Sub ProjectFundingFormat()
        Dim Ws As Worksheet
        Set Ws = Workbooks("Workbook1.xlsm").Worksheets("Sheet2") 'Needs to be open
    
        With Ws
            Dim srcRow As Range
            Set srcRow = .Range("A1", .Cells(1, .Columns.Count).End(xlToLeft))
    
            FindAndFormat srcRow, "2018FundingLabor"
            FindAndFormat srcRow, "2018FundingNonlabor"
            FindAndFormat srcRow, "2018 Total Funding"
        End With
    End Sub
    
    Public Sub FindAndFormat(srcRow As Range, FindWhat As String)
        With srcRow.Parent
            Dim found1 As Range
            Set found1 = srcRow.Find(What:=FindWhat, LookAt:=xlWhole, MatchCase:=False)
            If Not found1 Is Nothing Then
                Dim lastRow As Long
                lastRow = .Cells(.Rows.Count, found1.Column).End(xlUp).Row
                .Range(.Cells(2, found1.Column), .Cells(lastRow, found1.Column)).NumberFormat = "0.00"
            End If
        End With
    End Sub