+ Reply to Thread
Results 1 to 18 of 18

Using If Statement in VBA Code

  1. #1
    Registered User
    Join Date
    12-15-2008
    Location
    hanover
    Posts
    34

    Using If Statement in VBA Code

    I am very inexperienced in VBA coding, so I hope somebody can help me. What I am trying to accomplish is this:
    If Cell "B4" = 0, then lock cells "E14"-"E47" so that you cannot enter data in them, otherwise If cell "B4" contains another value, then leave cells "E14-E47" unlocked so that data can be entered in them.
    I do have the worksheet password protected, but cells E14-E47 are cells that I have unlocked because I want the user to be able to enter data into these cells, unless of course the value of "B4" is zero.
    Last edited by VBA Noob; 12-22-2008 at 03:52 PM.

  2. #2
    Registered User
    Join Date
    07-02-2008
    Location
    Fort Worth, TX
    Posts
    99
    I was wrong.
    Last edited by Tirren; 12-16-2008 at 05:18 PM.

  3. #3
    Registered User
    Join Date
    12-15-2008
    Location
    hanover
    Posts
    34
    You can unprotect the sheet as part of your VBA code can't you. Don't you just put in
    Please Login or Register  to view this content.
    I just don't know how to create the IF statement, if it can be done at all that is.
    Last edited by VBA Noob; 12-15-2008 at 05:58 PM. Reason: Added code tags as per forum rules

  4. #4
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Please use code tags next time as per forum rules below.

    Try something like

    Please Login or Register  to view this content.
    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  5. #5
    Registered User
    Join Date
    12-15-2008
    Location
    hanover
    Posts
    34

    Red face Still doesn't work

    I have decided to attach my excel spreadsheet so that you can understand what I am trying to accomplish. Again, not sure if it is even possible, but worth a try. I did try adding the code that you recomended into the visual basic editor, but not sure if I did it right, as it does not seem to work. All of my other applications that have code attached to them also have a button attached to them, but in this case, there is obviously no button. So is that all I do, put the code into the visual basic editor?

    Again, what I am trying to accomplish is this: If the mechanics available hours in column B is zero, then I don't want the user to be able to enter numbers into the corresponding column in the appointment part of the spreadsheet. So if B4=0, then lock cells E14-E47.
    The password to unlock the spreadsheet is "service".

    Thank you so much for taking the time to try and help me.
    Attached Files Attached Files

  6. #6
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    See if this helps. You could use a change event to trigger the macro if you wish

    Please Login or Register  to view this content.
    VBA Noob
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    12-15-2008
    Location
    hanover
    Posts
    34

    Red face what is a change event

    I'm sorry, but what is a change event? Again, I am a novice at this VBA stuff, so I'm not sure how to use a change event to trigger the macro. Still not sure if the code works, because again, I don't know how to trigger it. Hopefully you can help me put in a change event.

    Thanks so much

  8. #8
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    See link for more on change events. I was suggest a worksheet change event

    http://www.cpearson.com/excel/Events.aspx

    Basically you could right the sheet tab > select code > paste in the below
    When B4 is changed to zero the cells are locked otherwise they are unlocked
    Please Login or Register  to view this content.
    VBA Noob

  9. #9
    Registered User
    Join Date
    12-15-2008
    Location
    hanover
    Posts
    34

    Smile thank you so much

    Wow, it finally works. Thank you so much for your assistance. I truly could not have accomplished this without your help. One last request. Is it possible to also have the locked cell make a chime noise at the user, if they try to enter data into it when it is locked? If possible, how would you add this into the previous code that you sent me?

  10. #10
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Why? The locked cell message appears

    Here's a beep

    Please Login or Register  to view this content.
    VBA Noob
    Last edited by VBA Noob; 12-16-2008 at 04:49 PM.

  11. #11
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Or select the range E14:E47 and using Conditional formatting to colour the cells when unlocked

    =CELL("protect",E14)>0
    VBA Noob

  12. #12
    Registered User
    Join Date
    12-15-2008
    Location
    hanover
    Posts
    34

    Unhappy Little problem

    Now that the code that you suggested works for this technician, I went to copy it over for the other technicians so that for example if Cell B5=0, then now cells F14-F47 are locked and so on and so forth for all of the technicians. But I keep getting this Compile error, that says Ambiguous name detected: Worksheet_change. Can't I use the Worksheet_change command for all the technicians?
    Attached Files Attached Files

  13. #13
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Maybe

    Please Login or Register  to view this content.
    VBA Noob
    Last edited by VBA Noob; 12-16-2008 at 05:21 PM.

  14. #14
    Registered User
    Join Date
    12-15-2008
    Location
    hanover
    Posts
    34

    Red face not quite working

    The locked cells are kind of working, but there must be a little glitch. If I give the technician say 6 available hours, it will let me assign time under their name in the appointment part of the worksheet, but if I put my cursor in the cell where I had assigned their time, and then use the delete button on my keyboard, then it will no longer let me re-enter a number in that cell. It is now locked. The only time those cells should be locked, is if their corresponding available hours is zero.

    Also the beep is not working, but that doesn't really matter to me. It really is not important. Let's just get the other working. I should know better than to try and accomplish too many tasks at once

    Thanks again

  15. #15
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    See if my example helps

    VBA Noob
    Attached Files Attached Files

  16. #16
    Registered User
    Join Date
    12-15-2008
    Location
    hanover
    Posts
    34

    Red face

    even in your example, if I set Tony's available hours to 7, then I go into Cell E14 and type 2. Then maybe I decide that is wrong, now I click in cell E14, and click delete on my keyboard. Now I want to re-enter 1 in Cell E14, B4 is still 7, but for some reason now the cell is locked and won't let you enter data.
    Hope this makes sense

  17. #17
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Try

    Please Login or Register  to view this content.
    VBA Noob

  18. #18
    Registered User
    Join Date
    12-15-2008
    Location
    hanover
    Posts
    34

    Smile Solved

    Thank you so much for all of your help. The sheet seems to be working as planned, and I really could not have did it without your help.
    Could you please mark this thread as SOLVED.

+ 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