View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
moon[_3_] moon[_3_] is offline
external usenet poster
 
Posts: 5
Default function need to return value and update one cell value

Hi Oartick
It is ok using sub to update cell value, but function seem not.
Sub Update_cell()
Dim holidaySheet As Variant
Set holidaySheet = Sheets("Holiday")
holidaySheet.Range("i2").Value = "TTT"

End Sub


"Patrick Molloy" wrote in message
...
you really don't need code for this.
You can use MATCH() and COUNTIF() functions quite easily

eg for all hilidays
=COUNTIF(holiday!B:B,"Yes")

if A7 has the latest date, and you want all hoilday up and including that

date
=COUNTIF(OFFSET(holiday!B1,0,0,MATCH(A7,holiday!A: A,FALSE)),"Yes")

Other stuff
if you want to set a variable to a cell on a sheet
DIM wsHoliday as Worksheet
DIM cell as Range
SET wsHoliday = Worksheets("Holiday")
SET cell = Worksheets("Main").Range("F4")

you don't need to use 'address' at all
cell.Value= 10


---
with functions, you return a value to the caller
cell = Myfuncttion(par1,par2)

where

function MyFunction(par1 as long, par2 as long) as Date
MyFunction = Date + par2 - par1
End Function



"moon" wrote:

Below function can return non-holiday date, I want to return how many
holiday found.
Variable HolidayResult to be update as long integer. But , I try can not
able to return the non-holiday by coding HolidayResult.Address.Value =
cntHoliday

HolidayResult. Address is "$F$4' in my test case.

Holiday Sheet format
Column A - Date value
Column B - Yes = Holiday, No = Non-Holiday

Function xHoliday(HolidayResult As Range, loDate As Date, ByVal loOffset

As
Long) As Date
'~~ if input value no changed, this function will not call.
Dim HolidaySheet As Variant
Dim i As Long
Dim found As Boolean
Dim cntHoliday As Long
cntHoliday = 0
found = False
xHoliday = loDate - loOffset
Set HolidaySheet = Sheets("Holiday")
i = 1001
Do

If HolidaySheet.Range("A" & i) = xHoliday Then
found = True
Else
i = i - 1
End If

Loop Until found = True

'~~ When return date still holiday, skip it , until Holiday = "no"
found = False
Do

If Trim(UCase(HolidaySheet.Range("B" & i))) = "NO" Then
found = True
Else
xHoliday = xHoliday - 1
cntHoliday = cntHoliday + 1
i = i - 1
End If

Loop Until found = True
' MsgBox HolidayResult.Address
HolidayResult.Address.Value = cntHoliday
End Function