简体   繁体   English

VBA 的日期在 Excel 2011 中不起作用?

[英]Date for VBA not working in Excel 2011?

Here is my code, I am attempting, which works BTW on a PC, but not on a Mac, to run this code and have an excel sheet created, named, add a tab, change the color of said tabs, change the name of said tabs, and then transpose the data while maintaining the format of the cells and width and height of the cells to the new worksheet.这是我的代码,我正在尝试,顺便说一句,它在 PC 上运行,但在 Mac 上不起作用,运行此代码并创建一个 Excel 工作表,命名,添加一个选项卡,更改所述选项卡的颜色,更改名称所述选项卡,然后将数据转置,同时将单元格的格式以及单元格的宽度和高度保持到新工作表。

This works, on a PC.... but when I get onto a Mac, it doesn't.这在 PC 上有效......但是当我进入 Mac 时,它没有。

I go into References, and this is what I see.我进入参考文献,这就是我所看到的。

在此处输入图片说明

I see Ref Edit Control, and Microsoft Scripting Runtime are missing.我看到 Ref Edit Control 和 Microsoft Scripting Runtime 缺失。 I disabled both, and the script gives me an error here still:我禁用了两者,脚本仍然在这里给我一个错误:

wbBK2.SaveAs Dir & Application.PathSeparator & "Open Order Report -" & Format(Date, "mm-dd-yyyy") & ".xlsx"

The error happens at the (Date, "mm-dd-yyyy") Specifically the Date section.错误发生在(Date, "mm-dd-yyyy")特别是Date部分。 I can't figure out why this is happening honestly.我无法弄清楚为什么会发生这种情况。 If someone can peruse this and give me an answer and a solution it be greatly appreciated.如果有人可以仔细阅读并给我一个答案和解决方案,我将不胜感激。

The error I get is an Error '9 Subscript Out Of Range I can't see a reason why this error only shows up on a Mac, and not a PC.我得到的Error '9 Subscript Out Of RangeError '9 Subscript Out Of Range我看不出这个错误只出现在 Mac 上而不是 PC 上的原因。

Option Explicit

Sub OpenOrderReportExport()

    Dim wsJL As Worksheet   'Jobs List
    Dim wsPOT As Worksheet  'PO Tracking
    Dim wsTNO As Worksheet  'Tel-Nexx OOR
    Dim wsDOO As Worksheet  'Dakota OOR
    Dim wbBK1 As Workbook   'Open Order Report
    Dim wbBK2 As Workbook   'New Workbook
    Dim wsWS1 As Worksheet  'Sheet1
    Dim wsWS2 As Worksheet  'Sheet2
    Dim wsWS3 As Worksheet  'Sheet3
    Dim wsWS4 As Worksheet  'Sheet4
    Dim CurrentFile As String, NewFileType As String, NewFile As String, Dir As String, lastrow As Long

    Set wsJL = Sheets("Jobs List")      'Jobs List
    Set wsPOT = Sheets("PO Tracking")   'PO Tracking
    Set wsTNO = Sheets("Tel-Nexx OOR")  'Tel-Nexx OOR
    Set wsDOO = Sheets("Dakota OOR")    'Dakota OOR
    Set wbBK1 = ThisWorkbook
    Set wbBK2 = Workbooks.Add           'New Workbook
    Set wsWS1 = wbBK2.Sheets("Sheet1")  'Sheet1
    Set wsWS2 = wbBK2.Sheets("Sheet2")  'Sheet2
    Set wsWS3 = wbBK2.Sheets("Sheet3")  'Sheet3


        Application.ScreenUpdating = False    ' Prevents screen refreshing.
        CurrentFile = ThisWorkbook.FullName
        NewFileType = "Excel Files 2007 (*.xlsx)"
        Dir = ThisWorkbook.path & Application.PathSeparator & "Reports"
        wbBK2.SaveAs Dir & Application.PathSeparator & "Open Order Report -" & Format(Date, "mm-dd-yyyy") & ".xlsx"
        Sheets.Add After:=Sheets(Sheets.Count)

    Set wsWS4 = wbBK2.Sheets("Sheet4")  'Sheet4

        With wbBK2
            Dim Sht As Worksheet
                For Each Sht In Worksheets
                    Sht.Tab.Color = 255
                Next
        End With

        Sheets("Sheet1").Name = "Jobs List"
        Sheets("Sheet2").Name = "PO Tracking"
        Sheets("Sheet3").Name = "Dakota OOR"
        Sheets("Sheet4").Name = "Tel-Nexx OOR"

        With wbBK1
            'Jobs List Export
            lastrow = wsJL.Range("B" & Rows.Count).End(xlUp).Row
            wsJL.Range("A2:N2").Copy
            wsWS1.Range("A1").PasteSpecial xlPasteAll
            wsJL.Range("A3:N" & lastrow).Copy
            wsWS1.Range("A2").PasteSpecial xlPasteValuesAndNumberFormats
            wsWS1.Range("A2").PasteSpecial xlPasteColumnWidths
            wsJL.Range("B3:N" & lastrow).Copy
            wsWS1.Range("B2").PasteSpecial xlPasteFormats
            wsWS1.Columns("A").Delete

            'Tel-Nexx Export
            lastrow = wsTNO.Range("B" & Rows.Count).End(xlUp).Row
            wsTNO.Range("A2:Q2").Copy
            wsWS2.Range("A1").PasteSpecial xlPasteAll
            wsTNO.Range("A3:Q" & lastrow).Copy
            wsWS2.Range("A2").PasteSpecial xlPasteValuesAndNumberFormats
            wsWS2.Range("A2").PasteSpecial xlPasteColumnWidths
            wsTNO.Range("B3:Q" & lastrow).Copy
            wsWS2.Range("B2").PasteSpecial xlPasteFormats
            wsWS2.Columns("A").Delete

            'Dakota Export
            lastrow = wsDOO.Range("B" & Rows.Count).End(xlUp).Row
            wsDOO.Range("A2:O2").Copy
            wsWS3.Range("A1").PasteSpecial xlPasteAll
            wsDOO.Range("A3:O" & lastrow).Copy
            wsWS3.Range("A2").PasteSpecial xlPasteValuesAndNumberFormats
            wsWS3.Range("A2").PasteSpecial xlPasteColumnWidths
            wsDOO.Range("B3:O" & lastrow).Copy
            wsWS3.Range("B2").PasteSpecial xlPasteFormats
            wsWS3.Columns("A").Delete

            'PO Tracking Export
            lastrow = wsPOT.Range("B" & Rows.Count).End(xlUp).Row
            wsPOT.Range("A2:K2").Copy
            wsWS4.Range("A1").PasteSpecial xlPasteAll
            wsPOT.Range("A3:K" & lastrow).Copy
            wsWS4.Range("A2").PasteSpecial xlPasteValuesAndNumberFormats
            wsWS4.Range("A2").PasteSpecial xlPasteColumnWidths
            wsPOT.Range("B3:K" & lastrow).Copy
            wsWS4.Range("B2").PasteSpecial xlPasteFormats
            wsWS4.Columns("A").Delete
        End With

        With wsWS1
            .Activate
            .Range("A1").Select
        End With
End Sub

the MISSING references are what are causing the problems. MISSING引用是导致问题的原因。 Remove the checkmarks there, and the basic stuff like date will start working again.删除那里的复选标记, date等基本内容将再次开始工作。 If those references are critical to the code you are running, you will have to search out the Mac equivalents如果这些引用对您正在运行的代码至关重要,则您必须搜索 Mac 等效项

More about references - normally resolving "Missing" fixes it, but, from here关于参考的更多信息 - 通常解决“缺失”修复它,但是,从这里

What you're describing is typical of corrupted references.您所描述的是典型的损坏引用。 This can be caused by a referenced file being a different version or in a different location between the machine on which the code was developed, and the client machines.这可能是由于引用的文件版本不同或位于开发代码的机器和客户端机器之间的不同位置造成的。 Our company also tries to keep all the machines configured the same way, but I've found it's essentially impossible to manage.我们公司也试图保持所有机器配置相同,但我发现这基本上是不可能管理的。

Open any code module (or open the Debug Window, using Ctrl-G, provided you haven't selected the "keep debug window on top" option).打开任何代码模块(或使用 Ctrl-G 打开调试窗口,前提是您没有选择“保持调试窗口在顶部”选项)。 Select Tools |选择工具 | References from the menu bar.来自菜单栏的引用。 Examine all of the selected references.检查所有选定的参考。

If any of the selected references have "MISSING:" in front of them, unselect them, and back out of the dialog.如果任何选定的引用前面有“缺少:”,请取消选择它们,然后退出对话框。 If you really need the reference(s) you just unselected (you can tell by doing a Compile All Modules), go back in and reselect them.如果您真的需要您刚刚取消选择的参考(您可以通过编译所有模块来判断),请返回并重新选择它们。

If none have "MISSING:", select an additional reference at random, back out of the dialog, then go back in and unselect the reference you just added.如果没有“缺少:”,则随机选择一个额外的参考,退出对话框,然后返回并取消选择您刚刚添加的参考。 If that doesn't solve the problem, try to unselect as many of the selected references as you can (Office may not let you unselect them all), back out of the dialog, then go back in and reselect the references you just unselected.如果这不能解决问题,请尝试尽可能多地取消选择已选择的参考文献(Office 可能不允许您全部取消选择它们),退出对话框,然后返回并重新选择您刚刚取消选择的参考文献。 (NOTE: write down what the references are before you delete them, because they'll be in a different order when you go back in) (注意:在删除之前写下引用是什么,因为当您返回时它们的顺序会有所不同)

Yes, disambigulating as VBA.xxxx will work, since Excel no longer has to look through all of the references.是的,消除歧义为 VBA.xxxx 将起作用,因为 Excel 不再需要查看所有引用。

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

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