+ Reply to Thread
Results 1 to 4 of 4

problem in FOR EACH loop

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-20-2007
    MS-Off Ver
    2016, 365
    Posts
    127

    problem in FOR EACH loop

    Hi All.
    I create procedure with loop and try to call another procudure when match found.
    Sub ProcessSheets(btnName As String, assignSheetName As String)
        Dim wsExec As Worksheet
        Dim sheetNames As Variant
        Dim loopSheetName As Variant
        
        ' List of sheet names
        sheetNames = Array("W1", "W2", "W3", "W4", "W5")
        
        ' Find and set the execution sheet based on the button name
        For Each loopSheetName In sheetNames
            On Error Resume Next
            Set wsExec = ThisWorkbook.Sheets(loopSheetName)
            On Error GoTo 0
            
            If Not wsExec Is Nothing Then
                ' Call ProcessButton with the appropriate parameters
                ProcessButton wsExec, btnName, assignSheetName
            End If
        Next loopSheetName
    End Sub
    After line -
    ProcessButton wsExec, btnName, assignSheetName execution
    jump to ProcessButton() procedure
    When I commented "IF" statement in the loop I'm getting iteration.
    What is wrong? How to fix problem?

    Thanks

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: problem in FOR EACH loop


    Hi,

    'cause you badly use On Error Resume Next then do no forget at least to set the object variable to Nothing just before !

  3. #3
    Forum Contributor
    Join Date
    07-20-2007
    MS-Off Ver
    2016, 365
    Posts
    127

    Re: problem in FOR EACH loop

    Hi Marc L. Thanks for reply.
    When I debugged that procedure without IF statement
    Sub ProcessSheets(btnName As String, assignSheetName As String)
        Dim wsExec As Worksheet
        Dim sheetNames As Variant
        Dim loopSheetName As Variant
        
        ' List of sheet names
        sheetNames = Array("W1", "W2", "W3", "W4", "W5")
        
        ' Find and set the execution sheet based on the button name
        For Each loopSheetName In sheetNames
            On Error Resume Next
            Set wsExec = ThisWorkbook.Sheets(loopSheetName)
            On Error GoTo 0
         Next loopSheetName
    End Sub
    loop works normally.
    I would be grateful if you show how to solve the problem?
    Thanks
    Last edited by eugz; 08-22-2023 at 07:55 PM.

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: problem in FOR EACH loop


    And what about to just follow & try post #2 ?

+ 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. For Next Loop Problem
    By gyster in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 07-10-2016, 08:33 PM
  2. [SOLVED] VPA LOOP Problem
    By Jarred11 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-07-2016, 09:01 PM
  3. [SOLVED] Problem with a loop
    By z0rdd in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-21-2016, 07:27 AM
  4. Vlookup problem in a loop with cell property and variable cell problem (long title sry)
    By ExcelsiorLux in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-13-2013, 10:38 AM
  5. [SOLVED] Loop inside a loop problem!
    By questionguy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-22-2012, 12:54 PM
  6. [SOLVED] do loop problem
    By dwalkerc@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-20-2006, 04:20 PM
  7. [SOLVED] Problem adding charts using Do-Loop Until loop
    By Chris Bromley in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-23-2005, 09:06 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