简体   繁体   English

用Excel for Windows编写的VBA在Mac上不起作用

[英]VBA written in Excel for Windows not working on Mac

I have a set of macros to hide and unhide columns based on the contents of a specific row. 我有一组宏可以根据特定行的内容隐藏和取消隐藏列。 They were all written in Excel 2013 for Windows (running in parallels on my MBA, if that's relevant) and work fine there. 它们都是用Excel 2013 for Windows编写的(如果相关的话,可以在我的MBA上并行运行),并且可以在其中正常工作。 But when I open the worksheet in Excel 2011 for Mac, the macros give odd results. 但是,当我在Mac的Excel 2011中打开工作表时,这些宏给出了奇怪的结果。 The "unhide all columns" macro works fine; “取消隐藏所有列”宏可以正常工作; the other functions get as far as hiding all columns but not as far as unhiding the ones I want to see. 其他功能可以隐藏所有列,而不能隐藏我想看到的列。

I can only assume Excel for Mac is having a problem with what's in the FOR EACH loop, but I can't figure out what! 我只能假定Excel for Mac在FOR EACH循环中存在问题,但我不知道是什么! I'd appreciate any guidance: I need to get this system working on both Windows and Mac. 我将不胜感激任何指导:我需要使该系统在Windows和Mac上都能正常工作。

Code below. 下面的代码。

This function works: 此功能有效:

Sub GANTT_Filter_Show_All()

Dim rngDates As Range

Set rngDates = Range("GANTT_Dates")

rngDates.EntireColumn.Hidden = False

End Sub

But this one only hides all the columns: 但这仅隐藏了所有列:

Sub GANTT_Filter_This_Quarter()

Dim intCurrentMonth As Integer, intCurrentYear As Integer, rngDates As Range, cell As Range
Dim intCurrentQuarterMonths(3) As Integer

Set rngDates = Range("GANTT_Dates")
intCurrentMonth = DatePart("m", Date)
intCurrentYear = DatePart("yyyy", Date)

'loading months of current quarter into an array intCurrentMonth

Select Case intCurrentMonth
    Case 1 To 3
        intCurrentQuarterMonths(0) = 1
        intCurrentQuarterMonths(1) = 2
        intCurrentQuarterMonths(2) = 3
    Case 4 To 6
        intCurrentQuarterMonths(0) = 4
        intCurrentQuarterMonths(1) = 5
        intCurrentQuarterMonths(2) = 6
    Case 7 To 9
        intCurrentQuarterMonths(0) = 7
        intCurrentQuarterMonths(1) = 8
        intCurrentQuarterMonths(2) = 9
    Case 10 To 12
        intCurrentQuarterMonths(0) = 10
        intCurrentQuarterMonths(1) = 11
        intCurrentQuarterMonths(2) = 12
    End Select

'hiding all columns

rngDates.EntireColumn.Hidden = True

'comparing each column to array of months in current quarter and hiding if false

For Each cell In rngDates
    For Each v In intCurrentQuarterMonths
        If v = DatePart("m", cell.Value) And DatePart("yyyy", cell.Value) = intCurrentYear Then cell.EntireColumn.Hidden = False
    Next v
Next cell

Application.Goto Reference:=Range("a1"), Scroll:=True

End Sub

I'm with @Steven on this one, nothing obviously wrong with the code. 我在此上使用@Steven,代码显然没有错。 I'm not a Mac user, but it's entirely possible that there's some weirdness around the date functions, particularly those that require formatting to resolve. 我不是Mac用户,但日期函数很可能有些奇怪,尤其是那些需要格式化才能解决的函数。

I would try replacing the calls to DatePart() with calls to Month() and Year() in situations like this - even for non-Mac users. 在这种情况下,我尝试将对DatePart()的调用替换为对Month()和Year()的调用-即使对于非Mac用户也是如此。 It doesn't rely on parsing the strings for formatting, so it's much more efficient (and easy to read): 它不依赖于解析字符串来格式化,因此效率更高(且易于阅读):

Sub Benchmarks()

    Dim starting As Double, test As Date, i As Long
    test = Now

    starting = Timer
    For i = 1 To 1000000
        Year test
    Next i
    Debug.Print "Elapsed: " & (Timer - starting)

    starting = Timer
    For i = 1 To 1000000
        DatePart "yyyy", test
    Next i
    Debug.Print "Elapsed: " & (Timer - starting)

End Sub

Since you likely can't run the benchmark... 由于您可能无法运行基准测试...

Elapsed for Year():     0.109375
Elapsed for DatePart(): 0.515625

Also note that in addition to this, the dates in the column you're searching are coming through as Variants, it may help to explicitly cast them to dates: 还要注意,除此之外,您正在搜索的列中的日期以变体形式出现,这可能有助于将其显式转换为日期:

If v = Month(CDate(cell.Value)) And intCurrentYear = Year(CDate(cell.Value)) Then
    cell.EntireColumn.Hidden = False
End If

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

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