+ Reply to Thread
Results 1 to 8 of 8

IF statement in sub procedure (syntax ?)

  1. #1
    Registered User
    Join Date
    06-15-2012
    Location
    Baltimore, MD
    MS-Off Ver
    Excel 2010
    Posts
    68

    IF statement in sub procedure (syntax ?)

    HI,

    Hoping someone could get me started on the syntax for this particular issue.

    1st: I want a sub-procedure run all the time (everything I've done to date is based on teh click of a button), so first I'm not sure where to place this to have this IF statement always being evaluated

    2nd: What's the syntax for this? Right now all I can come up with isthe followikng and the VB window already gives me an error highlighting the "AND" in the very first line:

    SUB [name]

    IF (AND(K32="TEXT", K34="TEXT") THEN

    TXTBOX1.visitble = TRUE
    TXT Box2.visitble = False

    ElseIf (OR(K32="TEXT, K34="TEXT) THEN

    TXTBOX1.VISIBLE= TRUE
    TXTBOX2.VISIBLE= FALSE

    ELSE

    TXTBOX1.VISIBLE= TRUE
    TXTBOX2.VISIBLE = TRUE

    ENDIF

    EndSub

    Thanks in advance

  2. #2
    Forum Contributor tkowal's Avatar
    Join Date
    11-20-2010
    Location
    Miami, Fl
    MS-Off Ver
    Excel 2010
    Posts
    150

    Re: IF statement in sub procedure (syntax ?)

    If this is based on a button click then the code will go under the ButtonName_Click event on the sheet the button is placed on.

    In the Sub listed, you are using Excel Formulas instead of VBA syntax.
    Please Login or Register  to view this content.
    I cannot comment on the logic or correctness of the statements unless I can see what you are trying to do. A sample workbook depicting what your trying to accomplish would assist in providing assistance.
    Ted
    "Live Long and Prosper"

  3. #3
    Registered User
    Join Date
    06-15-2012
    Location
    Baltimore, MD
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: IF statement in sub procedure (syntax ?)

    That's obviously very helpful, and in the future will upload a sample wrkbook to assist. And will for this problem when time permite if this doesn't solve the issue. Much Apprecaited!

    In the first line I get an error "Compile error: Expected: identifier or bracketed expression" and the partenthesis just before the "K34") is highlighted.

    rivate Sub Elig_Instructions()

    If range("K32").value = "PROCEED" AND range.("K34").value = "PROCEED" Then
    SFInstructionsTxt.Visible = False
    EligInstructionsTxt.Visible = True
    TextBox5.Visible = True
    TextBox6.Visitble = False

    Is there an obvious problem with this?

  4. #4
    Forum Contributor tkowal's Avatar
    Join Date
    11-20-2010
    Location
    Miami, Fl
    MS-Off Ver
    Excel 2010
    Posts
    150

    Re: IF statement in sub procedure (syntax ?)

    With out context on where the code is being run from I cannot say.

    Send a sample workbook.... I don't have the time to recreate a test case based upon your description.

    I do not know where this code is running from. The sub appears to be marked private ... the range syntax give assumes access to currently active worksheet. The sub being private may not have access. Again I do not have time to recreate all your textboxes and such to develop a test for your code.

  5. #5
    Registered User
    Join Date
    06-15-2012
    Location
    Baltimore, MD
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: IF statement in sub procedure (syntax ?)

    Sample WorkBook attached - hope this helps
    Attached Files Attached Files

  6. #6
    Forum Contributor tkowal's Avatar
    Join Date
    11-20-2010
    Location
    Miami, Fl
    MS-Off Ver
    Excel 2010
    Posts
    150

    Re: IF statement in sub procedure (syntax ?)

    Sorry this took awhile.... got busy at work. I have attached a copy of the workbook you sent me.

    I noticed that you were using Form Controls instead of ActiveX Controls. ActiveX controls are much easier to work with while programming and are directly accessible through code. Form controls must be accessed through the Shapes collection. The drawback to ActiveX controls is that they have limited formatting possibilities unlike form controls.

    Your error was do to a misplaced "." ....

    I do not really work Form Controls if I am programming due to the difficulty in reaching their properties. I add some code to loop through all the Shapes to give me their names to verify the names in your code were correct. I also modified the code to work against the Shapes instead of ActiveX as your sample was written.

    Everything seems to work .... I added an activex Button to reset all the TextBox Visibility to the original state.....

    FormTextBoxSample.xlsm

  7. #7
    Registered User
    Join Date
    06-15-2012
    Location
    Baltimore, MD
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: IF statement in sub procedure (syntax ?)

    Hi tkowal,

    Very sorry for the delayed response. I know what it's like to get busy at work

    That's so much for doing that. For some reason I was still having difficult and it was probably cascading from my poor programming elsewhere when I tried to copy paste that into the complete spreadsheet. I know what I want to do, and end up figuring it out, albeit sloppy but it as long as it gets the task at hand done, I suppose ok. Doesn't help folks like you. Definately saving your code for the future. Anyway, I hunted a VB person at work, he came up with this for me and it did the trick:
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim First As String
    Dim Second As String
    First = Range("K32").Value
    Second = Range("K34").Value
    If First = "" Or Second = "" Then
    ActiveSheet.Shapes("SFInstructionsTxt").Visible = True
    ActiveSheet.Shapes("EligInstructionsTxt").Visible = True
    ActiveSheet.Shapes("TextBox5").Visible = False
    ActiveSheet.Shapes("TextBox6").Visible = False
    ElseIf First = "PROCEED" And Second= "PROCEED" Then
    ActiveSheet.Shapes("SFInstructionsTxt").Visible = False
    ActiveSheet.Shapes("EligInstructionsTxt").Visible = True
    ActiveSheet.Shapes("TextBox5").Visible = True
    ActiveSheet.Shapes("TextBox6").Visible = False

    ElseIf First = "INELIGIBLE" Or Second = "INELIGIBLE" Then
    ActiveSheet.Shapes("SFInstructionsTxt").Visible = True
    ActiveSheet.Shapes("EligInstructionsTxt").Visible = False
    ActiveSheet.Shapes("TextBox5").Visible = False
    ActiveSheet.Shapes("TextBox6").Visible = True
    Else
    ActiveSheet.Shapes("SFInstructionsTxt").Visible = True
    ActiveSheet.Shapes("EligInstructionsTxt").Visible = True
    ActiveSheet.Shapes("TextBox5").Visible = False
    ActiveSheet.Shapes("TextBox6").Visible = False
    End If
    End Sub

    Thanks again!
    Last edited by MaddyG; 06-21-2012 at 09:11 PM. Reason: small change in copy/past error

  8. #8
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,318

    Re: IF statement in sub procedure (syntax ?)

    Hello MaddyG,

    When posting code, please use code tags.
    HTH
    Regards, Jeff

+ 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