昨日同事有需求,想知道每個商品第一次銷售的月份,以及最后一次銷售的月份. 本想通過什么excel函數來解決,但是找了半天也沒找到合適的,最后還是通過VBA來解決吧.
使用方法:
Excel工具-宏-Visual Basic編輯器 在左側欄中點右鍵,
插入-模塊
然后輸入:
1 Function Last0(ByVal Int_Row As Integer) As Integer 2 Last0 = 14 3 Do While Cells(Int_Row, Last0) = "" And Last0 >= 3 4 Last0 = Last0 - 1 5 Loop 6 7 End Function 8 9 '這里需要注意的是 函數的返回值貌似是 變量必須與方法名一致 很奇葩的要求.... 10 Function Frist0(ByVal Int_Row As Integer) As Integer 11 Frist0 = 3 12 Do While Cells(Int_Row, Frist0) = "" And Frist0 <= 255 13 Frist0 = Frist0 + 1 14 Loop 15 If Frist0 > 255 Then 16 Frist0 = 8888 17 End If 18 End Function
然后在單位格中可以直接引用
例如:=Frist0(4) 返回值即為4行中第一個不為0的單元格列號,
如果函數返回8888,表明這行沒有數據。