函数在即时窗口上运行,但不在单元格表上运行

时间:2017-01-09 14:00:02

标签: excel-vba vba excel

有人可以帮我理解问题所在, 我想要从任何一个月末开始,每个运营商的价值。 循环方法不是问题, 这是我的功能:

 Function Tmese (c As String, m As Integer) As Integer 'C as Carrier, as m month
        Dim y As Date, x As Variant
         y = CDate (1 & "/" & m + 1 & "/" & 2016) - 1 'to have the end of the month add one month to c and I subtract one day
         With Worksheets (c)
             Set x = .Columns (1) .Find (y,,, xlWhole)
             If X Is Nothing Then
                 Tmese = .Cells (x.Row, 5) 'found max dates into a month to retreive corrispondence fair
             else
                 Exit Function 'not found
             end If
         end With
 end Function

如果我在即时窗口上播放我的函数,结果是立即的,但是,如果我将函数放在单元格中,如= Tmese(“GLS”; 2)没有任何反应 为什么?! ??!

1 个答案:

答案 0 :(得分:1)

跟进上述大部分评论:

  1. 您需要使用Find确保If Not x Is Nothing Then成功。

  2. 您可以使用WorksheetFunction.EoMonth查找m月末。

  3. 功能 Tmese 代码

    Function Tmese(c As String, m As Integer) As Integer  ' C as Carrier, as m month
    
        Dim y As Date, x As Variant
    
        y = WorksheetFunction.EoMonth(CDate("1/" & m & "/2016"), 0) ' find add of the month of m
        With Worksheets(c)
            Set x = .Columns(1).Find(What:=CStr(y))
            If Not x Is Nothing Then
                Tmese = .Cells(x.Row, 5)  ' found max dates into a month to retreive corrispondence fair
            Else
                Exit Function 'not found
            End If
        End With
    
    End Function
    

    GLS工作表数据

    enter image description here

    如何申请另一个工作表

    enter image description here