+ Reply to Thread
Results 1 to 3 of 3

Using Excel formulas with variables in VBA

  1. #1
    Registered User
    Join Date
    05-18-2012
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    22

    Using Excel formulas with variables in VBA

    Hi guys,

    I have two doubts related to some VBA code and would be very grateful if you could help me.

    1. I have a VBA macro with some variables defined and in that code I'm trying to use standard Excel formulas. However, I'm not sure how to use Excel formulas + the code variable(s) together. More specifically, here is a snippet of my code -

    Please Login or Register  to view this content.
    As you can see, I have a variable "i" which loops through multiple sheets of the workbook doing some calculations in cells (10,10), (11,10), (12,10) and (j,k) respectively ("j", "k" too are variables). When I run my macro, it runs totally fine if these 4 lines are commented. But otherwise I get an error in these lines. So could someone tell me *specifically* what is the correct syntax for this statement, and also what one must do *in general* if one wants to use Excel formulas in VBA?

    2. I also have one other question. How do I check if a particular value exists in a range (in my case the value is "#N/A Requesting Data...")? In my code currently I'm looping through every cell in that range and checking if
    Please Login or Register  to view this content.
    and then doing some particular task. Although this is working, it is obviously very expensive in terms of time. Is there a better way to check if a value exists in a range which you define?

    Hope someone can help me at the earliest.

    Thanks a lot,
    exceluser_1

  2. #2
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Using Excel formulas with variables in VBA

    No. 1 - In your case, the formula property (of the range object) returns the formula as a string in the same format that would be displayed in the formula bar (including the equal sign).

    Example:

    Please Login or Register  to view this content.
    If this example were expanded to include variables, and A4 and A10 were already defined range variables, you could simply convert the ranges via the address property
    Please Login or Register  to view this content.
    I hope this helps you understand the way the property works a bit more.

    No. 2 - the Find method is more efficient than a traditional loop. This example finds all cells in the range A1:A500 on worksheet one that contain the value 2 and changes it to 5.
    Please Login or Register  to view this content.
    Last edited by AlvaroSiza; 05-18-2012 at 02:00 AM.
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  3. #3
    Registered User
    Join Date
    05-18-2012
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Using Excel formulas with variables in VBA

    Thanks a lot AlvaroSiza. It certainly helped me in my code. However with regards to Question-1 of mine above, could you please specifically write down the code for what I had mentioned?
    Please Login or Register  to view this content.
    I ask because what I've written is a bit complicated so if you could give me the VBA equivalent of it, it will help me know better about the syntax for such situations in general.

    Thanks a lot again,
    excelworker_1

+ 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