+ Reply to Thread
Results 1 to 5 of 5

For Each / Next

  1. #1
    Registered User
    Join Date
    03-03-2005
    Posts
    20

    For Each / Next

    I need some help using for each / next, as I am not very familiar with how to use it. I have a worksheet with many combo boxes. I want to ensure that all of these drop downs have an answer before being emailed back to me for review. I was thinking something along these lines...

    Dim Question As ComboBox
    Dim Question As String
    Dim WS As Worksheet

    For Each WS In Worksheets (do not know what i need here)
    If Question = "" Then
    Msg = MsgBox("All questions have not been completed. Please double-check the drop down boxes to ensure no questions were left unanswered.", vbOKOnly, "Missing Answers")
    End If
    Next Question

    Can anyone help me?

    Thank you,

    NicB.
    Last edited by NicB.; 05-22-2006 at 09:52 AM.

  2. #2
    Tom Ogilvy
    Guest

    RE: For Each / Next

    Are these comoboxes from the control toolbox toolbar or from the forms toolbar?


    for control toolbox:

    Dim sh as Worksheet, obj as OleObject
    Dim msg as Variant
    for sh in thisworkbook.worksheets
    for each obj in sh.OleObjects
    if typeof obj.Object is MSForms.Combobox then
    if obj.Object.Value = "" then
    Msg = MsgBox("All questions have not been completed. " & _
    " Please double-check the drop down boxes to ensure no " & _
    "questions were left unanswered.", vbOKOnly, "Missing Answers")
    exit sub
    end if
    end if
    Next
    Next

    for forms toolbar:

    Dim sh as Worksheet, cbox as DropDown
    for each sh in ThisWorkbook.Worksheets
    for each cbox in sh.Dropdowns
    if cbox.Value = "" then
    Msg = MsgBox("All questions have not been completed. " & _
    " Please double-check the drop down boxes to ensure no " & _
    "questions were left unanswered.", vbOKOnly, "Missing Answers")
    exit sub
    End if
    Next
    Next

    --
    Regards,
    Tom Ogilvy

    "NicB." wrote:

    >
    > I need some help using for each / next, as I am not very familiar with
    > how to use it. I have a worksheet with many combo boxes. I want to
    > ensure that all of these drop downs have an answer before being emailed
    > back to me for review. I was thinking something along these lines...
    >
    > Dim Question As ComboBox
    > Question = "Question"
    > Dim WS As Worksheet
    >
    > For Each WS In Worksheets (do not know what i need here)
    > If Question = "" Then
    > Msg = MsgBox("All questions have not been completed. Please
    > double-check the drop down boxes to ensure no questions were left
    > unanswered.", vbOKOnly, "Missing Answers")
    > End If
    > Next Question
    >
    > Can anyone help me?
    >
    > Thank you,
    >
    > NicB.
    >
    >
    > --
    > NicB.
    > ------------------------------------------------------------------------
    > NicB.'s Profile: http://www.excelforum.com/member.php...o&userid=20639
    > View this thread: http://www.excelforum.com/showthread...hreadid=544297
    >
    >


  3. #3
    Registered User
    Join Date
    03-03-2005
    Posts
    20

    Thumbs up

    Thank you, Tom. The combo-boxes are controls, not forms. Your code worked very well, with the exception of one minor detail. The code checks all sheets in the workbook, what if I needed it to only reference one sheet? Is there a way to specify the worksheet? I will try to solve this, but if you have the answer, I would greatly appreciate it.

    Thanks again for your help!

    NicB.

  4. #4
    Tom Ogilvy
    Guest

    Re: For Each / Next

    I checked all sheets because the sample code you posted appeared to be doing
    that. Certainly you can just set the variable Sh to refer to a single sheet
    and remove that outer loop.

    Dim sh as Worksheet, obj as OleObject
    Dim msg as Variant
    set sh = worksheets("Sheet1")
    for each obj in sh.OleObjects
    if typeof obj.Object is MSForms.Combobox then
    if obj.Object.Value = "" then
    Msg = MsgBox("All questions have not been completed. " & _
    " Please double-check the drop down boxes to ensure no " & _
    "questions were left unanswered.", vbOKOnly, "Missing Answers")
    exit sub
    end if
    end if
    Next

    --
    Regards,
    Tom Ogilvy


    "NicB." wrote:

    >
    > Thank you, Tom. The combo-boxes are controls, not forms. Your code
    > worked very well, with the exception of one minor detail. The code
    > checks all sheets in the workbook, what if I needed it to only
    > reference one sheet? Is there a way to specify the worksheet? I will
    > try to solve this, but if you have the answer, I would greatly
    > appreciate it.
    >
    > Thanks again for your help!
    >
    > NicB.
    >
    >
    > --
    > NicB.
    > ------------------------------------------------------------------------
    > NicB.'s Profile: http://www.excelforum.com/member.php...o&userid=20639
    > View this thread: http://www.excelforum.com/showthread...hreadid=544297
    >
    >


  5. #5
    Registered User
    Join Date
    03-03-2005
    Posts
    20

    Talking

    Worked beautifully. I greatly appreciate your help, Tom!

    NicB.

+ 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