+ Reply to Thread
Results 1 to 5 of 5

Unable to Loop Function Properly

  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:

    Please Login or Register  to view this content.
    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:

    Please Login or Register  to view this content.
    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.

  2. #2
    Valued Forum Contributor Sean Thomas's Avatar
    Join Date
    03-25-2012
    Location
    HerneBay, Kent, UK
    MS-Off Ver
    Excel 2007,2016
    Posts
    971

    Re: Unable to Loop Function Properly

    Hi try this
    you were looping through k for each cell.
    you need to loop through the cells and add 3 after each loop

    Please Login or Register  to view this content.
    Last edited by Sean Thomas; 01-18-2014 at 04:28 AM. Reason: loop error
    Regards
    Sean

    Please add to my reputation if you think i helped
    (click on the star below the post)
    Mark threads as "Solved" if you have your answer
    (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code:
    [code] Your code here [code]
    Please supply a workbook containing example Data:
    It makes its easier to answer your problem & saves time!

  3. #3
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,873

    Re: Unable to Loop Function Properly

    I'd rather put k=6 outside of loop. And not cells but cell

    Please Login or Register  to view this content.
    PS. I left C1:C2 (or C1:C10) , but you wrote: "loops this function for all the cells in a row" while here you loop it through column.
    Last edited by Kaper; 01-18-2014 at 04:27 AM.

  4. #4
    Valued Forum Contributor Sean Thomas's Avatar
    Join Date
    03-25-2012
    Location
    HerneBay, Kent, UK
    MS-Off Ver
    Excel 2007,2016
    Posts
    971

    Re: Unable to Loop Function Properly

    Well pointed out Kaper - my mistake.
    Had a late night and not fully awake yet.
    I have amended my code for my error.

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

    Re: Unable to Loop Function Properly

    Thank you both of you, you've solved my problem!

+ Reply to Thread

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