#### anisurrahman

##### New Member

- Joined
- Jul 3, 2008

- Messages
- 4

This is my first post.

I have five columns Column (H,I,J,K,L), I am trying to check these columns cell value and then find average. I can use excel formulae [=M11/COUNTIF((H11:L11),">0")], but I need to convert this formulae into VBA.

Code:

```
' ------ code doesn't work ----------
countWeekPattern = Application.CountIf(Sheets("MasterData").Range(Cells(myRow, "H"), _
Cells(myRow, "I")), Cells(myRow, "J"), Cells(myRow, "K"), _
Cells(myRow, "L"), ">0")
'APPLICATION-DEFINED OR OBJECT-DEFINED ERROR
```

My full code ----

Code:

```
Private Sub saveHolButton_Click()
Dim myRow
'to find row
Dim sfind As String
Dim cl As Range
Dim remainingHours As Double, totalhoursWork As Double
With Sheets("MasterData")
myRow = Application.Match(Sheets("Temp").[A11], .[A:A], 0)
'find the userID Row
If IsNumeric(myRow) Then
'if found userID
.Cells(myRow, "Q") = Sheets("Temp").[c17]
'assign the contents of C17 of sheet Temp to corresponding cell in MasterData sheet
.Cells(myRow, "S") = .Cells(myRow, "Q") + .Cells(myRow, "R")
' sum of total booked holiday January to December
.Cells(myRow, "T") = .Cells(myRow, "P") - .Cells(myRow, "S")
remainingHours = .Cells(myRow, "T")
totalhoursWork = .Cells(myRow, "M")
' sum of column H,I,J,K and L
' How do I find average of Sum(H:L)
' To find average 5 columns cell value - so it should be sum(H:L)/5
'If one of the column cell value is 0 then it should sum(H:L)/4
'if two of the columns cell value is 0 then it should sum(H:L)/3
' I can use the formulae in Excel but how do i convert it into VBA
' Excel formula =M11/COUNTIF((H11:L11),">0")
' M11 is total of (H,I,J,K and L)
' ------ code doesn't work ----------
countWeekPattern = Application.CountIf(Sheets("MasterData").Range(Cells(myRow, "H"), _
Cells(myRow, "I")), Cells(myRow, "J"), Cells(myRow, "K"), _
Cells(myRow, "L"), ">0")
'APPLICATION-DEFINED OR OBJECT-DEFINED ERROR
worksheets("MasterData").Cells(myRow, "W") = .Cells(myRow, "T") / countWeekPattern
End If
End With
end sub
```