vba - 添加额外的 VBA 模块会在运行以前工作的模块时产生编译错误

标签 vba excel compiler-errors compilation

试图将两个模块合并到一个 Excel 工作表中,但遇到了一个我无法诊断的问题。模块独立工作,但是当两者都在文件中时,它会给我一个“编译错误:参数数量错误或属性分配无效”,并在调试中突出显示子行。我知道的不够多,无法进一步诊断,希望能在这里得到一些指导。

模块 1 - 创建新文件和电子邮件

    Dim fPO As String
    Dim fDate As Date
    Dim fPath As String
    Dim POFile As String
    Dim Template As String
    Dim yearfolder As String
    Dim monthfolder As String
    Dim newfolderpath As String
    Dim FileName As String

Sub CreatePOFile()
'
' Creates new file for each PO
'

'


    fPO = Range("C4").Value
    fDate = Date - 3 'adjust to change date used in file name
    fPath = "\\Kforce.com\Group\TAMPA\CORP\Strategic Accounts\Strategic Accounts Operations\CLIENT REPORTS\HP\HP Weekly PO Reporting" 'adjust where file is saved
    Template = ActiveWorkbook.Name
    monthfolder = Format(fDate, "mm. mmmm YYYY")
    yearfolder = Format(fDate, "YYYY")
    newfolderpath = fPath & "\" & yearfolder & "\" & monthfolder
    FileName = fPO & " Weekly Report as of " & Format(fDate, "yyyymmdd")

'
'
'

    'Format pivot table header
    Rows("7:7").Select
    With Selection
        .HorizontalAlignment = xlCenter
    End With

    'Create new workbook and save individual PO file. Creates new folders if nonexistant
    Workbooks.Add

    If Len(Dir(fPath & "\" & yearfolder, vbDirectory)) = 0 Then
        MkDir fPath & "\" & yearfolder
    End If
    If Len(Dir(fPath & "\" & yearfolder & "\" & monthfolder, vbDirectory)) = 0 Then
        MkDir fPath & "\" & yearfolder & "\" & monthfolder
    End If

    ActiveWorkbook.SaveAs FileName:=newfolderpath & "\" & FileName, FileFormat:=51
    Application.DisplayAlerts = True

    POFile = ActiveWorkbook.Name

    'Copy Pivot information to new file
    Windows(Template).Activate
    Range("B7").Select
    ActiveCell.CurrentRegion.Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    Selection.Copy
    Range("A1").Select

    Windows(POFile).Activate
    Range("B7").Select
    ActiveSheet.Paste

    ActiveSheet.Name = fPO

    'Formatting PO File and adding information fields
    Range("B2").Select
    ActiveCell.FormulaR1C1 = "Weekly PO Report"
    With Selection.Font
    .Name = "Calibri Light"
        .Size = 18
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorLight2
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMajor
    End With

    Range("B4").Value = "PO Number:"
    Range("B5").Value = "Date Range:"

    Range("B4:B5").Select
    With Selection
        .Interior.Pattern = xlSolid
        .Interior.PatternColorIndex = xlAutomatic
        .Interior.ThemeColor = xlThemeColorAccent2
        .Interior.TintAndShade = 0
        .Interior.PatternTintAndShade = 0
        .Font.ThemeColor = xlThemeColorDark1
        .Font.TintAndShade = 0
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With

    Range("C4").Value = fPO
    Range("C5").FormulaR1C1 = "=TEXT(MIN(C[-1]),""m/d/yyyy"")&"" - ""&TEXT(MAX(C[-1]),""m/d/yyyy"")"
    Range("C4:C5").Select
    With Selection
        .Interior.Pattern = xlSolid
        .Interior.PatternColorIndex = xlAutomatic
        .Interior.ThemeColor = xlThemeColorDark1
        .Interior.TintAndShade = -0.149998474074526
        .Interior.PatternTintAndShade = 0
        .HorizontalAlignment = xlCenter
    End With

    Cells.Select
    Cells.EntireColumn.AutoFit
    Columns("B:B").Select
    Selection.ColumnWidth = 15
    ActiveWindow.DisplayGridlines = False
    Range("A1").Select


    'asks if email should be created
    Dim answer As Integer

    answer = MsgBox("Would you like to create email?", vbYesNo + vbQuestion, "Email Option")

    If answer = vbYes Then EmailFile
    Else
    End If

End Sub

Sub EmailFile()

    'Creates email with file attached
    Dim OutApp As Object
    Dim OutMail As Object
    Dim MailBody As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    MailBody = "<p style='font-family:calibri;font-size:11pt'>" & "Hello,<br><br>" & _
                "Attached you will find the weekly report for " & fPO & "." & "<br><br>" & _
                "If you have any questions or discrepancies, please let me know!<br><br>Regards," & "</p>"

    On Error Resume Next
    With OutMail
        .Display
        .to = ""
        .CC = ""
        .BCC = ""
        .Subject = FileName
        .HTMLbody = MailBody & .HTMLbody
        .Attachments.Add ActiveWorkbook.FullName

    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub

模块 2 - 格式化原始数据选项卡以方便使用
Sub Format()
'
' Removes previous raw data and deletes the blank rows so the table doesn't need to be resized upon copy paste of new data
'

    On Error Resume Next
    Rows("4:" & Rows.Count).ClearContents
    Range("raw[Last Name]").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    Range("A3:Z3").ClearContents
    Range("A3").Activate


End Sub

Sub Refresh()

If Range("A3") = "" Then

MsgBox ("Please paste new data before clicking Refresh.")

Else
    ActiveWorkbook.RefreshAll
End If

End Sub

最佳答案

在模块 2 中,Sub Format()覆盖模块 1 中使用的内置函数

(monthfolder = Format(fDate, "mm. mmmm YYYY"))

重命名 Sub Format()Sub Format1()并在需要的地方更新代码

确定冲突的一种简单方法是在函数名称内部单击并按 F1 激活帮助模块 - 它应该返回“找不到关键字”

关于vba - 添加额外的 VBA 模块会在运行以前工作的模块时产生编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44552786/

相关文章:

ms-access - 函数在 Ms Access 中返回 0 值

vba - 如何将字符串格式化为日期 - Microsoft Project 导出到 Excel?

vba - 使用vba在excel单元格中拆分文本和数字

java - try-catch 和 final 变量

android - Eclipse中未定义的外部引用

Android:Eclipse 不会运行 Android 项目

json - 在 VBA 中将对象转换为 JSON

html - 从网站中提取值(value)

vba - 如何根据条件使用整列的vb代码

excel - VBA 中的十六进制颜色代码