简体   繁体   English

使用一个宏运行多个宏

[英]Using one macro to run multiple macros

I have the following macro that will search for three different column headings, select the data under those headings and change the data from a currency format to a number format. 我有以下宏,它将搜索三个不同的列标题,选择这些标题下的数据,并将数据从货币格式更改为数字格式。 The data is stored in Sheet 2 of my workbook and the macro works fine when I run it on it's own in this Sheet. 数据存储在我的工作簿的工作表2中,当我在此工作表中单独运行它时,宏可以正常工作。

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

I am wanting to combine this macro with another two so that I can go into Sheet 1, click a "run" button I have inserted and all my macros will run together to update my data. 我想将此宏与另外两个宏结合使用,以便进入工作表1,单击已插入的“运行”按钮,所有宏将一起运行以更新数据。
However, I am getting the error 但是,我得到了错误

Run time error 1004 - select method of range class failed 运行时错误1004-范围类的选择方法失败

at the line 在行

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

Does anyone know what might be wrong with my code? 有人知道我的代码可能出什么问题吗? I am confused since it works fine on its own, but won't run when combined with my other macros. 我很困惑,因为它本身可以很好地工作,但是当与我的其他宏结合使用时将无法运行。
I am using the following macro to combine my two existing macros: 我正在使用以下宏来合并两个现有宏:

Sub ProjectUpdate()
Call ProjectName
Call ProjectFunding
Call ProjectFundingFormat

MsgBox "Done"

End Sub

You should really avoid to use .Select and .Activate at all! 您应该完全避免使用.Select.Activate This slows down your code a lot and leads into many issues. 这会大大降低您的代码速度,并导致许多问题。 It is a very bad practice. 这是非常不好的做法。 If you are interested in writing stable and good quality code I really recommend to read and follow: How to avoid using Select in Excel VBA 如果您对编写稳定和高质量的代码感兴趣,那么我建议您阅读并遵循: 如何避免在Excel VBA中使用“选择”。

Your code can be reduced to the following: 您的代码可以简化为以下内容:

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. Throw out unnecessary repeating With statements. 丢弃不必要的重复With语句。
  2. Last column number needs not to be determined you can use the last cell of row 1 direclty with .Cells(1, .Columns.Count).End(xlToLeft) . 无需确定最后一列号,可以将第1行.Cells(1, .Columns.Count).End(xlToLeft)的最后一个像元与.Cells(1, .Columns.Count).End(xlToLeft)

  3. Throw out all .Select and .Activate . 丢弃所有.Select.Activate

Makes it much cleaner and more stable. 使它更清洁,更稳定。


Or you even use an additional procedure to avoid repeating code at all, which is a good practice to not repeat code and better have procedures instead: 或者甚至使用额外的过程来避免完全重复代码,这是不重复代码的更好的做法,而最好使用过程代替:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM