Results 1 to 5 of 5

Unable to Loop Function Properly

Threaded View

  1. #1
    Registered User
    Join Date
    01-18-2014
    Location
    KL, Msia
    MS-Off Ver
    Excel 2007
    Posts
    2

    Unable to Loop Function Properly

    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!
    Last edited by pgtgogo; 01-18-2014 at 05:42 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Sumproduct with OR criteria, unable to properly subtract all the AND cases.
    By psytroniks in forum Excel Formulas & Functions
    Replies: 16
    Last Post: 12-04-2012, 01:24 PM
  2. Loop For not working properly
    By mabbutt in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-12-2008, 08:59 AM
  3. how to properly loop a macro
    By brawny_javo in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-18-2006, 01:30 PM
  4. For Each Loop not working properly
    By oakman in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 03-26-2006, 06:44 PM
  5. Loop Function unable to loop
    By Junior728 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-28-2005, 06:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1