+ Reply to Thread
Results 1 to 14 of 14

VBA code If then Statement

  1. #1
    Registered User
    Join Date
    09-06-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2007
    Posts
    12

    VBA code If then Statement

    I am very much a novice when it comes VBA for excel, I am currently working on a macro for a spread trading worksheet. The below if statement changes the value on the buy side, however the conditions are met to slowly and the value doesn't adjust quickly enough.

    =IF(AND(H6=0,I68=0),D4,IF(AND(H6>0,H6<D4,I68=0),D4-H6,IF(AND(H6<0,H6>=-E4-F4,ISBLANK(L2)),H6*(-1),IF(H6<-E4-F4,-H6-E4-F4,IF(H6=D4,1,IF(H6>D4,1,IF(AND(H6=0,ISTEXT(I66),I68>0),D72,IF(AND(H6>0,H6<P5,ISTEXT(I66),I68>0),P5-H6,IF(AND(H6=P5,ISTEXT(I66)),D73,IF(AND(H6>P5,H6<P6,ISTEXT(I66)),P6-H6,IF(AND(H6=P6,ISTEXT(I66)),D74,IF(AND(H6>P6,H6<O7,ISTEXT(I66)),O7-H6,IF(AND(H6=D4,ISTEXT(I66),I68>0),1,IF(AND(H6+I68>D4,ISTEXT(I66),I68>0,H6>0),D4-H6,IF(AND(H6<-O3,H6>=-E4-F4,ISTEXT(L2)),H6*(-N2),1)))))))))))))))

    I started writing a macro to perform the same function:

    Please Login or Register  to view this content.
    I am having a problem with the H6>=-E4-F4 and ISBLANK(L2), not 100% how this should be written in VBA to satisfy the condition.

    Also wanted to know how to automate the process so that when I open the spreadsheet the macro runs on its own and if there was a way to improve the code.

    Thanks for the assistance
    Last edited by Fotis1991; 09-11-2013 at 03:59 AM. Reason: Moderator Note:Welcome to the forum. Pls use code tags around your code as per forum rules

  2. #2
    Valued Forum Contributor wenqq3's Avatar
    Join Date
    04-01-2013
    Location
    Malaysia
    MS-Off Ver
    Excel 2003
    Posts
    868

    Re: VBA code If then Statement

    Please Login or Register  to view this content.
    Is this?
    -If the problem is solved, please mark your thread as Solved: Click Thread Tools above your first post, select "Mark your thread as Solved".

    -Always upload a workbook before start your question
    To attach a file, push the button with the paperclip (or scroll down to the Manage Attachments button), browse to the required file, and then push the Upload button.

    +++ If my answer(s) helped you, please add me reputation by click on * +++

  3. #3
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: VBA code If then Statement

    Hi,

    For the macro to run when the workbook is opened, insert this code into the ThisWorkbook module:
    Please Login or Register  to view this content.
    To write your condition in VBA, you would use:
    Please Login or Register  to view this content.
    By the way, it might also be worth noting that this code will probably fail if it is moved to the Workbook_Open subroutine, as none of your cell references are worksheet-specific. The simplest way for you to amend this would be to wrap your code in a With statement and change every instance of "Cells" to ".Cells".

    Hope this helps

  4. #4
    Registered User
    Join Date
    09-06-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA code If then Statement

    Thanks for the help guys. Just having a bit of an issue with the automation. Still having to click on the macro to run it.

  5. #5
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: VBA code If then Statement

    Hi,

    If you place my code above into the "ThisWorkbook" module, your code will run when the workbook is opened...Capture.JPG

  6. #6
    Registered User
    Join Date
    09-06-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA code If then Statement

    Put the code in ThisWorkbook and it does change when I start up however I need the macro to run based cell H6 which changes continually. effectively all that should happen is that I should open the spread sheet and select the cells to pull through to DDE and allow the trading to take place without having to touch the spread sheet.

  7. #7
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: VBA code If then Statement

    Hi, try inserting this code into the worksheet module for the worksheet that you want to monitor cell H6 on:
    Please Login or Register  to view this content.
    Hopefully this time it works for you
    Last edited by ajryan88; 09-11-2013 at 05:04 AM.

  8. #8
    Registered User
    Join Date
    09-06-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA code If then Statement

    Hi, still only works when I open the worksheet and then I manually have to run the macro each time H6 changes.

    Any chance I could attached the spreadsheet and you could take a look at the code and maybe see what I am doing wrong?

  9. #9
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: VBA code If then Statement

    Certainly, please feel free to upload it

  10. #10
    Registered User
    Join Date
    09-06-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA code If then Statement

    Hi, attached the file. I have done all my workings on the third tab.
    Attached Files Attached Files
    Last edited by Deenz_13; 09-11-2013 at 05:12 AM.

  11. #11
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: VBA code If then Statement

    Hi,

    Your Worksheet_Change subroutine was placed in the module "Module1", not the worksheet module for "Control Panel" (see the amended attachment below).

    Note, I have also tidied your code up a little bit to make it more readable.

    Hope this helps
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    09-06-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA code If then Statement

    Thanks so much. Everything is working properly

  13. #13
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: VBA code If then Statement

    No worries! Glad to hear it.

    Please don't forget to mark this thread as solved and please click on the * next to my post(s) to say thanks

  14. #14
    Registered User
    Join Date
    09-06-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA code If then Statement

    Hi any chance of change this code:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$H$6" Then
    Buy
    Sell_Number
    End If
    End Sub

    I need to include all the entire range of cells in the spread sheet as some cells require the data changing in other cells before they can run.

+ 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. [SOLVED] Like some ideas on improving code on long IF statement used in VBA code please
    By dawatcher in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-12-2013, 01:44 PM
  2. [SOLVED] need 2 if and then statement code
    By EXLent in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-21-2012, 10:21 PM
  3. code:IF statement
    By arfermo in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 03-05-2011, 12:11 PM
  4. Using If Statement in VBA Code
    By alanda in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 12-22-2008, 03:47 PM
  5. 'For..Next' & 'If Statement' Code
    By dugong in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-22-2008, 06:52 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