Hi everyone,
I've written a function and it works perfectly, but I'm having problems trying to loop it in a sub. Here is the function:
Public Function FHC(ColumnNumber)
Dim LastRowNumber As Integer
LastRowNumber = Cells(3, ColumnNumber).End(xlDown).Row
For Each cell In Range(Cells(3, ColumnNumber), Cells(LastRowNumber, ColumnNumber))
If cell.Value = "SINGAPORE" Or cell.Value = "Unknown Country" Then
x = cell.Offset(0, 1).Value
Count = Count + x
End If
Next
FHC = 100 - Count
End Function
Basically it scans through the range and if the cell value is "SINGAPORE" or "Unknown Country", then it will sum up the cells next to it. So far so good.
Now on Sheet 1, I want to write a sub that loops this function for all the cells in a row. Here's my current code:
Sub FinalCalculation()
For Each cell In Range("C1:C2")
For k = 6 To 9 Step 3
Cells.Value = FHC(k)
Next k
Next
End Sub
Here is what I want to achieve:
The value of Cell C1 = FHC(6)
The value of Cell C2 = FHC(9).....and so on until the end
However, when I run the sub, what happens is that VBA does C1 = FHC(6) and then FHC(9), before moving on to the next row and doing the same thing FHC(6) then FHC(9).
I'm quite stumped on how to fix this problem, and any help provided will be greatly appreciated. Thank you!
Bookmarks