+ Reply to Thread
Results 1 to 4 of 4

How to make macro to return a value?

  1. #1
    Registered User
    Join Date
    02-22-2013
    Location
    Troy, US
    MS-Off Ver
    Excel 2010
    Posts
    2

    How to make macro to return a value?

    Hi guys, I am new in Excel VBA and for the experiment data, I want to find the first number that is bigger than the next one, and i want it to show on a blank cell. How can I achieve this?
    The following is the code I wrote

    Please Login or Register  to view this content.
    Thank you for help!!
    Last edited by jeffreybrown; 02-22-2013 at 10:02 PM. Reason: Please use code tags...Thanks.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,621

    Re: How to make macro to return a value?

    It looks like you are traversing down column F, so Cells(i, F).Value > Cells(i + 1, F).Value Then must be written Cells(i, "F").Value > Cells(i + 1, "F").Value Then (notice the quotation marks around the F's).
    If you want cell F3 to hold the value that meets the "greater than" test then use: Cells(3,"F").Value = Cells(i,"F").Value
    Also, you should always use Option Explicit at the top of your module to force variable declarations. Since you did not, the VB compiler sees the F in Cells(i,F) as a variable and not a column specifier and sets its value to zero. Because there is no such thing as column zero, those lines will cause the macro to crash.
    Please Login or Register  to view this content.
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    02-22-2013
    Location
    Troy, US
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: How to make macro to return a value?

    Hi Thank you alot
    However when I run it, the system says that there variable not defined. Do you know why that happens??

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,621

    Re: How to make macro to return a value?

    I think it's the variable, i:
    Please Login or Register  to view this content.
    When you use Option Explicit, all variables must be "Dim'd" before use.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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