+ Reply to Thread
Results 1 to 15 of 15

Need help with "For Each Cel" command.

Hybrid View

  1. #1
    Registered User
    Join Date
    01-08-2015
    Location
    Finland
    MS-Off Ver
    10
    Posts
    9

    Need help with "For Each Cel" command.

    Hi, could anyone help me with this task please?
    In words, I want something like this:
    1. If each cell value ( all cells) in Range ("A10:A13, A15:A18, A20:A22, A24:A26") = False , then MsgBox ("check 1")
    2. If at list one (or 2 or all) cells = true in Range ( “A24”:A26”), then Range(“F27”) = “ fail/ incorrect or some other text
    Else (otherwise)
    Range(“F27”)= “average/ or other text”

    I tried with next one ( below), but it did not work from the beginning =/ something is wrong…
    For Each Cel In Range("A10:A13,A15:A18,A20:A22,A24:A26")
    If Cel.Value = False Then MsgBox ("check 1")
    End
    Next
    Thanks in advance
    Last edited by Leith Ross; 01-08-2015 at 04:45 PM. Reason: Added Code Tags

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Need help with "For Each Cel" command.

    did not work why? did you get an error, it doesn't do what you want... etc. Need more details.
    you want to check all cells in your range, and only if they are all false you want to message? You know that a blank cell counts as False if you are checking for boolean values?
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Registered User
    Join Date
    01-08-2015
    Location
    Finland
    MS-Off Ver
    10
    Posts
    9

    Re: Need help with "For Each Cel" command.

    Yes, I want to check them all, and if they are all false, I want message.

    It do not work as I want…. If range “A11” is “true” and all other = “false” it gives me box with message. But I don’t want it to appear if one of the cells is true…

    Sorry, I don’t know what “boolean values” means in this case . This is my first task in VBA excel. ( this is only one small part of whole exercise) and i am little bit stuck here =/
    Last edited by Clickme; 01-08-2015 at 03:20 PM. Reason: text added

  4. #4
    Registered User
    Join Date
    01-08-2015
    Location
    Finland
    MS-Off Ver
    10
    Posts
    9

    Re: Need help with "For Each Cel" command.

    Ok, boolean = 2 byte = value True or False

  5. #5
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Need help with "For Each Cel" command.

    Hi Clickme
    boolean is a data type (like string, long, integer, etc.) Boolean variables are either True or False.
    try replacing your code with this:

    dim checker as boolean
    checker = true
    For Each Cel In Range("A10:A13,A15:A18,A20:A22,A24:A26")
         If Cel.Value = true Then checker = false
         End
    Next
    if checker = true then msgbox("Check 1")
    Last edited by Arkadi; 01-08-2015 at 03:33 PM.

  6. #6
    Registered User
    Join Date
    01-08-2015
    Location
    Finland
    MS-Off Ver
    10
    Posts
    9

    Re: Need help with "For Each Cel" command.

    Now box is not appearing, when all cells are false =(

  7. #7
    Registered User
    Join Date
    01-08-2015
    Location
    Finland
    MS-Off Ver
    10
    Posts
    9

    Re: Need help with "For Each Cel" command.

    Quote Originally Posted by Arkadi View Post
    Hi Clickme
    boolean is a data type (like string, long, integer, etc.) Boolean variables are either True or False.
    try replacing your code with this:

    dim checker as boolean
    checker = true
    For Each Cel In Range("A10:A13,A15:A18,A20:A22,A24:A26")
         If Cel.Value = true Then checker = false
         End
    Next
    if checker = true then msgbox("Check 1")
    box is not appearing , when all cells = false

  8. #8
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Need help with "For Each Cel" command.

    OOPS!

    dim checker as boolean
    checker = true
    For Each Cel In Range("A10:A13,A15:A18,A20:A22,A24:A26")
         If Cel.Value = true Then 
              checker = false
              End
         end if
    Next
    if checker = true then msgbox("Check 1")

  9. #9
    Registered User
    Join Date
    01-08-2015
    Location
    Finland
    MS-Off Ver
    10
    Posts
    9

    Re: Need help with "For Each Cel" command.

    Yeee, first problem solved Now it works... Thanks!

  10. #10
    Registered User
    Join Date
    01-08-2015
    Location
    Finland
    MS-Off Ver
    10
    Posts
    9

    Re: Need help with "For Each Cel" command.

    I am using " the bottons " from control to change values of range ("A10:A13,A15:A18,A20:A22,A24:A26") TRUE <-> FALSE

  11. #11
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Need help with "For Each Cel" command.

    instead of End you could also use Exit For... this would keep code below the check running instead of just ending the macro.

  12. #12
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Need help with "For Each Cel" command.

    I posted my correction above, forgot to keep the "End" inside my IF statement, it should work fine that way now.

  13. #13
    Registered User
    Join Date
    01-08-2015
    Location
    Finland
    MS-Off Ver
    10
    Posts
    9

    Re: Need help with "For Each Cel" command.

    Solutions for the second problem are also welcome

    Note, I corrected second problem (Range(“F27”) = “ fail/ incorrect or some other text” ) and also " average" is a text, not equation ) Sorry about this confusion...

    I will try to fix it by myself tomorrow using/modifying previous example.
    I know that it cold be done like:
    If Range("A24") = True Then
    Range("F27") = "Fail / or other text"
    Else
    Range("B27")= " average/ or some other text"
    End If
    and so on...

    But I need shorter version, because I will use it several time....

    P.S.
    Sorry for my bad english
    Last edited by Leith Ross; 01-08-2015 at 04:47 PM. Reason: Added Code Tags

  14. #14
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Need help with "For Each Cel" command.

    so...

    Range("F27")="average/ or other text" 'this will make F27 average, but if any cells in range are true (below) then it will change to fail/...
    for each cel in range("A24:A26")
         if cel.value = true then 
              Range("F27").value = " fail/ incorrect or some other text"
              exit for
         endif
    next

  15. #15
    Registered User
    Join Date
    01-08-2015
    Location
    Finland
    MS-Off Ver
    10
    Posts
    9

    Re: Need help with "For Each Cel" command.

    Second problem solved.
    Arkadi, thanks for your help

+ 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. Adding "Command Button" and "Messages Box" in VBA Macro
    By sanjeevkumarmc in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-09-2014, 07:21 AM
  2. Replies: 3
    Last Post: 05-31-2013, 05:16 AM
  3. Create a Command Button to "Save As" and "Close" an Excel Workbook
    By thedunna in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-26-2013, 05:38 PM
  4. How to change a Command Button caption from "Enable" to "Disable"?
    By Infinity in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-22-2007, 12:14 AM
  5. [SOLVED] inserting a conditional "go to" command on a excel "if" function
    By velasques in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-10-2006, 04:20 PM

Tags for this Thread

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